您的位置:首页 > 其它

第8周任务4(分数类中的对象可以和整型数进行四则运算,且满足交换律)

2012-04-10 22:54 344 查看
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作    者:   臧鹏
* 完成日期:   2012   年  4 月 10  日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:在任务3的基础上扩展,分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream>

using namespace std;

class CFraction

{

private:

int nume; // 分子

int deno; // 分母

public:

CFraction(int nu=0,int de=1); //构造函数,初始化用

void Set(int nu=0,int de=1); //置值,改变值时用

friend CFraction operator+(CFraction &t1,CFraction &t2);

friend CFraction operator-(CFraction &t1,CFraction &t2);

friend CFraction operator*(CFraction &t1,CFraction &t2);

friend CFraction operator/(CFraction &t1,CFraction &t2);

friend CFraction operator-(CFraction &t);//取反

friend CFraction operator+(CFraction &t,int h);   //整数在后

friend CFraction operator-(CFraction &t,int h);

friend CFraction operator*(CFraction &t,int h);

friend CFraction operator/(CFraction &t,int h);

friend CFraction operator+(int h,CFraction &t); //整数在前

friend CFraction operator-(int h,CFraction &t);

friend CFraction operator*(int h,CFraction &t);

friend CFraction operator/(int h,CFraction &t);

bool operator>(CFraction &t);

bool operator<(CFraction &t);

bool operator>=(CFraction &t);

bool operator<=(CFraction &t);

bool operator==(CFraction &t);

bool operator!=(CFraction &t);

void Simplify(int n);            //化简(使分子分母没有公因子)

int gcd(int x,int y);

void output(int style=0);   //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始化用

};

CFraction::CFraction(int nu,int de)     //构造函数
{
nume = nu;
deno = de;

}

void CFraction::Set(int nu,int de) //置值,改变值时用
{
if(de !=0)
{
nume = nu;
deno = de;
}
else
{
cout<<"分母不能为零"<<endl;
exit(0);
}

nume = nu;

deno = de;

}

void CFraction::Simplify(int n)        //化简(使分子分母没有公因子)
{
n = gcd(nume,deno);

nume = nume/n;
deno = deno/n;

}

int CFraction::gcd(int x,int y)  //求公约数的函数
{
int r;
while( y!= 0)
{
r = x%y;
x = y;
y = r;
}
return x;
}

void CFraction::output(int style)   //输出:以8/6为例,style为0时,输出8/6;CFraction(int nu=0,int de=1); //构造函数,初始化用

