Devam Ediyor

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 (<<) and stream extraction (>>) 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 <= 1/2 according to the overloaded > operator

1/3 < 1/2 according to the overloaded >= operator

1/3 < 1/2 according to the overloaded < operator

1/3 <= 1/2 according to the overloaded <= 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 << "Enter a Rational Number (n/d): ";

cin >> c;

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

cin >> d;

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

cout << c << " + " << d << " = " << x << endl;

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

cout << c << " - " << d << " = " << x << endl;

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

cout << c << " * " << d << " = " << x << endl;

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

cout << c << " / " << d << " = " << x << endl;

// test overloaded > operator

cout << c << ( ( c > d ) ? " > " : " <= " ) << d

<< " according to the overloaded > operator\n";

// test overloaded >= operator

cout << c << ( ( c >= d ) ? " >= " : " < " ) << d

<< " according to the overloaded >= operator\n";

// test overloaded < operator

cout << c << ( ( c < d ) ? " < " : " >= " ) << d

<< " according to the overloaded < operator\n";

// test overloaded <= operator

cout << c << ( ( c <= d ) ? " <= " : " > " ) << d

<< " according to the overloaded <= operator\n";

// test overloaded == operator

cout << c << ( ( c == d ) ? " == " : " != " ) << d

<< " according to the overloaded == operator\n";

// test overloaded != operator

cout << c << ( ( c != d ) ? " != " : " == " ) << d

<< " according to the overloaded != operator\n";

return 0;

} // end main

Beceriler: Her şey Kabul, C Programlama

Daha fazlasını görün: stream programming, programming number, number programming, programming operators, programming operator, form multiplication, following number, endl, overloaded operator, multiplication program, overload addition, multiplication, program fraction reduced, program addition fraction, rational number program, rational number , fractions, fraction, enter number, driver sample, multiplication form

İş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