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
|
#include <cstdlib>
#include <iostream>
using namespace std;
/////////////////////////////////////////
class frac
{
public:
float pay;
char ch;
float payda;
public:
frac(float p,float pd): pay(p),payda(pd)
{ }
frac(): pay(0.0),payda(0.0)
{ }
friend ostream& operator << (ostream& s,frac& k);
friend istream& operator << (istream& s,frac& k);
frac operator + (frac& k)
{
float tempp = pay + k.pay;
float tempd = payda + k.payda;
return frac(tempp,tempd);
}
/////////////////////////////////7
frac operator - (frac& k)
{
float tempp = pay - k.pay;
float tempd = payda - k.payda;
return frac(tempp,tempd);
}
//////////////////////////////////////
frac operator * (frac& k)
{
float tempp = pay * k.pay;
float tempd = payda * k.payda;
return frac(tempp,tempd);
}
//////////////////////////////////
frac operator / (frac& k)
{
float tempp = pay / k.pay;
float tempd = payda / k.payda;
return frac(tempp,tempd);
}
};
//---------------------------------------
istream& operator >> (istream& s,frac& k)
{
char ch;
s >> k.pay >> ch >> k.payda;
return s;
}
ostream& operator << (ostream& s,frac& h)
{
s << "Result:" << endl << h.pay << "/" << h.payda << endl;
return s;
}
int main(int argc, char *argv[])
{
frac s1,s2,s3;
char ch;
cout << "Enter fractions: ";
cin >> s1 >>ch >> s2;
switch(ch)
{
case '+':
s3 = s1 + s2;
break;
case '-':
s3 = s1 - s2;
break;
case '*':
s3 = s1 * s2;
break;
case '/':
s3 = s1 / s2;
}
cout << s3 ;
system("PAUSE");
return EXIT_SUCCESS;
}
|