问题及代码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cop
*作者:
*完成日期:2015年5月17日
*版本号:v1.0
*
*问题描述:
*输入描述:
*输出描述:
#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(int n=0,int d=1){nume=n;deno=d;}
friend ostream &operator<<(ostream &out,CFraction c);
friend istream &operator>>(istream &in,CFraction &c);
CFraction operator+();
CFraction operator-();
CFraction operator~();
void display();
void simply();
CFraction operator+(const CFraction &t);
CFraction operator-(const CFraction &t);
CFraction operator*(const CFraction &t);
CFraction operator /(const CFraction &t);
bool operator>(const CFraction &t);
bool operator<(const CFraction &t);
bool operator>=(const CFraction &t);
bool operator<=(const CFraction &t);
bool operator==(const CFraction &t);
bool operator!=(const CFraction &t);
};
//重载函数的实现及用于测试的main()函数
void CFraction::display()
{
cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simply()
{
int a,b,c;
if(nume>deno){a=nume;b=deno;}
else {a=deno;b=nume;}
while(a%b)
{
c=a%b;
a=b;
b=c;
}
nume=nume/b;
deno=deno/b;
}
ostream &operator<<(ostream&out,CFraction c)
{
out<<c.nume<<"/"<<c.deno<<endl;
return out;
}
istream &operator>>(istream&in,CFraction &c)
{
char a;
while(1)
{
in>>c.nume>>a>>c.deno;
if(c.deno!=0&&a=='/')break;
else
cout<<"输入格式错误,请从新输入:"<<endl;
}
}
CFraction CFraction::operator-()
{
nume=-nume;
return *this;
}
CFraction CFraction::operator+()
{
return *this;
}
CFraction CFraction::operator~()
{
int a;
a=nume;
nume=deno;
deno=a;
return *this;
}
CFraction CFraction::operator+(const CFraction &t)
{
CFraction c1;
c1.deno=deno*t.deno;
c1.nume=nume*t.deno+t.nume*deno;
c1.simply();
return c1;
}
CFraction CFraction::operator-(const CFraction &t)
{
CFraction c1;
c1.deno=deno*t.deno;
c1.nume=nume*t.deno-t.nume*deno;
c1.simply();
return c1;
}
CFraction CFraction::operator*(const CFraction &t)
{
CFraction c1;
c1.deno=deno*t.deno;
c1.nume=nume*t.nume;
return c1;
}
CFraction CFraction::operator/(const CFraction &t)
{
CFraction c1;
c1.deno=deno*t.nume;
c1.nume=nume*t.deno;
return c1;
}
bool CFraction::operator>(const CFraction &t)
{
if(nume*t.deno>t.nume*deno)
return true;
else return false;
}
bool CFraction::operator<(const CFraction &t)
{
if(nume*t.deno>t.nume*deno)
return false;
else return true;
}
bool CFraction::operator<=(const CFraction &t)
{
if(*this<t==true) return false;
else return true;
}
bool CFraction::operator>=(const CFraction &t)
{
if(*this>t==true) return false;
else return true;
}
bool CFraction::operator==(const CFraction &t)
{
if((*this>t==false)&&(*this<t==false)) return true;
else return false;
}
bool CFraction::operator!=(const CFraction &t)
{
if((*this==t)==false) return true;
else return false;
}
int main()
{
CFraction x,y,s;
cout<<"输入x: ";
cin>>x;
cout<<"输入y: ";
cin>>y;
s=+x+y;
cout<<"+x+y="<<s<<endl;
s=x-y;
cout<<"x-y="<<s<<endl;
s=x*y;
cout<<"x*y="<<s<<endl;
s=x/y;
cout<<"x/y="<<s<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒数: "<<~x<<endl;
cout<<x;
if (x>y) cout<<"大于";
if (x<y) cout<<"小于";
if (x==y) cout<<"等于";
cout<<y<<endl;
return 0;
}
运行结果: