sâmbătă, 8 septembrie 2018

Clasa numere rationale

# include <iostream.h>;
# include <stdio.h>;
# include <conio.h>;
# include <math.h>;

class RATIONAL
      {
       public:
      int x,y;
      RATIONAL()
      {x=0;y=1;}
      RATIONAL(int a,int b);//:x(a),y(b){};
      RATIONAL operator+(RATIONAL &);
      RATIONAL operator-(RATIONAL &);
      RATIONAL operator*(RATIONAL &);
      RATIONAL operator/(RATIONAL &);
      RATIONAL operator-();
      RATIONAL operator~();
      friend ostream & operator<<(ostream& out, RATIONAL &);
      friend istream & operator>>(istream& out, RATIONAL &);
       private:
       int cmmdc(int a,int b)
       {
if(a%b==0)
  return b;
else
    return abs(cmmdc(b,a%b));
       }
      };

RATIONAL :: RATIONAL(int a,int b)
{
if(b==0) b=1;
x=a;
y=b;
}
RATIONAL RATIONAL :: operator+(RATIONAL & b)
{
RATIONAL c;
c.x=x*b.y+b.x*y;
c.y=y*b.y;
int d=cmmdc(c.x,c.y);
return RATIONAL(c.x/d,c.y/d);
}
RATIONAL RATIONAL :: operator-()
{
return RATIONAL(-x,-y);
}
RATIONAL RATIONAL :: operator-(RATIONAL & b)
{
RATIONAL c;
c=-b;
return (c+ *this);
}
RATIONAL RATIONAL :: operator*(RATIONAL & b)
{
RATIONAL c;
c.x=x*b.x;
c.y=y*b.y;
int d=cmmdc(c.x,c.y);
return RATIONAL(c.x/d,c.y/d);
}
RATIONAL RATIONAL :: operator~()
{
return RATIONAL(this->y,this->x);
}
RATIONAL RATIONAL :: operator/(RATIONAL & d)
{
return (*this)*(~d);
}

ostream & operator<<(ostream & out, RATIONAL & obj)
{
out<<obj.x<<"/"<<obj.y;
return out;
}
istream & operator>>(istream & in, RATIONAL & obj)
{
scanf("%d/%d",&obj.x,&obj.y);
return in;
}

Varianta 2:

# include <iostream.h>;
# include <stdio.h>;
# include <conio.h>;
# include <math.h>;

class RATIONAL
      {
       public:
      int x,y;
      RATIONAL()
      {x=0;y=1;}
      RATIONAL(int a,int b);//:x(a),y(b){};
      RATIONAL operator+(RATIONAL &);
      RATIONAL operator-(RATIONAL &);
      RATIONAL operator*(RATIONAL &);
      RATIONAL operator/(RATIONAL &);
      RATIONAL operator-();
      RATIONAL operator~();
      friend ostream & operator<<(ostream& out, RATIONAL &);
      friend istream & operator>>(istream& out, RATIONAL &);
       private:
       int cmmdc(int a,int b)
       {
if(a%b==0)
  return b;
else
    return abs(cmmdc(b,a%b));
       }
      };

RATIONAL :: RATIONAL(int a,int b)
{
if(b==0) b=1;
x=a;
y=b;
}
RATIONAL RATIONAL :: operator+(RATIONAL & b)
{
RATIONAL c;
c.x=x*b.y+b.x*y;
c.y=y*b.y;
int d=cmmdc(c.x,c.y);
return RATIONAL(c.x/d,c.y/d);
}
RATIONAL RATIONAL :: operator-()
{
return RATIONAL(-x,-y);
}
RATIONAL RATIONAL :: operator-(RATIONAL & b)
{
RATIONAL c;
c=-b;
return (c+ *this);
}
RATIONAL RATIONAL :: operator*(RATIONAL & b)
{
RATIONAL c;
c.x=x*b.x;
c.y=y*b.y;
int d=cmmdc(c.x,c.y);
return RATIONAL(c.x/d,c.y/d);
}
RATIONAL RATIONAL :: operator~()
{
return RATIONAL(this->y,this->x);
}
RATIONAL RATIONAL :: operator/(RATIONAL & d)
{
return (*this)*(~d);
}

ostream & operator<<(ostream & out, RATIONAL & obj)
{
out<<obj.x<<"/"<<obj.y;
return out;
}
istream & operator>>(istream & in, RATIONAL & obj)
{
scanf("%d/%d",&obj.x,&obj.y);
return in;
}

void main(void)
{
RATIONAL r(3,7),z;

clrscr();

z=-r;
cout<<z<<endl;

z=~r;
cout<<z<<endl;

cout<<"\Intr un nr RATIONAL sub forma a/b: ";
cin>>z;
cout<<z<<endl;

//

RATIONAL a,b,c;
cout<<"\Intr un nr RATIONAL sub forma a/b: ";
 cin>>a;
cout<<"\Intr un nr RATIONAL sub forma a/b: ";
 cin>>b;

c=a+b;
cout<<c<<endl;

c=a-b;
cout<<c<<endl;

c=a*b;
cout<<c<<endl;

c=a/b;
cout<<c<<endl;

getche();
}

Niciun comentariu:

Trimiteți un comentariu