-
-
[求助]c++实训,如何修改运行结果!!!!急!!!
-
发表于: 2014-12-21 12:24 2674
-
//基本要求:
//(1) 分数的输入。
//(2) 分数的约分。
//(3) 分数的比较、加、减、乘、除。
//(4) 分数的输出。
#include<iostream.h>
#include<fstream>
#include<cmath>
using namespace std;
class fenshu
{
private:
int fenzi; // 分子
int fenmu; // 分母
int gcd(int m, int n);
public: //构造函数及运算符重载的函数声明
fenshu(int z=1,int m=1)
{
if (m==0) return;
fenzi=z,fenmu=m;
}
/irtual~fenshu(){}
void Reduction();
int Getfenz() const{return fenzi;}
int Getfenmu() const{return fenmu;}
void Setfenzi(int z);
void Setfenmu(int 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);
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::fenshu(int z,int m):fenzi(z),fenmu(m)
{
if(fenmu==0) cout<<"分母为0"<<endl;
Reduction();
}
void fenshu::Setfenzi(int z)
{fenzi=z;Reduction();}
void fenshu::Setfenmu(int m)
{
if(m==0) cout<<"分母为0"<<endl;
Reduction();
}
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.simplify();
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.simplify();
return c;
}
fenshu operator *(fenshu c1, fenshu c2) //*
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenzi;
c.fenmu=c1.fenmu*c2.fenmu;
c.simplify();
return c;
}
fenshu operator / (fenshu c1, fenshu c2) // /
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenmu;
c.fenmu=c1.fenmu*c2.fenzi;
c.simplify();
return c;
}
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);
a.Reduction();
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 infile("output.txt",ios::app);
if(!outfile)
{
cout<<"打开文件output.txt失败"<<endl;
exit(1);
}
ofstream outfile("output.txt",ios::app);
}
out<<"------本次计算结果如下-----"<<endl;
for(;;)
{
infile>>obj1;
infile>>index;
infile>>obj2;
infile>>ch;
if(index=='+') result=obj1+obj2;
else if(index=='-') result=obj1-obj2;
else if(index=='*') result=obj1*obj2;
else if(index=='/') result=obj1/obj2;
else
{
system("PAUSE");
return 0;
}
cout<<"正在计算"<<obj1<<" "<<index<<" "<<obj2<<"..."<<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;
if(ch==';') continue;
else if(ch=='=')
{
outfile<<"---谢谢使用----"<<endl;
break;
}
outfile.close();
}
infile.close();
return 0;
}
//(1) 分数的输入。
//(2) 分数的约分。
//(3) 分数的比较、加、减、乘、除。
//(4) 分数的输出。
#include<iostream.h>
#include<fstream>
#include<cmath>
using namespace std;
class fenshu
{
private:
int fenzi; // 分子
int fenmu; // 分母
int gcd(int m, int n);
public: //构造函数及运算符重载的函数声明
fenshu(int z=1,int m=1)
{
if (m==0) return;
fenzi=z,fenmu=m;
}
/irtual~fenshu(){}
void Reduction();
int Getfenz() const{return fenzi;}
int Getfenmu() const{return fenmu;}
void Setfenzi(int z);
void Setfenmu(int 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);
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::fenshu(int z,int m):fenzi(z),fenmu(m)
{
if(fenmu==0) cout<<"分母为0"<<endl;
Reduction();
}
void fenshu::Setfenzi(int z)
{fenzi=z;Reduction();}
void fenshu::Setfenmu(int m)
{
if(m==0) cout<<"分母为0"<<endl;
Reduction();
}
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.simplify();
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.simplify();
return c;
}
fenshu operator *(fenshu c1, fenshu c2) //*
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenzi;
c.fenmu=c1.fenmu*c2.fenmu;
c.simplify();
return c;
}
fenshu operator / (fenshu c1, fenshu c2) // /
{
fenshu c;
c.fenzi=c1.fenzi*c2.fenmu;
c.fenmu=c1.fenmu*c2.fenzi;
c.simplify();
return c;
}
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);
a.Reduction();
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 infile("output.txt",ios::app);
if(!outfile)
{
cout<<"打开文件output.txt失败"<<endl;
exit(1);
}
ofstream outfile("output.txt",ios::app);
}
out<<"------本次计算结果如下-----"<<endl;
for(;;)
{
infile>>obj1;
infile>>index;
infile>>obj2;
infile>>ch;
if(index=='+') result=obj1+obj2;
else if(index=='-') result=obj1-obj2;
else if(index=='*') result=obj1*obj2;
else if(index=='/') result=obj1/obj2;
else
{
system("PAUSE");
return 0;
}
cout<<"正在计算"<<obj1<<" "<<index<<" "<<obj2<<"..."<<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;
if(ch==';') continue;
else if(ch=='=')
{
outfile<<"---谢谢使用----"<<endl;
break;
}
outfile.close();
}
infile.close();
return 0;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
赞赏
他的文章
看原图
赞赏
雪币:
留言: