首页
社区
课程
招聘
为什么输出是错的!!!!如何修改得到正确的运行结果!!!!急!!!
发表于: 2014-12-21 22:31 3094

为什么输出是错的!!!!如何修改得到正确的运行结果!!!!急!!!

2014-12-21 22:31
3094

//基本要求:
//(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;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 192
活跃值: (45)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
lz 这个问题 去csdn 问 会得到答案
2014-12-22 09:58
0
雪    币: 102
活跃值: (1845)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
哈哈,楼上高级黑
2014-12-22 13:10
0
雪    币: 21
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
1、自己可以单步调试下;
2、哪个地方出错了,可以贴出来讨论讨论
2014-12-22 14:15
0
游客
登录 | 注册 方可回帖
返回
//