Hello everyone, i'm getting very wrong outputs with some of the functions. The getProduct() and getRatio() functions are supposed to reduce the fraction to the lowest terms but they're not doing it right. I Don't know what the problem is. Please any help will be very much appreciated. Thanks.
Here is the code..
//This program performs math operations on two rational numbers
// A class "ratio" is being used
#include <iostream>
using namespace std;
class ratio
{
private:
int a , b;
int c , d;
public:
void setValues(int, int , int , int);
void getSum();
void getProduct();
void getDifference();
void getRatio();
};
int gcd (int x , int y)
{
if (y!=0)
gcd (y , x%y);
return x;
}
void ratio :: setValues(int x, int y , int p, int q)
{
a = x;
b = y;
c = p;
d = q;
}
void ratio :: getSum()
{
int m , n;
m = (a * d) + (c * b) ;
n = (b * d);
cout << m <<"/"<< n << endl;
}
void ratio :: getDifference()
{
int m , n;
m = (a * d) - (c * b);
n = ( b * d);
cout << m <<"/"<< n<< endl;
}
void ratio :: getProduct()
{
int m, n , temp;
m = a * c;
n = b * d;
cout << "Their product is" << m << "/" << n << endl;
cout << "Reducing in lowest terms gives :";
temp = gcd(m , n);
int num , denum;
num = m / temp;
denum = n / temp;
cout << num << "/" << denum << endl;
}
void ratio :: getRatio()
{
int m , n , temp;
m = a * d;
n = b * c;
cout << "Their ratio is " << m << "/" << n << endl;
cout << "The simplified ratio is ";
temp = gcd(m , n);
int num , denum;
num = m / temp;
denum = n / temp;
cout << m << "/" << n << endl;
}
int main()
{
ratio frac;
int h , s, c , t;
cout << "My job is to sum , divide , add and subtract";
cout << "any two rational numbers for you" << endl;
cout << "Enter the numerator for fraction 1"<<endl;
cin >> h;
cout << "Enter its denominator"<<endl;
cin >> s;
cout << "Enter the numerator for fraction 2"<<endl;
cin >> c;
cout << "Enter its denominator"<<endl;
cin >> t;
frac. setValues(h,s,c,t);
cout << "Their sum is " << endl ;
frac.getSum() ;
cout << "Their difference is" << endl;
frac.getDifference();
frac.getProduct();
frac.getRatio();
return 0;
}