.11 Sprint 4:01 PM instructure-uploads.s3.amazonaws.com C Your first rational nu
ID: 3605426 • Letter: #
Question
.11 Sprint 4:01 PM instructure-uploads.s3.amazonaws.com C Your first rational number is: 3/2 which is 1.5 ur second number is 5/2 which is 25, 2 of 2 The sum of the numbers is 4/l or 4.00 output the rest of the result the same way. Note that in the above example 3/2 5/2 8/2, your class stores the fraction in the reduced form, 4/1 4.00 is the floating point version of the same number Allew the user to continue the process (use a loop) Here is some code examples to get you started Function definition passing a Rational object to the member function of another Rational Rational multiply (Rational num) Rational temp: //creating a temporary object temp.numerator numerator num.numerator temp. denominator denominator * num denominator temp.simplify /calling the helper function to reduce the fraction return temp: In your main functions Rational num, num2, /lget values from the user for num1 and num //some other codes result numi add(um2): //some ether codes here result printRational ): result printFloating ) your progrom with several test cases As usual, add all necessary comments. Run Copture the screen and save it. Remember, testing is os important as writing codes. In foct, the software industry spends a lot more money and time on testing and maintenance than writing the original codes.Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
class Rational
{
private:
int numerator;
int denominator;
int gcd (int x, int y) { //function to find greatest common divisor
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
public:
Rational() //default constructor
{
numerator = 0;
denominator = 1;
}
Rational(int num) // constructor with numerator only
{
numerator = num;
denominator = 1;
}
Rational(int num,int den) // constructor with numerator and denominator
{
numerator=num;
denominator=den;
simplify();
}
void simplify() {
int factor;
factor = gcd(numerator, denominator); //using gcd() to simplify Rational number
numerator = numerator / factor;
denominator = denominator / factor;
}
Rational add(Rational num);
void printRational();
void printFloating();
void setNumerator(int num);
void setDenominator(int denom);
};
void Rational::setNumerator(int num) //add two rational numbers
{
numerator = num;
}
//set methods
void Rational::setDenominator(int denom)
{
denominator = denom;
}
Rational Rational::add(Rational num)
{
Rational temp;
temp.numerator = numerator * num.denominator + denominator * num.numerator ;
temp.denominator = denominator * num.denominator;
temp.simplify();
return temp;
}
void Rational::printRational()
{
cout<<numerator<<"/"<<denominator;
}
void Rational::printFloating()
{
cout<<(double)numerator /denominator;
}
int main() {
Rational num1,num2,result;
int numerator,denominator;
cout<<"Enter numerator and denominator of num1 : ";
cin>>numerator>>denominator;
num1.setNumerator(numerator);
num1.setDenominator(denominator);
cout<<" Your first rational number is ";
num1.printRational();
cout<<" which is ";
num1.printFloating();
cout<<" Enter numerator and denominator of num2 : ";
cin>>numerator>>denominator;
num2.setNumerator(numerator);
num2.setDenominator(denominator);
cout<<" Your second rational number is ";
num2.printRational();
cout<<" which is ";
num2.printFloating();
result = num1.add(num2);
cout<<fixed<<setprecision(2); //use fixed format and precision of 2 decimal places using iomanip functions
cout<<" Sum of num1 and num2 : ";
result.printRational();
cout<<" or ";
result.printFloating();
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.