Create a class called Rational for performing arithmetic with fractions. Write a
ID: 3633535 • Letter: C
Question
Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class.Use integer variables to represent the private data of the class- the numerator and the denominator. Provide a constructor function that enables an object of the class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form (i.e the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator ). Provide public member functions for each of the following :
a) Addition of two Rational numbers. The result should be stored in reduced form.
b) Subtraction of two Rational numbers. The result should be stored in reduced form.
c) Multiplication of two Rational numbers. The result should be stored in reduce form.
d) Division of two Rational numbers. The result should be stored in reduced form.
e) Printing Rational numbers in the form a/b where a is the numerator and b is the denominator.
f) Printing Rational in floating point format.
Explanation / Answer
please rate - thanks
#include <iostream>
class fracType
{
public:
fracType(); // default constructor
void setFrac (int newNum,int newDen);
fracType Add (fracType otherFraction) const;
fracType Sub (fracType otherFraction) const;
fracType Mul (fracType otherFraction) const;
fracType Div (fracType otherFraction) const;
void simplify();
void showFrac() const;
void showDec() const;
private:
int numerator;
int denominator;
};
using namespace std;
int main()
{
bool exit=false;
do
{
int num;
int den;
fracType num1;
fracType num2;
fracType result;
int choice;
bool error;
do{
error=false;
cout << " What is the numerator of the first fraction? ";
if(!(cin >> num)) {
cout << "Please enter numeric characters only. " ;
cin.clear();
cin.ignore(10000,' ');
error=true;
}
}while(error);
do{
error=false;
cout << "What is the denominator of the first fraction? ";
if(!(cin >> den)) {
cout << "Please enter numeric characters only. " ;
cin.clear();
cin.ignore(10000,' ');
error=true;
}
}while(error);
num1.setFrac(num, den);
do{
error=false;
cout << " What is the numerator of the second fraction? ";
if(!(cin >> num)) {
cout << "Please enter numeric characters only. " ;
cin.clear();
cin.ignore(10000,' ');
error=true;
}
}while(error);
do{
error=false;
cout << "What is the denominator of the second fraction? ";
if(!(cin >> den)) {
cout << "Please enter numeric characters only. " ;
cin.clear();
cin.ignore(10000,' ');
error=true;
}
}while(error);
num2.setFrac(num, den);
cout << endl;
do{
error=false;
cout << "Enter a number from 1 to 5 "
<< "1. Displays the sum of the two fractions "
<< "2. Displays the difference "
<< "3. Displays the product "
<< "4. Displays the quotient "
<< "5. Exit "
<< " Selection is ";
if(!(cin >> choice)) {
cout << "Please enter numeric characters only. " ;
cin.clear();
cin.ignore(10000,' ');
error=true;
}
}while(error);
cout << endl;
exit=false;
switch (choice)
{
case 1:
cout << endl;
num1.showFrac();
cout << " + ";
num2.showFrac();
cout << " = ";
result = num1.Add(num2);
result.simplify();
result.showFrac();
result.showDec();
cout << endl;
break;
case 2:
num1.showFrac();
cout << " - ";
num2.showFrac();
cout << " = ";
result = num1.Sub(num2);
result.simplify();
result.showFrac();
result.showDec();
cout << endl;
break;
case 3:
num1.showFrac();
cout << " * ";
num2.showFrac();
cout << " = ";
result = num1.Mul(num2);
result.simplify();
result.showFrac();
result.showDec();
cout << endl;
break;
case 4:
num1.showFrac();
cout << " / ";
num2.showFrac();
cout << " = ";
result = num1.Div(num2);
result.simplify();
result.showFrac();
result.showDec();
cout << endl;
break;
case 5:
exit=true;
break;
default: cout << "Not an input between 1 and 7. ";
break;
}
}while (!exit);
system("pause");
}
void fracType::simplify() //Euclids algorithm
{ int a,b;
a=numerator;
b=denominator;
while(a!=b)
{if(a>b)
a -= b;
else
b -= a;
}
numerator/=a;
denominator/=a;
}
fracType::fracType()
{
numerator = 0;
denominator = 0;
}
void fracType::setFrac(int newNum, int newDen)
{
numerator = newNum;
denominator = newDen;
}
fracType fracType::Add(fracType otherFraction) const
{
fracType sum;
sum.numerator = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
sum.denominator = denominator*otherFraction.denominator;
return sum;
}
fracType fracType::Sub(fracType otherFraction) const
{
fracType difference;
difference.numerator = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
difference.denominator = denominator*otherFraction.denominator;
return difference;
}
fracType fracType::Mul(fracType otherFraction) const
{
fracType product;
product.numerator = numerator*otherFraction.numerator;
product.denominator = denominator*otherFraction.denominator;
return product;
}
fracType fracType::Div(fracType otherFraction) const
{
fracType quotient;
quotient.numerator = numerator*otherFraction.denominator;
quotient.denominator = denominator*otherFraction.numerator;
return quotient;
}
void fracType::showFrac() const
{ if(denominator>numerator)
cout << numerator << '/' << denominator;
else
if(numerator%denominator!=0)
cout<<numerator/denominator<<" "<<numerator%denominator<<"/"<<denominator;
else
cout<<numerator/denominator;
}
void fracType::showDec() const
{cout<<" = "<<numerator/(double)denominator;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.