源程序:
#include<iostream.h>
//using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Simplify(); //化简(使分子分母没有公因子)
friend istream& operator >> (istream&, CFraction &);
friend ostream& operator << (ostream&, CFraction &);
CFraction operator +(CFraction &c); //声明重载运算符 + 的函数
CFraction operator -(CFraction &c); //声明重载运算符 + 的函数
CFraction operator *(CFraction &c); //声明重载运算符 + 的函数
CFraction operator /(CFraction &c); //声明重载运算符 + 的函数
bool operator > (CFraction &c);
bool operator < (CFraction &c);
bool operator >= (CFraction &c);
bool operator <= (CFraction &c);
bool operator == (CFraction &c);
bool operator != (CFraction &c);
};
CFraction::CFraction(int nu, int de)
{
nume = nu;
deno = de;
}
istream& operator >> (istream& input, CFraction &c)
{
char f;
while(1)
{
cout << "请依次输入要操作的分数的分子和分母(用“/”隔开):";
input >> c.nume >> f >> c.deno;
if(f != '/')
cout << "输入格式有误!!!" << endl;
else
break;
}
return input;
}
ostream& operator << (ostream& output, CFraction &c)
{
output << c.nume << "/" << c.deno << endl;
c.Simplify();
if( c.nume % c.deno == 0)
output << "化到最简后的分数为:" << c.nume / c.deno << endl;
else
output << "化到最简后的分数为:" << c.nume << '/' << c.deno << endl;
int n;
output << "化到真分数的分数为:";
if( c.nume % c.deno == 0)
output<< c.nume / c.deno << endl;
else
{
if( c.nume < c.deno)
output<< c.nume << '/' << c.deno << endl;
else
{
n = c.nume / c.deno;
output<< n << "(" << c.nume - n * c.deno << '/' << c.deno <<")"<<endl;
}
}
return output;
}
void CFraction::Simplify() //化简(使分子分母没有公因子)
{
int i, a;
if(nume > deno)
{
a = nume;
}
else
{
a = deno;
}
for(i = a; i > 1; -- i)
{
if(nume % i == 0 && deno % i == 0)
{
nume = nume / i;
deno = deno / i;
}
}
}
CFraction CFraction::operator +(CFraction &c)
{
return CFraction (nume * c.deno + deno * c.nume, deno * c.deno);
}
CFraction CFraction::operator -(CFraction &c)
{
return CFraction (nume * c.deno - deno * c.nume, deno * c.deno);
}
CFraction CFraction::operator *(CFraction &c)
{
return CFraction (nume * c.nume, deno * c.deno);
}
CFraction CFraction::operator /(CFraction &c)
{
return CFraction (nume * c.deno, deno * c.nume);
}
bool CFraction::operator > (CFraction &c)
{
if(deno > c.deno)
{
return false;
}
if(deno < c.deno)
{
return true;
}
if(deno == c.deno)
{
if(nume > c.nume)
return true;
else
return false;
}
}
bool CFraction::operator < (CFraction &c)
{
if(deno > c.deno)
{
return true;
}
if(deno < c.deno)
{
return false;
}
if(deno == c.deno)
{
if(nume < c.nume)
return true;
else
return false;
}
}
bool CFraction::operator >= (CFraction &c)
{
if(*this < c)
return false;
else
return true;
}
bool CFraction::operator <= (CFraction &c)
{
if(*this > c)
return false;
else
return true;
}
bool CFraction::operator == (CFraction &c)
{
if(nume == c.nume && deno == c.deno)
return true;
else
return false;
}
bool CFraction::operator != (CFraction &c)
{
if(nume == c.nume && deno == c.deno)
return false;
else
return true;
}
int main()
{
CFraction c1, c2, c3;
cout << "1.实现两个分数的相加" << endl;
cin >> c1;
cout << "c1 = " << c1;
cin >> c2;
cout << "c2 = " << c2;
c3 = c1 + c2;
cout << "c1 + c2 = " << c3 ;
cout << "2.实现两个分数的相减" << endl;
CFraction c5, c6, c7;
cin >> c5;
cout << "c5 = " << c5;
cin >> c6;
cout << "c6 = " << c6;
c7 = c5 - c6;
cout << "c5 - c6 = " << c7;
cout << "3.实现两个分数的相乘" << endl;
CFraction c8, c9, c10;
cin >> c8;
cout << "c8 = " << c8;
cin >> c9;
cout << "c9 = " << c9;
c10 = c8 * c9;
cout << "c8 * c9 = " << c10;
cout << "4.实现两个分数的相除" << endl;
CFraction c11, c12, c13;
cin >> c11;
cout << "c11 = " << c11;
cin >> c12;
cout << "c12 = " << c12;
c13 = c11 / c12;
cout << "c11 / c12 = " << c13;
cout << "5.实现两个分数大于的比较" << endl;
CFraction c14, c15;
cin >> c14;
cout << "c14 = " << c14;
cin >> c15;
cout << "c15 = " << c15;
cout << "c14 > c15" << (c14.operator >(c15)?" 成立":" 不成立") << endl;
cout << "6.实现两个分数小于的比较" << endl;
CFraction c16, c17;
cin >> c16;
cout << "c16 = " << c16;
cin >> c17;
cout << "c17 = " << c17;
cout << "c16 < c17" << (c16.operator <(c17)?" 成立":" 不成立") << endl;
cout << "7.实现两个分数大于等于的比较" << endl;
CFraction c18, c19;
cin >> c18;
cout << "c18 = " << c18;
cin >> c19;
cout << "c19 = " << c19;
cout << "c18 >= c19" << (c18.operator >=(c19)?" 成立":" 不成立") << endl;
cout << "87.实现两个分数小于等于的比较" << endl;
CFraction c20, c21;
cin >> c20;
cout << "c20 = " << c20;
cin >> c21;
cout << "c21 = " << c21;
cout << "c20 <= c21" << (c20.operator <=(c21)?" 成立":" 不成立") << endl;
cout << "9.实现两个分数等于的比较" << endl;
CFraction c22, c23;
cin >> c22;
cout << "c22 = " << c22;
cin >> c23;
cout << "c23 = " << c23;
cout << "c22 == c23" << (c22.operator ==(c23)?" 成立":" 不成立") << endl;
cout << "7.实现两个分数不等于的比较" << endl;
CFraction c24, c25;
cin >> c24;
cout << "c24 = " << c24;
cin >> c25;
cout << "c25 = " << c25;
cout << "c24 != c25" << (c24.operator !=(c25)?" 成立":" 不成立") << endl;
// system("pause");
return 0;
}
截图:
图是在vs2008上运行时截得···不是伪造的哈~这个任务也不难呢~