/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:分数运算
* 作者:郭岩岩
* 完成日期:2012年4月10日
* 版本号:vc.1
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
*程序头部的注释结束
*/
#include<iostream>
using namespace std;
int gcd(int,int);
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Set(int nu=0,int de=1); //置值,改变值时用
void input(); //按照"nu/de"的格式,如"5/2"的形式输入
void Simplify(); //化简(使分子分母没有公因子)
CFraction operator+(CFraction &c);
CFraction operator-(CFraction &c);
CFraction operator*(CFraction &c);
CFraction operator/(CFraction &c);
CFraction operator-();
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)
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
exit(0);
}
}
void CFraction::Set(int nu,int de)
{
if(deno!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::input()
{
cout<<nume<<"/"<<deno<<endl;
}
//化简
void CFraction::Simplify()
{
int n=gcd(nume,deno);
nume=nume/n;
deno=deno/n;
}
int gcd(int m,int n)
{
int r;
if (m<n){r=m;m=n;n=r;}
while(r=m%n) // 求m,n的最大公约数(原来求公约数可以这样求啊)
{
m=n;
n=r;
}
return n;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction c2;
c2.nume =nume*c.deno +c.nume*deno ;
c2.deno =deno*c.deno ;
c2.Simplify();
return c2;
}
CFraction CFraction::operator-(CFraction &c)
{
CFraction c2;
c2.nume =nume*c.deno -c.nume*deno ;
c2.deno =deno*c.deno ;
c2.Simplify ();
return c2;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction c2;
c2.nume =nume*c.nume;
c2.deno =deno*c.deno ;
c2.Simplify();
return c2;
}
CFraction CFraction::operator/(CFraction &c)
{
CFraction c2;
c2.nume =nume*c.deno ;
c2.deno =deno*c.nume ;
c2.Simplify();
return c2;
}
CFraction CFraction::operator-()
{
CFraction c2;
c2.nume =0-nume;
c2.deno =deno ;
c2.Simplify();
return c2;
}
bool CFraction::operator>(CFraction &c)
{
if((nume*c.deno -c.nume*deno)>0)
{
return true;
}
else
return false;
}
bool CFraction::operator<(CFraction &c)
{
CFraction c2;
if((nume*c.deno -c.nume*deno)<0)
{
return true;
}
else
return false;
}
bool CFraction::operator==(CFraction &c)
{
CFraction c2;
if((nume*c.deno -c.nume*deno)==0)
{
return true;
}
else
return false;
}
bool CFraction::operator!=(CFraction &c)
{
CFraction c2;
if((nume*c.deno -c.nume*deno)!=0)
{
return true;
}
else
return false;
}
bool CFraction::operator>=(CFraction &c)
{
CFraction c2;
if((nume*c.deno -c.nume*deno)>=0)
{
return true;
}
else
return false;
}
bool CFraction::operator<=(CFraction &c)
{
CFraction c2;
if((nume*c.deno -c.nume*deno)<=0)
{
return true;
}
else
return false;
}
int main()
{
CFraction cf1,cf2,cf3;
cf1.Set(24,72);
cf1.input();
cf2.Set (8,2);
cf2.input ();
cf3=cf1+cf2;
cout<<"cf1+cf2=";
cf3.input ();
cf3=cf1-cf2;
cout<<"cf1-cf2=";
cf3.input ();
cf3=cf1*cf2;
cout<<"cf1*cf2=";
cf3.input ();
cf3=cf1/cf2;
cout<<"cf1/cf2=";
cf3.input ();
cf3=-cf1;
cout<<"-cf1=";
cf3.input();
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;
}
if(cf1<=cf2)
{
cout<<"cf1<=cf2"<<endl;
}
system("pause");
return 0;
}
为什么在比较大小的时候不能用两个分数直接做差判断是否大于零啊????
上机感言: