Question: Contents ? Introduction GCD class files GCD.h GCD.cpp Rational class files Rational.h - No changes need to be made to this file Rational.cpp - Changes
Contents?
Introduction
GCD class files
GCD.h
GCD.cpp
Rational class files
Rational.h - No changes need to be made to this file
Rational.cpp - Changes need to be made to this file
maya_tolappa_Rational_class_tester.cpp - This file should not be changed
Output from my version of the Lab
Submit Instructions?
Introduction
The program makes use of classes and operator overloading. Problem specifications are as shown below with my changes marked in blue.
| 8. Rational Arithmetic I A rational number is a quotient of two integers. For example, 12/5, 12/4, 3/4, and 4/6 are all rational numbers. A rational number is said to be in reduced form if its denominator is positive and its numerator and denominator have no common divisor other than 1. For example, the reduced forms of the rational numbers given above are 12/5, 3/1, 3/4, and 2/3. Write a class called Rational with a constructor Rational(int, int) that takes two integers, a numerator and a denominator, and stores those two values in reduced form in corresponding private members. The class should have a private member function void reduce() that is used to accomplish the transformation to reduced form. Use the GCD class provided by me to determine GCD of 2 numbers. The class should have an overloaded insertion operator Create functions that return string representations of the class data. |
| 9. Rational Arithmetic II Modify the class Rational of Programming Challenge 8 to add overloaded operators +, ?, *, and / to be used for addition, subtraction, multiplication, and division. Test the class by reading and processing from the keyboard (or from a file) a series of rational expressions such as 2/3+2/ 8 2/3*2/ 8 2 / 3 2/ 8 2/3/ 2/8 To facilitate parsing of the input, you may assume that numbers and arithmetic operators are separated by whitespace. Use my tester program. |
The Rational.h, and maya_tolappa_Rational_class_tester.cpp files are provided to you and should be used unchanged. Partial code for the Rational.cpp file is provided to you and it needs to be completed.
The files can be downloaded by clicking here or from BlackBoard.
There are 2 classes in this project. These are:
Rational. This class is incomplete and needs to be completed by you as part of this assignment
GCD. This class is provided to you in completed form.
Class UML diagrams are as follows. Functions highlighted in yellow need to be completed :
The following website provides useful information on performing mathematical operations on Rational numbers:
https://www.mathsisfun.com/algebra/rational-numbers-operations.html
GCD class files
GCD.h
| 1 2 3 4 5 6 7 8 9 10 11 | #include |
GCD.cpp
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include "GCD.h" GCD::GCD(int _f_num, int _s_num) { first_number = _f_num; second_number = _s_num; } int GCD::getGCD() { int min_value = min(abs(first_number), abs(second_number)); int max_value = max(abs(first_number), abs(second_number)); int gcd_ret_val = 1; for (int i = 2; i |
Rational class files
Rational.h - No changes need to be made to this file
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include |
Rational.cpp - Changes need to be made to this file
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include "Rational.h" #include "GCD.h" Rational::Rational(int _numer, int _denom) { numer = _numer; if (_denom == 0) denom = 1; else denom = _denom; reduce(); } void Rational::reduce() { if (numer == 0) denom = 1; else { GCD mygcd = GCD(numer, denom); int gcd_val = mygcd.getGCD(); numer = numer / gcd_val; denom = denom / gcd_val; } } Rational Rational::operator+(const Rational &other) const { Rational ret_val; ret_val.numer = numer * other.denom + denom * other.numer; ret_val.denom = denom * other.denom; ret_val.reduce(); return ret_val; } Rational Rational::operator-(const Rational &other) const { Rational ret_val; ret_val.reduce() return ret_val; } Rational Rational::operator*(const Rational &other) const { Rational ret_val; return ret_val; } Rational Rational::operator/(const Rational &other) const { Rational ret_val; return ret_val; } string Rational::to_string_format() { return get_fraction_format() + "(" + get_double_format() + ")"; } string Rational::get_double_format() { string ret_val = ""; ostringstream ostr; return ret_val; } string Rational::get_fraction_format() { return to_string(numer) + "/" + to_string(denom); } |
Highlighted functions need to ne completd.
maya_tolappa_Rational_class_tester.cpp - This file should not be changed
Here is the file I used to test the Rational class. It populates variables using random number generation. Deliberately, the seed for the random number generation is not set to time. This way, the set of numbers generated is predictable and makes debugging easier.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include |
Output from my version of the Lab
frac1 : 1/7(0.14)
frac2 : 4/1(4.00)
frac3 details: 1/7 + 4/1 = 29/7(4.14)
frac3 details: 1/7 - 4/1 = -27/7(-3.86)
frac3 details: 1/7 * 4/1 = 4/7(0.57)
frac3 details: 1/7 / 4/1 = 1/28(0.04)
-----------------------------------------------
frac1 : 9/4(2.25)
frac2 : 1/1(1.00)
frac3 details: 9/4 + 1/1 = 13/4(3.25)
frac3 details: 9/4 - 1/1 = 5/4(1.25)
frac3 details: 9/4 * 1/1 = 9/4(2.25)
frac3 details: 9/4 / 1/1 = 9/4(2.25)
-----------------------------------------------
frac1 : 1/2(0.50)
frac2 : 1/1(1.00)
frac3 details: 1/2 + 1/1 = 3/2(1.50)
frac3 details: 1/2 - 1/1 = -1/2(-0.50)
frac3 details: 1/2 * 1/1 = 1/2(0.50)
frac3 details: 1/2 / 1/1 = 1/2(0.50)
-----------------------------------------------
frac1 : 1/7(0.14)
frac2 : 1/1(1.00)
frac3 details: 1/7 + 1/1 = 8/7(1.14)
frac3 details: 1/7 - 1/1 = -6/7(-0.86)
frac3 details: 1/7 * 1/1 = 1/7(0.14)
frac3 details: 1/7 / 1/1 = 1/7(0.14)
-----------------------------------------------
frac1 : 5/2(2.50)
frac2 : 7/6(1.17)
frac3 details: 5/2 + 7/6 = 11/3(3.67)
frac3 details: 5/2 - 7/6 = 4/3(1.33)
frac3 details: 5/2 * 7/6 = 35/12(2.92)
frac3 details: 5/2 / 7/6 = 15/7(2.14)
-----------------------------------------------
Press any key to continue . . .
Submit Instructions
Submit one of the following via BlackBoard's submission tool.
Zipped solution folder created by Visual Studio
2 .h files, 3 .cpp files
Rational Class GCD Class Fields Fields a first_number int asecond_number int Methods a denom int numer int Methods get_double_format0 string GCDint f num, int s num) getGCDO: int a get fraction_format string operator-(const Rational&): Rational operator*(const Rational&) : Rational operator/(const Rational&): Rational operator+(const Rational8 ): Rational Rational(int numer, int_denom) reduce) void to_string format0 string
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
