建立用于完成分数形式运算的类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
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥35 平滑拟合曲线该如何生成
- ¥100 c语言,请帮蒟蒻写一个题的范例作参考
- ¥15 名为“Product”的列已属于此 DataTable
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 自己瞎改改,结果现在又运行不了了
- ¥15 链式存储应该如何解决
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站