C++ ONLY I NEED THE CODE PLEASE Create a program to handle Rational numbers usin
ID: 3731527 • Letter: C
Question
C++ ONLY I NEED THE CODE PLEASE
Create a program to handle Rational numbers using functions. A Rational number has 2 parts, an int numerator and an int denominator. Output data should print as: numerator/denominator. Input data can be read in from a file as two integer values separated by a space. Code 5 functions to combine two Rational numbers in addition, subtraction, multiplication, division and negation. Code 6 functions to test if two Rational numbers are equal, not equal, less than, less than or equal, greater than, and greater than or equal. Code a main method to read in the two sets of data and test test all methods – follow the output format given below. Reducing values to lowest terms is extra credit. Run 3 times with the following data: 2/3 and ½ ½ and 16/32 3/-5 and 2/1. Sample Format for Output on the Screen: Rational Number 1 _________ reduced ________ Rational Number 2 _________ reduced ________ R1 + R2 = ________ reduced ________ R1 - R2 = ________ reduced _____ R1 * R2 = ________ reduced ________ R1 / R2 = ________ reduced ________ (-) R1 = _________ reduced ________ R1 == R2 _________ (true or false) R1 != R2 _________ (true or false) R1 < R2 _________ (true or false) R1 <= R2 _________ (true or false) R1 > R2 _________ (true or false) R1 >= R2 _________ (true or false) Logic for Rational Operations: a/b + c/d = (a*d + b*c) / (b*d) a/b – c/d = (a*d –b*c) / (b*d) (a/b) * (c/d) = (a*c) / (b*d) (a/b) / (c/d) = (a*d) / (c*b) (a/b) = (-a/b) (a/b) = = (c/d) means (a*d) = = (c*b) (a/b) != (c/d) means (a*d) != (c*b) (a/b) < (c/d) means (a*d) < (c*b) (a/b) < = (c/d) means (a*d) < = (c*b) (a/b) > (c/d) means (a*d) > (c*b) (a/b) >= (c/d) means (a*d) >= (c*b) Turn in the project assignment, the algorithm, the source code, the output and the Academic Honesty Promise, in that order, in a folder with clamps. Put the proper identification on the front of the folder.
Explanation / Answer
#include<iostream>
using namespace std;
int gcd(int,int);
void reduce(int,int);
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
void div(int,int,int,int);
void neg(int,int);
void eq(int,int,int,int);
void notequal(int,int,int,int);
void greaterthan(int,int,int,int);
void greaterthan_orequal(int,int,int,int);
void lessthan(int,int,int,int);
void lessthan_orequal(int,int,int,int);
int gcd(int x,int y)
{
int A = x;
int B = y;
int Temp;
while(B)
{
Temp = B;
B = A%B;
A = Temp;
}
return A;
}
void reduce(int n,int d)
{
int GCDNumber = gcd(n,d);
if(GCDNumber != 0)
{
n = n/GCDNumber;
d = d/GCDNumber;
}
cout<<" Reduced"<<n<<"/"<<d<<" ";
}
void add(int a,int b,int c,int d)
{ int x,y;
x=(a*d + b*c);
y=(b*d);
cout<<"R1+R2="<<x<<"/"<<y<<" ";
reduce(x,y);
}
void sub(int a,int b,int c,int d)
{ int x,y;
x=(a*d - b*c);
y=(b*d);
cout<<"R1-R2="<<x<<"/"<<y<<" ";
reduce(x,y);
}
void mul(int a,int b,int c,int d)
{ int x,y;
x=(a*c);
y=(b*d);
cout<<"R1*R2="<<x<<"/"<<y<<" ";
reduce(x,y);
}
void div(int a,int b,int c,int d)
{ int x,y;
x=a*d;
y=(b*c);
cout<<"R1/R2="<<x<<"/"<<y<<" ";
reduce(x,y);
}
void neg(int a,int b)
{
int x,y;
x=a;
y=b;
x*=-1;
cout<<"(-)R1="<<x<<"/"<<y<<" ";
reduce(x,y);
}
void eq(int a,int b, int c,int d)
{
if((a*d)==(c*b))
cout<<"R1==R2 is TRUE ";
else
cout<<"R1==R2 is FALSE ";
}
void notequal(int a,int b, int c,int d)
{ if((a*d)!=(c*b))
cout<<"R1!=R2 is TRUE ";
else
cout<<"R1!=R2 is FALSE ";
}
void greaterthan(int a,int b, int c,int d)
{
if((a*d)>(c*b))
cout<<"R1>R2 is TRUE ";
else
cout<<"R1>R2 is FALSE ";
}
void greaterthan_orequal(int a,int b, int c,int d)
{ if( (a*d)>=(c*b))
cout<<"R1>=R2 is TRUE ";
else
cout<<"R1>=R2 is FALSE ";
}
void lessthan(int a,int b, int c,int d)
{
if ((a*d)<(c*b))
cout<<"R1<R2 is TRUE ";
else
cout<<"R1<R2 is FALSE ";
}
void lessthan_orequal(int a,int b, int c,int d)
{
if((a*d)<=(c*b) )
cout<<"R1<=R2 is TRUE ";
else
cout<<"R1<=R2 is FALSE ";
}
int main()
{
int num1,den1,num2,den2;
cout<<"Enter the first rational number in the form of two integer values ";
cin>>num1>>den1;
cout<<"Enter the second rational number in the form of two integer values ";
cin>>num2>>den2;
cout<<"Rational Number 1: "<<num1<<"/"<<den1;
reduce(num1,den1);
cout<<"Rational Number 2: "<<num2<<"/"<<den2;
reduce(num2,den2);
add(num1,den1,num2,den2);
sub(num1,den1,num2,den2);
mul(num1,den1,num2,den2);
div(num1,den1,num2,den2);
neg(num1,den1);
eq(num1,den1,num2,den2);
notequal(num1,den1,num2,den2);
greaterthan(num1,den1,num2,den2);
greaterthan_orequal(num1,den1,num2,den2);
lessthan(num1,den1,num2,den2);
lessthan_orequal(num1,den1,num2,den2);
return 0;
}
// I followed the given logics for rational operations!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.