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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Rational{
int numerator;
int denominator;
public:
friend ostream& operator<< (ostream &out, Rational &rat);
friend istream& operator>> (istream &in, Rational &rat);
friend const Rational operator +(const Rational &rat1, const Rational &rat2);
friend const Rational operator *(const Rational &rat1, const Rational &rat2);
friend const Rational operator -(const Rational &rat1, const Rational &rat2);
friend const Rational operator /(const Rational &rat1, const Rational &rat2);
friend const Rational operator ==(const Rational &rat1, const Rational &rat2);
Rational();
Rational(int n,int d);
Rational(int wholenumber);
};
Rational::Rational()
{
numerator=0;
denominator=1;
}
Rational::Rational(int n,int d)
{
numerator=n;
denominator=d;
}
Rational::Rational(int wholenumber)
{
numerator=wholenumber;
denominator=1;
}
const Rational operator*(const Rational& rat1,const Rational& rat2)
{
return (rat1.numerator * rat2.numerator, rat1.denominator * rat2.denominator);
}
ostream& operator<< (ostream &out, Rational &rat)
{
if(rat.denominator==0||rat.denominator==1)
{out<<rat.numerator;}
else
{
out << rat.numerator;
out << "/";
out << rat.denominator;
}
}
const Rational operator +(const Rational &rat1, const Rational &rat2)
{
if(rat1.denominator==rat2.denominator)
{
return (rat1.numerator+rat2.numerator,rat1.denominator);
}
else
{
return (rat1.denominator*rat2.denominator,((rat1.numerator*rat2.denominator)+(rat1.numerator*rat2.denominator)));
}
}
const Rational operator -(const Rational &rat1, const Rational &rat2)
{
if(rat1.denominator==rat2.denominator)
{
return(rat1.numerator-rat2.numerator,rat1.denominator);
}
else
{
return(rat1.denominator*rat2.denominator,((rat1.numerator*rat2.denominator)-(rat1.numerator*rat2.denominator)));
}
}
const Rational operator /(const Rational &rat1, const Rational &rat2)
{
if( rat2.denominator==1)
{
return (rat2.numerator*rat1.numerator,rat1.denominator);
}
else
{
return (rat1.numerator*rat2.denominator,rat1.denominator*rat2.numerator);
}
}
istream& operator>> (istream &in, Rational &rat)
{
char a;
in>>rat.numerator>>a>>rat.denominator;
return in;
}
const Rational operator ==(const Rational &rat1, const Rational &rat2)
{
if((rat1.numerator/rat2.numerator==rat1.denominator/rat2.denominator)||(rat2.numerator/rat1.numerator==rat2.denominator/rat1.denominator))
{
return 1;
}
else{ return 0;}
}
int main(){
Rational rat6;
Rational rat1(1,2);
Rational rat2(-1,2);
Rational rat3(-1);
Rational rat4(0);
Rational rat5(5,2);
cout<< "Input Rat 6"<<endl;
cin>>rat6;
cout << "Number1:" <<" " <<rat1;
cout<<endl;
cout << "Number2:" <<" "<<rat2;
cout <<endl;
cout << "Number3:" <<" " <<rat3;
cout <<endl;
cout << "Number4:" <<" " <<rat4;
cout <<endl;
cout << "Number5:" <<" " <<rat5;
cout <<endl;
cout << "Number6:" <<" " <<rat6;
cout <<endl;
cout << "Operation +: "<<(rat1+rat2);
cout <<endl;
cout << "Operation *: "<<(rat1*rat2);
cout<<endl;
cout << "Operation -: "<<(rat1-rat2);
cout<<endl;
cout<< "Operation /: "<<(rat1/rat2);
cout<<endl;
cout<< "Operation==: "<<(rat1==rat2);
cout<<endl;
system("PAUSE");
return 0;
}
|