Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a class called Rational for performing arithmetic with fractions. write a

ID: 3633971 • 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 initializes are provided and should store the fraction in reduced form
provide public member functions for each of the following

addition of two rational members. the result should be stored in reduced form
subtraction of two rational members. the result should be stored in reduced form
multiplication of two rational members. the result should be stored in reduced form
division of two rational members. the result should be stored in reduced form
printing rational numbers in the form a/b where a is the numerator and b is the denominator
printing rational numbers in floating point format

output:
1/3 + 7/8 = 29/24
29/24 = 1.20833
etc.



this is what i tried doing but after that i blanked out and i dont even know if its right


#include <iostream>
using namespace std;
class Rational
{
private:
int numerator;
int denominator;
public:
Rational(int = 0, int = 1);
int Add(int, int);
int Subtract(int, int);
int Divide(int, int);
int Mutiply(int, int);
void printnums(int, int );
};

Rational::Rational()
{

}

void main ()
{

}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
class Rational
{public:
void Inputc();
void Outputc();
Rational operator+(Rational x);
Rational operator-(Rational x);
Rational operator*(Rational x);
Rational operator/(Rational x);
int Rational:: gcd(Rational x);
void Rational::reduce(Rational& x);
private:
int num,den;
};
Rational Rational::operator + (Rational x)
{Rational temp;
temp.num=num+x.num;
temp.den=x.den;
cout<<temp.num<<" "<<temp.den<<"before"<<endl;
reduce(temp);
cout<<temp.num<<" "<<temp.den<<"after"<<endl;
return(temp);}

Rational Rational::operator - (Rational x)
{Rational temp;
temp.num=num-x.num;
temp.den=x.den;
cout<<temp.num<<" "<<temp.den<<endl;
reduce(temp);
cout<<temp.num<<" "<<temp.den<<endl;
return(temp);}

Rational Rational::operator *(Rational x)
{Rational temp;
temp.num=num*x.num;
temp.den=den*x.den;
reduce(temp);
return(temp);}

Rational Rational::operator / (Rational x)
{Rational temp;
temp.num=num*x.den;
temp.den=den*x.num;
reduce(temp);
return(temp);}

void Rational::Inputc(void)
{char i,slash,trash;
do{
   cin>>num>>slash>>den;
   if(den==0)
      cout<<"Illegal denominator - retry ";
   }while(den==0);
trash=getchar();
}
void Rational::reduce(Rational &number)
{int denom,fact;
denom=gcd(number);
cout<<number.num<<" "<<number.den<<" "<<denom<<endl;
fact=number.num/denom;
number.num*=fact;
fact=number.den/denom;
number.den*=fact;
cout<<number.num<<" "<<number.den<<" "<<denom<<endl;
if(((number.num>0)&&(number.den<0))||((number.num<0)&&(number.den<0)))
    {number.num*=-1;
     number.num*=-1;
     }
return;
}

int Rational::gcd(Rational number)
{int a=number.num;
int b=number.den;
if(a<0)
    a=-a;
if(b<0)
    b=-b;
     while (a != b)
     {
          if (a > b)
               a -= b;
          else
               b -= a;       
     }
return a;
}       

void Rational::Outputc(void)
{cout<<num<<"/"<<den<<endl;}

int main()
{char choice,trash;
Rational a,b,c;
printf("enter a rational number pair such as 1/2: ");
a.Inputc();
printf("enter a Rational number pair such as 1/2: ");
b.Inputc();
printf("enter choice: (a)dd, (s)ub,(m)ult, (d)ivide:");

choice=getchar();
trash=getchar();
switch (choice)
{case'a':c=a+b;
            break;
case's': c=a-b;
         break;
case'm':c=a*b;
         break;
case'd':c=a/b;
         break;        
default: printf("error");}
//cout<<c;
c.Outputc();
system("pause");
return 0;
}