/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者:王引琳
* 完成日期: 2012 年 4 月 10 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#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); //置值,改变值时用
void disply(); //按照"nu/de"的格式,如"5/2"的形式输入
void Simplify(); //化简(使分子分母没有公因子)
CFraction operator+(const CFraction &c);//分数相加
CFraction operator-(const CFraction &c);//分数相减
CFraction operator*(const CFraction &c);//分数相乘
CFraction operator/(const CFraction &c);//分数相除
CFraction operator+();//取正运算
CFraction operator-();//取反运算
//比较运算
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator!=(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
CFraction::CFraction(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::Set(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::disply()
{
cout<<nume<<"/"<<deno<<endl;;
}
void CFraction::Simplify()
{
int a,b,c,p;
if(nume<=deno)
{
p=nume;
}
else
p=deno;
for(a=2;a<=p;a++)
{
b=nume%a;
c=deno%a;
if(b==0&&c==0)
{
do
{
nume=nume/a;
deno=deno/a;
b=nume%a;
c=deno%a;
}while(b==0&&c==0);
}
}
}
CFraction CFraction::operator+(const CFraction &c)//分数相加
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno ;
t.deno=deno*c.deno;
t.Simplify();
return t;
}
CFraction CFraction::operator-(const CFraction &c)//分数相减
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
t.Simplify();
return t;
}
CFraction CFraction::operator*(const CFraction &c)//分数相乘
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.Simplify();
return t;
}
CFraction CFraction::operator/(const CFraction &c)//分数相除
{
CFraction t;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.Simplify();
return t;
}
CFraction CFraction::operator+()//取正运算
{
return *this;
}
CFraction CFraction::operator-()//取反运算
{
CFraction t;
t.nume=-nume;
t.deno=-deno;
return t;
}
//比较运算
bool CFraction::operator>(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return true;
else
return false;
}
bool CFraction::operator!=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return false;
else
return true;
}
bool CFraction::operator>=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return false;
else
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return false;
else
return true;
}
void main()
{
CFraction c1(2,5),c2(3,4),c3;
cout<<"c1=";
c1.disply();
cout<<"c2=";
c2.disply ();
c3=c1+c2;
cout<<"c1+c2=";
c3.disply ();
c3=c1-c2;
cout<<"c1-c2=";
c3.disply ();
c3=c1*c2;
cout<<"c1*c2=";
c3.disply ();
c3=c1/c2;
cout<<"c1/c2=";
c3.disply ();
c3=-c1;
cout<<"-c1=";
c3.disply ();
cout<<"两分数比较得:"<<endl;
if (c1 > c2) cout << "c1 > c2" << endl;
if (c1 < c2) cout << "c1 < c2" << endl;
if (c1 == c2) cout << "c1 = c2" << endl;
if (c1 != c2) cout << "c1 ≠ c2" << endl;
if (c1 >= c2) cout << "c1 ≥ c2" << endl;
if (c1 <= c2) cout << "c1 ≤ c2" << endl;
cout<<endl;
system("pause");
}
上机感言:做了前两个任务再做这个感觉容易多了。