「已注销」 2021-05-23 11:46 采纳率: 100%
浏览 75
已结题

分数类的设计与实现。。

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

  • 写回答

3条回答 默认 最新

  • benbenli 2021-05-23 13:49
    关注
    
    #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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月8日

悬赏问题

  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
  • ¥15 这怎么做,怎么在我的思路下改下我这写的不对
  • ¥50 数据库开发问题求解答
  • ¥15 安装anaconda时报错
  • ¥20 如何用Python处理单元格内连续出现的重复词语?
  • ¥15 小程序有个导出到插件方式,我是在分包下引入的插件,这个export的路径对吗,我看官方文档上写的是相对路径
  • ¥20 希望有人能帮我完成这个设计( *ˊᵕˋ)
  • ¥100 将Intptr传入SetHdevmode()将Intptr传入后转换为DEVMODE的值与外部代码不一致
  • ¥50 基于ERA5数据计算VPD