# 493255 Rational Number Class

Create a class RationalNumber (fractions) with the following capabilities:

a.) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.

b.) Overload the addition, subtraction, multiplication and division operators for this class.

c.) Overload the relational and equality operators for this class.

In addition to the capabilities outlined above, the RationalNumber class should overload the stream insertion (&lt;&lt;) and stream extraction (&gt;&gt;) operators. The stream extraction operator should prevent a 0 denominator in a fraction, reduce fractions that are not in reduced form, and prevent negative denominators. Negative or 0 denominators should be set to 1.

Sample output 1

Enter a Rational Number (n/d): 1/3

Enter a Rational Number (n/d): 2/4

1/3 + 1/2 = 5/6

1/3 - 1/2 = -1/6

1/3 * 1/2 = 1/6

1/3 / 1/2 = 2/3

1/3 &lt;= 1/2 according to the overloaded &gt; operator

1/3 &lt; 1/2 according to the overloaded &gt;= operator

1/3 &lt; 1/2 according to the overloaded &lt; operator

1/3 &lt;= 1/2 according to the overloaded &lt;= operator

1/3 != 1/2 according to the overloaded == operator

1/3 != 1/2 according to the overloaded != operator

Driver Program:

#include "RationalNumber.h"

int main()

{

// RationalNumber c( 1, 3 ), d( 2, 4 ), x;

RationalNumber c, d, x;

// test overloaded stream extraction operator

cout &lt;&lt; "Enter a Rational Number (n/d): ";

cin &gt;&gt; c;

cout &lt;&lt; "Enter a Rational Number (n/d): ";

cin &gt;&gt; d;

x = c + d; // test overloaded operators + and =

cout &lt;&lt; c &lt;&lt; " + " &lt;&lt; d &lt;&lt; " = " &lt;&lt; x &lt;&lt; endl;

x = c - d; // test overloaded operators - and =

cout &lt;&lt; c &lt;&lt; " - " &lt;&lt; d &lt;&lt; " = " &lt;&lt; x &lt;&lt; endl;

x = c * d; // test overloaded operators * and =

cout &lt;&lt; c &lt;&lt; " * " &lt;&lt; d &lt;&lt; " = " &lt;&lt; x &lt;&lt; endl;

x = c / d; // test overloaded operators / and =

cout &lt;&lt; c &lt;&lt; " / " &lt;&lt; d &lt;&lt; " = " &lt;&lt; x &lt;&lt; endl;

cout &lt;&lt; c &lt;&lt; ( ( c &gt; d ) ? " &gt; " : " &lt;= " ) &lt;&lt; d

&lt;&lt; " according to the overloaded &gt; operator\n";

cout &lt;&lt; c &lt;&lt; ( ( c &gt;= d ) ? " &gt;= " : " &lt; " ) &lt;&lt; d

&lt;&lt; " according to the overloaded &gt;= operator\n";

cout &lt;&lt; c &lt;&lt; ( ( c &lt; d ) ? " &lt; " : " &gt;= " ) &lt;&lt; d

&lt;&lt; " according to the overloaded &lt; operator\n";

cout &lt;&lt; c &lt;&lt; ( ( c &lt;= d ) ? " &lt;= " : " &gt; " ) &lt;&lt; d

&lt;&lt; " according to the overloaded &lt;= operator\n";

cout &lt;&lt; c &lt;&lt; ( ( c == d ) ? " == " : " != " ) &lt;&lt; d

&lt;&lt; " according to the overloaded == operator\n";

cout &lt;&lt; c &lt;&lt; ( ( c != d ) ? " != " : " == " ) &lt;&lt; d

&lt;&lt; " according to the overloaded != operator\n";

return 0;

} // end main

Beceriler: Her şey Kabul, C Programlama

İşveren Hakkında:
( 2 değerlendirme )

Proje NO: #2239169

## Seçilen:

eperfections

This is very easy and simple program. I can do it. Please accept my bid.

1 gün içinde 15\$ USD
(326 Değerlendirme)
7.1