分数类的设计与实现。。

建立用于完成分数形式运算的类RationalNumber。编写一个测试该类的程序。用整数 变量表示类的私有数据(即分子和分母) 。给类提供一个能够对所声明的对象初始化的构造 函数。 为了能够在不提供初始化值的情况下也能对对象初始化, 构造函数中应该包含默认的 值。构造函数还应该以最简分数的形式存储数据,即2/4应该在对象中存储成分子为1、分 母为2的形式。公有成员函数应该有以下功能: (1) 两个有理数相加,以最简形式保存结果; (2) 两个有理数相减,以最简形式保存结果; (3) 两个有理数相乘,以最简形式保存结果; (4) 两个有理数相除,以最简形式保存结果; (5) 以a/b的形式输出有理数(a是分子,b是分母) ; (6) 以浮点形式输出有理数。


#include <iostream>
#include <string>

using namespace std;

class RationalNumber
{
public:
    RationalNumber(int denominator, int numerator)
    {
        if (denominator == 0)
            throw "denominator can not be 0.";
        int g = gcd(abs(denominator), abs(numerator));
        d = denominator / g;
        n = numerator / g;
        
        if (d < 0)
        {
            d = -d;
            n = -n;
        }
    }
    RationalNumber(int numerator): d(1), n(numerator) { }
    RationalNumber(): d(1), n(0) { }
    
    friend RationalNumber operator+(const RationalNumber& a, const RationalNumber& b)
    {
        return RationalNumber(a.d * b.d, a.n * b.d + b.n * a.d);
    }
    friend RationalNumber operator-(const RationalNumber& a, const RationalNumber& b)
    {
        return RationalNumber(a.d * b.d, a.n * b.d - b.n * a.d);
    }
    friend RationalNumber operator*(const RationalNumber& a, const RationalNumber& b)
    {
        return RationalNumber(a.d * b.d, a.n * b.n);
    }
    friend RationalNumber operator/(const RationalNumber& a, const RationalNumber& b)
    {
        return RationalNumber(a.d * b.n, a.n * b.d);
    }
    
    string getFraction()
    {
        return to_string(n) + "/" + to_string(d);
    }
    
    double getDecimal()
    {
        return 1.0 * n / d;
    }
    
private:
    int gcd(int a, int b)
    {
        if (a == 0)
           return b;
        else if (b == 0)
           return a;
        else if (a == b)
            return a;
        else if (a > b)
            return gcd(a-b, b);
        else
            return gcd(a, b-a);
    }
    
    int d;
    int n;
};

int main()
{
    RationalNumber threeQuarters(4, 3);
    cout << "threeQuarters: " << threeQuarters.getFraction() << " or " << threeQuarters.getDecimal() << endl;
    RationalNumber twoSixths(6, 2);
    cout << "twoSixths: " << twoSixths.getFraction() << " or " << twoSixths.getDecimal() << endl;
    
    RationalNumber sum = threeQuarters + twoSixths;
    cout << "sum: " << sum.getFraction() << " or " << sum.getDecimal() << endl;
    
    RationalNumber diff = threeQuarters - twoSixths;
    cout << "diff: " << diff.getFraction() << " or " << diff.getDecimal() << endl;
    RationalNumber diff2 = twoSixths - threeQuarters;
    cout << "diff2: " << diff2.getFraction() << " or " << diff2.getDecimal() << endl;
    
    RationalNumber product = threeQuarters * twoSixths;
    cout << "product: " << product.getFraction() << " or " << product.getDecimal() << endl;
    
    RationalNumber quotient = threeQuarters / twoSixths;
    cout << "quotient: " << quotient.getFraction() << " or " << quotient.getDecimal() << endl;
    
    return 0;
}

// Output:
threeQuarters: 3/4 or 0.75
twoSixths: 1/3 or 0.333333
sum: 13/12 or 1.08333
diff: 5/12 or 0.416667
diff2: -5/12 or -0.416667
product: 1/4 or 0.25
quotient: 9/4 or 2.25

 

问题栏提示:”授人以鱼不如授人以渔“。费了这么多功夫写代码和调试,要个采纳都这么难吗?

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632