#include <iostream>
using namespace std;
int Nod (int c, int d)
{
if (d==0)
return c;
if (c%d==0)
return 1;
return Nod (d, c%d);
}
class Rational
{int a,b; //a-числитель b-знаменатель//
public:
Rational ()
{
a=0;
b=1;
}
Rational (int chislit, int znamenat);
Rational Add (const Rational&) const;
Rational Sub (const Rational&) const;
Rational Mult (const Rational&) const;
Rational Div (const Rational&) const;
void Print () const;
void Change (int x,int y);
};
Rational::Rational (int chislit, int znamenat)
{a=chislit;
if(znamenat)
b=znamenat;
else
{cout<<"Ne mogu podelit na ZERO"<<endl;}
}
Rational Rational::Add (const Rational&s) const
{
int c;
Rational Sum;
Sum.a=a*s.b+b*s.a;
Sum.b=b*s.b;
if (Sum.a>Sum.b)
c=Nod (Sum.b, Sum.a);
else
c=Nod (Sum.b, Sum.a);
Sum.a=Sum.a/c;
Sum.b=Sum.b/c;
return Sum;
}
Rational Rational::Sub (const Rational&s) const
{
int c;
Rational Sum;
Sum.a=a*s.b-b*s.a;
Sum.b=b*s.b;
if (Sum.a>Sum.b)
c=Nod (Sum.b, Sum.a);
else
c=Nod (Sum.b, Sum.a);
Sum.a=Sum.a/c;
Sum.b=Sum.b/c;
return Sum;
}
Rational Rational::Mult (const Rational&s) const
{
int c;
Rational Sum;
Sum.a=a*s.a;
Sum.b=b*s.b;
if (Sum.a>Sum.b)
c=Nod (Sum.b, Sum.a);
else
c=Nod (Sum.b, Sum.a);
Sum.a=Sum.a/c;
Sum.b=Sum.b/c;
return Sum;
}
Rational Rational::Div (const Rational&s) const
{
int c;
Rational Sum;
Sum.a=a*s.b;
Sum.b=b*s.a;
if (s.a==0)
{cout<<"nelzia delit na Zero"<<endl;};
if (Sum.a>Sum.b)
c=Nod (Sum.b, Sum.a);
else
c=Nod (Sum.b, Sum.a);
Sum.a=Sum.a/c;
Sum.b=Sum.b/c;
return Sum;
}
void Rational::Print () const
{cout<<a<<"/"<<b<<endl;}
void Rational::Change (int d, int e)
{int f;
a=d;
if (e)
b=e;
else
{cout<<"ne mogu delit na zero"<<endl;};
if (a>b)
f=Nod(a,b);
else
f=Nod(b,a);
a=a/f;
b=b/f;
}
int main ()
{
Rational R1 (7,11), R2 (0,7), R3;
R3=R1.Add(R2);
R1.Print();
R2.Print();
R3.Print();
R3=R1.Sub(R2);
R3.Print();
R3=R2.Sub(R1);
R3.Print();
R3=R1.Mult(R2);
R3.Print();
R3=R1.Div(R2);
R3.Print();
return 0;
}