您的位置:首页 > 其它

第八周任务三

2012-04-11 20:42 246 查看
#include <iostream>
using namespace std;

class CFraction
{
private:
int nume;
int deno;
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de){}
void simplify();
void display();
CFraction operator +(CFraction &c1);
CFraction operator -(CFraction &c1);
CFraction operator *(CFraction &c1);
CFraction operator /(CFraction &c1);
bool operator >(CFraction &c1);
bool operator <(CFraction &c1);
bool operator ==(CFraction &c1);
bool operator !=(CFraction &c1);
bool operator >=(CFraction &c1);
bool operator <=(CFraction &c1);
CFraction operator +();
CFraction operator -();
};
void CFraction::simplify()
{
int x;
int max;
if(nume>deno)
x=nume;
else
x=deno;
for(int i=2;i<x;i++)
if(nume%i==0&&deno%i==0)
{
deno=deno/i;
nume=nume/i;
max=i;
}
}

void CFraction::display()
{
cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}

CFraction CFraction::operator +(CFraction &c1)
{
CFraction c2;

c2.deno=deno*c1.deno;
c2.nume=nume*c1.deno+c1.nume*deno;
c2.simplify();

return (c2);
}

CFraction CFraction::operator -(CFraction &c1)
{
CFraction c2;

c2.deno=deno*c1.deno;
c2.nume=nume*c1.deno-c1.nume*deno;
c2.simplify();

return (c2);
}

CFraction CFraction::operator *(CFraction &c1)
{
CFraction c2;

c2.deno=deno*c1.deno;
c2.nume=nume*c1.nume;
c2.simplify();

return (c2);
}

CFraction CFraction::operator /(CFraction &c1)
{
CFraction c2;

c2.deno=deno*c1.nume;
c2.nume=nume*c1.deno;
c2.simplify();

return (c2);
}

bool CFraction::operator >(CFraction &c1)
{
CFraction  c2,c3;
c2.nume=nume*c1.deno;
c3.nume=c1.nume*deno;

if(c2.nume>c3.nume)
return true;
else
return false;
}

bool CFraction::operator <(CFraction &c1)
{
CFraction  c2,c3;
c2.nume=nume*c1.deno;
c3.nume=c1.nume*deno;

if(c2.nume<c3.nume)
return true;
else
return false;
}

bool CFraction::operator ==(CFraction &c1)
{
if( operator >(c1)!=1&& operator <(c1)!=1)
return true;
else
return false;
}

bool CFraction::operator !=(CFraction &c1)
{
if(operator >(c1)==1|| operator <(c1)==1)
return true;
else
return false;
}

bool CFraction::operator >=(CFraction &c1)
{
if( operator <(c1)!=1)
return true;
else
return false;
}

bool CFraction::operator <=(CFraction &c1)
{
if(operator >(c1)!=1)
return true;
else
return false;
}
CFraction CFraction::operator +()
{
CFraction c2;

c2.nume=nume;
c2.deno=deno;

return (c2);
}

CFraction CFraction::operator -()
{
CFraction c2;

c2.nume=-nume;
c2.deno=deno;

return (c2);
}
int main()
{
CFraction x(1,3),y(-5,10),s;
cout<<"分数x=1/3      y=-5/10"<<endl;
s=+x+y;
cout<<"+x+y=";
s.display();
s=x-y;
cout<<"x-y=";
s.display();
s=x*y;
cout<<"x*y=";
s.display();
s=x/y;
cout<<"x/y=";
s.display();
s=-x+y;
cout<<"-x+y=";
s.display();

x.display();
if (x>y) cout<<"大于"<<endl;
if (x<y) cout<<"小于"<<endl;
if (x==y) cout<<"等于"<<endl;
cout<<endl;
y.display();
system("pause");
return 0;
}


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c include system