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

I need to write a Rational number calculator in c++. Part of it is overwriting o

ID: 3863003 • Letter: I

Question

I need to write a Rational number calculator in c++. Part of it is overwriting operators. I dont know how to write the function for "<" and "<=". Its going to give back true or false. Right now i have

bool Rational::operator>(const Rational &r) const
{
int num1 = numerator * r.denominator;
   int num2 = denominator * r.numerator;
   return(num1 > num2);
}

Im not sure if it will work. The Rational class has two variable numerator and denominator. I also have access to functions that use lcm and gcd but not sure if i need them.

Explanation / Answer

NORMAL OPERATIONS

#include <iostream>
#include <cstdlib>
using namespace std;
enum MenuSelection
{
NONE =0,
ADD =1,
SUBTRACT =2,
MULTIPLY =3,
DIVIDE =4,
QUIT =5,
END =6,
};
int menu()
{
int MenuSelection=0;

do {


cout<<" 1) Add"<<endl;
cout<<"2) Subtract"<<endl;
cout<<"3) Multiply"<<endl;
cout<<"4) Divide"<<endl;
cout<<"5) %"<<endl;

cin>> MenuSelection;
if(MenuSelection==QUIT)
{cout<<"You have chosen to exit the program"<<endl;
cout<<"You have answered ";
exit(1);
}

}

while(MenuSelection<= NONE || MenuSelection >= END);
return MenuSelection;
}
void addFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{   
NUM_result= (num1* den2) +(num2*den1);
DEN_result= den1*den2;
}

void subFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = (num1*den2) - (num2*den1);
DEN_result= den1*den2;
}
void multiplyFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = num1*num2;
DEN_result= den1* den2;
}
void divideFractions(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result)
{
NUM_result = num1*den2;
DEN_result = den1*num2;

}
void input_values_FROM_USER(int &num1, int &num2, int &den1, int &den2)

{
cout<<endl;
cout<<"Enter the numerator for first fraction-->";
cin>>num1;
cout<<"Enter denominator for first fraction--> ";
cin>>den1;
cout<<"Enter numerator for second fraction--> ";
cin>>num2;
cout<<"Enter denominator for second fraction--> ";
cin>>den2;
cout<<"------------------------------------------- ";
}
void outputResults(int num1, int num2, int den1, int den2, int &NUM_result, int &DEN_result, int operation)
{
if(operation == ADD)
{
cout<<"The sum of the two is--> "<<num1<<'/'<<den1<<'+'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}
if(operation== SUBTRACT)
{
cout<<"The difference of the two is--> "<<num1<<'/'<<den1<<'- '<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;

}

if(operation== MULTIPLY)
{
cout<<"The product of the two is--> " <<num1<<'/'<<den1<<'*'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}
if(operation== DIVIDE)
{
cout<<"The quotient of the two is--> " <<num1<<'/'<<den1<<'/'<<num2<<'/'<<den2<<'='<<NUM_result<<'/'<<DEN_result<<endl;
}
}
void main()
{ int MenuSelection = NONE;
int num1=0, num2=0, den1=0, den2=0;
int NUM_result=0, DEN_result=0;
cout<<"Author: Jose Soto."<<endl;
cout<<"CSCI 110-- Jose Soto's Project 2 (Basic Fraction Arithmetic) "<<endl;
do
{
MenuSelection= menu();
input_values_FROM_USER(num1,num2,den1,den2);
if(MenuSelection==ADD)
{
addFractions(num1,num2,den1,den2,NUM_result, DEN_result);
}
if(MenuSelection==SUBTRACT)
{
subFractions(num1,num2,den1,den2,NUM_result, DEN_result);

}
if(MenuSelection==MULTIPLY)
{
multiplyFractions(num1,num2,den1,den2,NUM_result,DEN_result);
}
if(MenuSelection==DIVIDE)
{
divideFractions(num1,num2,den1,den2,NUM_result,DEN_result);
}
outputResults(num1,num2,den1,den2,NUM_result,DEN_result,MenuSelection);
}
while(MenuSelection != QUIT);
cin.get();
}

lcm and gcd::

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int gcd(int x, int y)
{
int rem=0,a,b;
a=(x>y)?x:y;
b=(x<y)?x:y;

rem=b;
while(a%b!=0)
{
rem=a%b;
a=b;
b=rem;
}
return rem;
}

int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y;
while (true)
{
if(a%x==0&&a%y==0)
return a;
++a;
}
}

int main(int argc, char **argv)
{
cout << "Enter the two numbers: ";
int x, y;
cin >> x >> y;
cout << "The GCD of two numbers is: " << gcd(x, y) << endl;
cout << "The LCM of two numbers is: " << lcm(x, y) << endl;
return 0;
}