1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include<stdexcept>
#include<conio.h>
#include <iostream>
#include<string.h>
using namespace std;
class Rational
{
private:
int x,y;
public:
Rational():x(0),y(0){}
Rational(int a , int b):x(a),y(b){}
void setx(int a)
{
x=a;
}
void sety(int b)
{
y=b;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
void display()
{
cout<<x<<"/"<<y;
}
Rational operator +(Rational r1)
{
Rational temp;
temp.x=x*r1.y+r1.x*y;
temp.y=y*r1.y;
return Rational (temp.x,temp.y);
}
Rational operator -(Rational r1)
{
Rational temp;
temp.x=x*(r1.x);
temp.y=y*(r1.y);
return Rational (temp.x,temp.y);
}
Rational operator *(Rational r1)
{
Rational temp;
temp.x=x*(r1.x);
temp.y=y*r1.y;
return Rational (temp.x,temp.y);
}
Rational operator ~(Rational r1)
{
Rational temp;
temp.x=r1.y;
temp.y=r1.x;
return Rational (temp.x,temp.y)
}
};
int main ()
{
Rational first,second,result,invert;
int n1,n2,d1,d2;
cout<<"Enter numeration and denominator for first value\n";
cin>>n1>>d1;
first.setx(n1);
first.sety(d1);
first.display();
cout<<endl;
cout<<"Enter numeration and denominator for second value\n";
cin>>n2>>d2;
second.setx(n2);
second.sety(d2);
second.display();
cout<<endl;
result=first+second;
first.display();
cout<<"+";
second.display();
cout<<"=";
result.display();
cout<<endl;
result=first-second;
first.display();
cout<<"-";
second.display();
cout<<"=";
result.display();
cout<<endl;
result=first*second;
first.display();
cout<<"*";
second.display();
cout<<"=";
result.display();
cout<<endl;
invert=~result;
cout<<endl;
cout<<"Inverting:";
result.display();
cout<" :";
invert.display();
}
|