Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks: (a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d)Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal. 用c++5.11寫出,且使用using namespace std;

时间: 2024-02-28 17:54:05 浏览: 22
以下是符合要求的C++ 代码: ``` #include <iostream> using namespace std; class Rational { private: int numerator, denominator; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public: Rational(int num = 0, int den = 1) { int divisor = gcd(num, den); numerator = num / divisor; denominator = den / divisor; } Rational operator- (Rational other) { int num = numerator * other.denominator - other.numerator * denominator; int den = denominator * other.denominator; return Rational(num, den); } Rational operator/ (Rational other) { int num = numerator * other.denominator; int den = denominator * other.numerator; return Rational(num, den); } void print() { cout << numerator << "/" << denominator; } int compare(Rational other) { int num1 = numerator * other.denominator; int num2 = other.numerator * denominator; if (num1 < num2) return 1; if (num1 > num2) return 2; return 0; } }; int main() { Rational r1, r2; cout << "Enter the first rational number (numerator denominator): "; cin >> r1; cout << "Enter the second rational number (numerator denominator): "; cin >> r2; Rational diff = r1 - r2; cout << "The difference is "; diff.print(); cout << endl; Rational quotient = r1 / r2; cout << "The quotient is "; quotient.print(); cout << endl; int result = r1.compare(r2); if (result == 1) { cout << "The first number is smaller." << endl; } else if (result == 2) { cout << "The second number is smaller." << endl; } else { cout << "The two numbers are equal." << endl; } return 0; } ``` 请注意,此代码使用了重载运算符-和/来实现两个Rational数的减法和除法。此外,它还定义了一个比较函数来比较两个Rational数的大小。在主函数中,我们首先提示用户输入两个Rational数,然后执行所需的操作并输出结果。

相关推荐

最新推荐

recommend-type

三大UML建模工具Visio、Rational Rose、PowerDesign的区别比较

UML建模工具相信大家有所了解,那么你对UML建模工具Visio 、Rational Rose、PowerDesign之间的区别和联系是否了解,这里就像大家简单介绍一下
recommend-type

5分钟装好Rational Rose(画图神器)

1.下载这个https://pan.baidu.com/s/1PDzra4d6hdPcAt-YEBuo6w文件,另外一个下载download快一些,里面第一个是安装包,第二个是打开这个安装包内镜像的工具。 2.打开下载第二个压缩包的工具, ...
recommend-type

详解IBM Rational Functional Tester 的工作原理.docx

Rational Functional Tester 可以操控被测控件、完成用户指定的自动测试动 作,但前提是它需要具备与被测应用程序(Application Under Test,AUT)进行通讯的能力。要做到这一点,Rational Functional Tester 首先必须...
recommend-type

利用Rational_Rose进行C++代码和数据库结构分析.doc

利用Rational_Rose进行C++代码和数据库结构分析.doc 利用Rational_Rose进行C++代码和数据库结构分析.doc 利用Rational_Rose进行C++代码和数据库结构分析.doc
recommend-type

jenkins中文使用手册.doc

常用的版本控制软件有 IBM Rational ClearCase、CVS、Subversion 等; 2)开发人员必须及时向版本控制库中提交代码,也必须经常性地从版本控制库中更新代码到本地; 3)需要有专门的集成服务器来执行集成构建。...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解答下列问题:S—>S;T|T;T—>a 构造任意项目集规范族,构造LR(0)分析表,并分析a;a

对于这个文法,我们可以构造以下项目集规范族: I0: S -> .S S -> .T T -> .a I1: S -> S. [$ T -> T. [$ I2: S -> T. I3: S -> S.;S S -> S.;T T -> T.;a 其中,点(.)表示已经被扫描过的符号,;$表示输入串的结束符号。 根据项目集规范族,我们可以构造出LR(0)分析表: 状态 | a | $ ---- | - | - I0 | s3| I1 | |acc I2 | | 其中s3表示移进到状态3,acc表示接受。在分析字符串a;a时,我们可以按照以下步骤进行
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。