//基本要求:
//(1) 分数的输入。
//(2) 分数的约分。
//(3) 分数的比较、加、减、乘、除。
//(4) 分数的输出。
#include<iostream.h>
#include <stdlib.h>
#include<fstream.h>
//using namespace std;
class fenshu
{
private:
int fenzi; // 分子
int fenmu; // 分母
public: //构造函数及运算符重载的函数声明
fenshu(int z=1,int m=1)
{
if (m==0) return;
fenzi=z,fenmu=m;
}
void Reduction();
int gcd(int m, int n);
int Getfenzi() const{return fenzi;}
int Getfenmu() const{return fenmu;}
void Setfenzi(int z){ fenzi=z;}
void Setfenmu(int m){ fenmu=m;}
friend fenshu operator + (fenshu &c1, fenshu &c2);
friend fenshu operator - (fenshu &c1, fenshu &c2);
friend fenshu operator * (fenshu &c1, fenshu &c2);
friend fenshu operator / (fenshu &c1, fenshu &c2);
bool operator > (fenshu &c);
bool operator < (fenshu &c);
bool operator == (fenshu &c);
void simplify();
void display();
};
istream &operator >>(istream &in,const fenshu &a);
ostream &operator <<(ostream &out,const fenshu &a);
int fenshu::gcd(int m,int n)
{
if(n==0)
return m;
else return gcd(n,m%n);
}
void fenshu::Reduction()
{
if(fenmu<0)
{
fenzi=-fenzi;
fenmu=-fenmu;
}
int n=gcd(fenzi,fenmu);
fenzi/=n; // 化简
fenmu/=n;
}
fenshu operator +(fenshu &c1, fenshu &c2) //+
{
fenshu c;
c.fenmu=c1.fenmu*c2.fenmu;
c.fenzi=c1.fenzi*c2.fenmu+c2.fenzi*c1.fenmu;
c.Reduction();
return c;
}
fenshu operator -(fenshu &c1, fenshu &c2) //-
{
fenshu c;
c.fenmu=c1.fenmu*c2.fenmu;
c.fenzi=c1.fenzi*c2.fenmu-c2.fenzi*c1.fenmu;
c.Reduction();
return c;
}
fenshu operator *(fenshu &c1, fenshu &c2) //*
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenzi;
c.fenmu=c1.fenmu*c2.fenmu;
c.Reduction();
return c;
}
fenshu operator / (fenshu &c1, fenshu &c2) // /
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenmu;
c.fenmu=c1.fenmu*c2.fenzi;
c.Reduction();
return c;
}
bool fenshu::operator >(fenshu &c) // >
{
fenshu cf(*this / c);
if (cf.fenzi/cf.fenmu> 1)
return true;
else
return false;
}
bool fenshu::operator <(fenshu &c) // <
{
fenshu cf(*this / c);
if (cf.fenzi/cf.fenmu< 1)
return true;
else
return false;
}
bool fenshu::operator ==(fenshu &c) // ==
{
fenshu cf(*this / c);
if (cf.fenzi/cf.fenmu==1)
return true;
else
return false;
}
ostream &operator <<(ostream &out,const fenshu &a)
{
out<<a.Getfenzi()<<"/"<<a.Getfenmu();
return out;
}
istream &operator >> (istream &in,fenshu &a)
{
char ch;
int m,n;
in>>m;
in>>ch;
if(ch !='/') cout<<"非法字符"<<endl;
in>>n;
if(n==0) cout<<"分母为0"<<endl;
a.Setfenzi(m);
a.Setfenmu(n);
return in;
}
int main()
{
fenshu obj1,obj2,result;
char index,ch;
ifstream infile("input.txt",ios::in);
if(!infile)
{
cout<<"打开文件input.txt失败!"<<endl;
exit(1);
}
ofstream outfile("output.txt",ios::app);
if(!outfile)
{
cout<<"打开文件output.txt失败"<<endl;
return 0;
}
cout<<"------本次计算结果如下-----"<<endl;
while(1)
{
cout<<"请输入您要计算的的式子:"<<endl;
cin>>ch;
if(ch=='N' || ch=='n')
break;
cin>>obj1;
cin>>index;
cin>>obj2;
if(index=='+') result=obj1+obj2;
else if(index=='-') result=obj1-obj2;
else if(index=='*') result=obj1*obj2;
else if(index=='/') result=obj1/obj2;
if(obj1>obj2)
cout<<"obj1>obj2"<<endl;
if(obj2<obj1)
cout<<"obj2<obj1"<<endl;
if(obj1==obj2)
cout<<"obj1==obj2"<<endl;
else
{
system("pause");
return 0;
}
cout<<"是否还要继续(是Y,否N):"<<endl;
cout<<"正在计算"<<obj1<<" "<<index<<" "<<obj2<<"="<<result<<endl;
ofstream outfile("output.txt",ios::app);
if(!outfile)
{
cout<<"打开文件output.txt失败"<<endl;
exit(1);
}
outfile<<obj1<<" "<<index<<" "<<obj2<<" "<<"="<<" "<<result<<endl;
cout<<"计算完成,结果保存在output.txt中"<<endl;
outfile<<"---谢谢使用----"<<endl;
break;
}
outfile.close();
return 0;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!