{
switch(style)
{

case 0:
{
cout<<nume<<'/'<<deno<<endl;
break;
}
case 1:
{
int m = gcd(nume,deno);
cout<<(nume/m)<<'/'<<(deno/m)<<endl;
break;
}
case 2:
{
int nu,de,g;
g = gcd(nume,deno);
nu = nume/g;
de = deno/g;
cout<<(nu/de)<<"("<<(nu%de)<<'/'<<de<<")"<<endl;
break;
}
default:
{
cout<<nume<<'/'<<deno<<endl;
}
}

}
CFraction operator+(CFraction &t1,CFraction &t2)
{
CFraction cf;
if(t1.deno==t2.deno)  //分母相同时加分子
{
cf.deno=t1.deno;
cf.nume=t1.nume+t2.nume;
}
else  //否则同分比较分子
{
cf.deno=t1.deno*t2.deno;
cf.nume=t1.nume*t2.deno+t1.deno*t2.nume;
}
return cf;
}
CFraction operator-(CFraction &t1,CFraction &t2)
{
CFraction cf;
if(t1.deno==t2.deno)
{
cf.deno=t1.deno;
cf.nume=t1.nume-t2.nume;
}
else
{
cf.deno=t1.deno*t2.deno;
cf.nume=t2.deno*t1.nume-t2.nume*t1.deno;
}
return cf;
}
CFraction operator*(CFraction &t1,CFraction &t2)
{
CFraction cf;
cf.deno = t1.deno*t2.deno;
cf.nume = t1.nume*t2.nume;
return cf;
}
CFraction operator/(CFraction &t1,CFraction &t2)
{
CFraction cf;
cf.deno = t1.deno*t2.nume;
cf.nume = t1.nume*t2.deno;
return cf;
}
CFraction operator-(CFraction &t)//取反
{
CFraction cf;
cf.deno  = -t.deno;
cf.nume  = t.nume;
return(cf);
}
bool CFraction::operator>(CFraction &t)
{
if(nume*t.deno>t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator<(CFraction &t)
{
if(nume*t.deno<t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator>=(CFraction &t)
{
if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator<=(CFraction &t)
{
if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator==(CFraction &t)
{
if(nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator!=(CFraction &t)
{
if(!operator==(t))
return true;
else
return false;
}
CFraction operator + (CFraction &t, int h)
{
CFraction t2 = t;

t2.nume = t.nume + h * t.deno;

return t2;
}

CFraction operator - (CFraction &t, int h)
{
CFraction t2 = t;

t2.nume = t.nume - h * t.deno;

return t2;
}

CFraction operator * (CFraction &t, int h)
{
CFraction t2 = t;

t2.nume = t.nume * h;

return t2;
}

CFraction operator / (CFraction &t, int h)
{
CFraction t2 = t;

t2.deno = t.deno * h;

return t2;
}

CFraction operator + (int h, CFraction &t)
{
CFraction t2 = t;

t2.nume = t.nume + h * t.deno;

return t2;
}

CFraction operator - (int h, CFraction &t)
{
CFraction t2 = t;

t2.nume = h * t.deno - t.nume;

return t2;
}

CFraction operator * (int h, CFraction &t)
{

CFraction t2 = t;

t2.nume = h * t.nume;

return t2;
}

CFraction operator / (int h, CFraction &t)
{

CFraction t2 = t;

t2.nume = h * t.deno;

t2.deno = t.nume;

return t2;
}
int main ()
{
CFraction cf1(17,5),cf2(9,6),cf3;
int i =6;

cout<<"cf1 =";
cf1.output ();

cout<<"cf2 =";
cf2.output ();

cf3=cf1*cf2;
cout<<"cf1*cf2=";
cf3.output();

cout<<"约分后:";
cf3.output(1);

cout<<"化简后的形式:";
cf3.output(2);

cf3=cf1-cf2;
cout<<"cf1-cf2=";
cf3.output(1);

cf3=cf1+cf2;
cout<<"cf1+cf2=";
cf3.output(1);

cf3=cf1/cf2;
cout<<"cf1/cf2=";
cf3.output(1);

cf3=-cf1;
cout<<"-cf1 =";
cf3.output(1);

if (cf1>cf2) cout<<"cf1>cf2"<<endl;

if (cf1<cf2) cout<<"cf1<cf2"<<endl;

if (cf1==cf2) cout<<"cf1=cf2"<<endl;

if (cf1!=cf2) cout<<"cf1≠cf2"<<endl;

if (cf1>=cf2) cout<<"cf1≥cf2"<<endl;

if (cf1<=cf2) cout<<"cf1≤cf2"<<endl;

cout<<"i = 6"<<endl;

cf3 = cf1 + i;
cout<<"cf1 + i =";
cf3.output(1);

cf3 = cf1 - i;
cout<<"cf1 - i =";
cf3.output(1);

cf3 = cf1 * i;
cout<<"cf1 * i =";
cf3.output(1);

cf3 = cf1 / i;
cout<<"cf1 / i =";
cf3.output(1);

cf3 = i + cf1 ;
cout<<"i + cf1 =";
cf3.output(1);

cf3 = i - cf1 ;
cout<<"i - cf1 =";
cf3.output(1);

cf3 = i * cf1 ;
cout<<"i * cf1 =";
cf3.output(1);

cf3 = i / cf1 ;
cout<<"i / cf1 =";
cf3.output(1);

system("pause");

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