萌新求问Visual Studio2010环境下求分数加减运算出现问题。

初学C++不到一年,做了一个结构化的求分数加减法的程序,但是在输出结果时不能做到负数的加减法(可以出现EXE文件但是输入数据后不能出现结果),后来只要是输入数据就不能出现结果了。。。

Fraction.h
class Fraction
{
public:
    Fraction();
    Fraction(int numerator,int denominator);
    friend Fraction operator+(Fraction &fa,Fraction &fb);
    friend Fraction operator-(Fraction &fa,Fraction &fb);
    void display();
private:
    int m_denominator;
    int m_numerator;
    friend int Divisor(int a, int b);
};
Fraction.cpp
#include"fraction.h"
#include<iostream>
using namespace std;
Fraction::Fraction()
{
    m_denominator=1;
    m_numerator=0;
}
Fraction::Fraction(int numerator,int denominator)
    {
        m_denominator=denominator;
        m_numerator=numerator;
        int e=Divisor(m_denominator,m_numerator);
        m_denominator/=e;
        m_numerator/=e;
    }
Fraction operator+(Fraction &fa,Fraction &fb)
{
    Fraction f;
    f.m_denominator=fa.m_denominator*fb.m_denominator;
    f.m_numerator=fa.m_denominator*fb.m_numerator+fa.m_numerator*fb.m_denominator;
    int tmp=Divisor(f.m_denominator,f.m_numerator);
    f.m_denominator/=tmp;
    f.m_numerator/=tmp;
    if(f.m_denominator<0)
    {
        f.m_denominator*=-1;
        f.m_numerator*=-1;
        return f;
    }
    return f;
}
int Divisor(int a, int b)
    {
        if(a<b) return Divisor(b,a);
        if(a%b==0) 
            return b;
        else
            return Divisor(b,a%b);
    }
Fraction operator-(Fraction &fa,Fraction &fb)
{
    Fraction f;
    f.m_denominator=fa.m_denominator*fb.m_denominator;
    f.m_numerator=fa.m_numerator*fb.m_denominator-fa.m_denominator*fb.m_numerator;
    int tmp=Divisor(f.m_denominator,f.m_numerator);
    f.m_denominator/=tmp;
    f.m_numerator/=tmp;
    if(f.m_denominator<0)
    {
        f.m_denominator*=-1;
        f.m_numerator*=-1;
        return f;
    }
    return f;
}
void Fraction::display()
{
    if(m_denominator==1)
        cout<<m_numerator<<endl;
    else cout<<m_numerator<<"/"<<m_denominator<<endl;
}



test.cpp
#include"fraction.h"
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,d;
    cout<<"输入第一个分数的分子:";
    cin>>a;

    cout<<"输入第一个分数的分母:";
    cin>>b;
    if(b==0)
    {
        cout<<"分母不能为0,请重新输入分母:";
        cin>>b;
        cout<<endl;
    }

    cout<<"输入第二个分数的分子:";
    cin>>c;

    cout<<"输入第二个分数的分母:";
    cin>>d;
    if(d==0)
    {
        cout<<"分母不能为0,请重新输入分母:";
        cin>>d;
        cout<<endl;
    }

    if(a==0&&c!=0)
    {
        Fraction f2(c,d);
        cout<<"第一个分数为0"<<endl;
        cout<<"第二个分数为";
        f2.display();
        cout<<endl;
        cout<<"2个分数相加为";
        f2.display();
        cout<<endl;
        cout<<"2个分数相减为"<<"-";
        f2.display();
        cout<<endl;
    }
    if(a!=0&&c==0)
    {
        Fraction f1(a,b);
        cout<<"第一个分数为";
        f1.display();
        cout<<endl;
        cout<<"第二个分数为0"<<endl;
        cout<<"2个分数相加为";
        f1.display();
        cout<<endl;
        cout<<"2个分数相减为";
        f1.display();
        cout<<endl;
    }
    if(a==0&&c==0)
    {
        cout<<"第一个分数为0"<<endl;
        cout<<"第二个分数为0"<<endl;
        cout<<"2个分数相加为0"<<endl;
        cout<<"2个分数相减为0"<<endl;
    }
    else
    {
    Fraction f1(a,b);
    Fraction f2(c,d);
    Fraction f3=f1+f2;
    Fraction f4=f1-f2;
    cout<<"第一个分数为";
    f1.display();
    cout<<"第二个分数为";
    f2.display();
    cout<<"2个分数相加为";
    f3.display();
    cout<<"2个分数相减为";
    f4.display();
    }
}
c++
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问