Write a class to represent Rational numbers. What to submit: 3 files: Rational.h
ID: 3694357 • Letter: W
Question
Write a class to represent Rational numbers.
What to submit:
3 files:
Rational.h - class declaration
Rational.cpp – class function definitions
main.cpp - main program that tests the class
Class Rational specifications:
Private data members: 2 integer fields representing the numerator and denominator of the Rational number
Public member functions:
default constructor: Sets the Rational number to 0/1
constructor receiving size: If the denominator is 0, set the Rational number to be 0/1, otherwise, store the 2 parameter values into the numerator and denominator fields and reduce the fraction to its simplest form.
setNumerator: receives a parameter value, stores it to the numerator field, and then reduces the fraction.
setDenominator: receives a parameter value. If the parameter is 0, do nothing. Otherwise, store the parameter value to private data and reduce the fraction.
getNumerator: Return a copy of the numerator field.
getDenominator: Return a copy of the denominator field.
floatVal: Returns the floating point equivalent of the fraction operators:
+, -, *, / : all operate on two Rational numbers and return the calculated reduced result.
>, <, >=, <=, ==, != : all operate on two Rational numbers and return a boolean true or
false value based on the comparison.
++ : both prefix and postfix versions. Adds 1 to the Rational number and returns a
Rational number that is either the original or the
changed version depending on which way it is used.
<< : writes out the Rational number in fraction form (i.e. 3 / 4 )
Private member function:
reduce : reduces the Rational number (numerator and denominator) to the simplest form.
Main program:
Declare at least 2 objects of Rational type. Test all of the class functions and show the testing results in the output console window.
Explanation / Answer
------------------------------------------------
134 135 136 137
#include <iostream> using namespace std; class rational { public: void output(ostream& out); void set(short num, short den); void set(short integer); short get_numerator(); short get_denominator(); rational add(rational number); rational add(short number); rational sub(rational number); rational sub(short number); rational mul(rational number); rational mul(short number); rational div(rational number); rational div(short number); rational neg(); private: short numerator; short denominator; }; int main() { char loop = 'y'; do { rational rational1, rational2; cout << "Input the numerator and denominator: "; short num, den; cin >> num >> den; rational1.set(num, den); cout << "Input the numerator and denominator: "; cin >> num >> den; rational2.set(num, den); cout << "What would you like to do? " << "1) add " << "2) subtract " << "3) multiply "; char choice; cin >> choice; while(choice != '1' && choice != '2' && choice != '3') { cout << "Invalid choice. What would you like to do? " << "1) add " << "2) subtract " << "3) multiply "; cin >> choice; } rational result; if(choice == '1') { result = rational1.add(rational2); cout << "The result is: "; result.output(cout); }/* else if(choice == '2') { result = rational1.sub(rational2); cout << "The result is: "; result.output(cout); } else if(choice == '3') { result = rational1.mul(rational2); cout << "The result is: "; result.output(cout); }*/ cout << "Would you like to run the program again? (y/n)? "; cin >> loop; loop = tolower(loop); while(loop != 'y' && loop != 'n') { cout << "Incorrect output. Would you like to run the program again? "; cin >> loop; } }while(loop == 'y'); cout << "Press a key to exit: "; char exit; cin >> exit; return 0; } void rational::output(ostream& out = cout) { out << numerator << "/" << denominator; } void rational::set( short num, short den) { numerator = num; denominator = den; } void rational::set( short integer) { numerator = integer; denominator = 1; } short rational::get_denominator() { return denominator; } short rational::get_numerator() { return numerator; } rational rational::add(rational number) { numerator = numerator*number.denominator + denominator*number.numerator; denominator = denominator*number.denominator; } rational rational::add(short integer) { numerator = numerator + integer*denominator; rational result; result.set(numerator, denominator); return result; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.