0

I am trying to write a program that does operations with rational numbers. My program runs and is able to add, subtract, multiply and divide. However, it does nto reduce the fraction and when printing in decimal form, it returns an int rather than a double. For example, it would print 7/4 as 1 rather than 1.75. Here is the code I have. The driver.cpp is a test code taht was given to me to test my program so I should not have to change the code in taht file.

RATIONAL.H FILE:

```
#ifndef RATIONAL_H
#define RATIONAL_H
#include <cstdlib>
#include <iostream>
using namespace std;
class Rational
{
public:
Rational::Rational(int numerator=0, int denominator=1);
Rational addition (const Rational&);
Rational subtraction (const Rational&);
Rational multiplication (const Rational&);
Rational division (const Rational&);
Rational printRational ();
Rational printRationalAsFloating();
private:
int num;
int den;
void reduction();
};
#endif // #ifndef DATE_H
```

RATIONAL.CPP FILE:

```
#include "Rational.h"
Rational::Rational(int numerator, int denominator) : num(numerator), den(denominator)
{
}
void Rational::reduction()
{
int smallest;
int gcd;
Rational t;
if (num <den)
smallest=num;
else
smallest=den;
for (int i=2; i<smallest;i++)
{
if (num%i==0&&den%i==0)
gcd=i;
if (gcd!=0)
{
t.num=num/gcd;
t.den=den/gcd;
}
}
}
Rational Rational::addition(const Rational&a)
{
Rational t;
t.num=a.num*den+a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}
Rational Rational::subtraction (const Rational&a)
{
Rational t;
t.num=a.den*num-a.num*den;
t.den=a.den*den;
t.reduction();
return t;
}
Rational Rational::multiplication (const Rational&a)
{
Rational t;
t.num=a.num*num;
t.den=a.den*den;
t.reduction();
return t;
}
Rational Rational::division (const Rational&a)
{
Rational t;
t.num=a.num*den;
t.den=a.den*num;
t.reduction();
return t;
}
Rational Rational::printRational()
{
cout <<num<<"/"<< den ;
}
Rational Rational::printRationalAsFloating()
{
cout << num/den;
}
```

DRIVER.CPP FILE:

```
//
// driver for rational.cpp
//
#include <iostream>
using namespace std;
#include "Rational.h"
int main()
{
// define numerators and denominators for a pair of rational numbers
int num1 = 7;
int den1 = 4;
int num2 = 3;
int den2 = 7;
// create rational numbers
Rational c( num1, den1 );
Rational d( num2, den2 );
// store result of calculation in object x
Rational x;
// print default object
cout << "By default, x is: ";
x.printRational();
cout << endl << endl;
c.printRational();
cout << " + ";
d.printRational();
x = c.addition( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit addition result: "
<< ( ( double(num1)/den1 ) + ( double(num2)/den2 ) )
<< endl << endl;
c.printRational();
cout << " - ";
d.printRational();
x = c.subtraction( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit subtraction result: "
<< ( ( double(num1)/den1 ) - ( double(num2)/den2 ) )
<< endl << endl;
c.printRational();
cout << " x ";
d.printRational();
x = c.multiplication( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit multiplication result: "
<< ( ( double(num1)/den1 ) * ( double(num2)/den2 ) )
<< endl << endl;
c.printRational();
cout << " / ";
d.printRational();
x = c.division( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit division result: "
<< ( ( double(num1)/den1 ) / ( double(num2)/den2 ) )
<< endl << endl;
getchar();
return 0;
}
```

OUTPUT WHEN PROGRAM IS RUN:

```
By default, x is: 0/1
7/4 + 3/7 = 61/28
61/28 = 2
Explicit addition result: 2.17857
7/4 - 3/7 = 37/28
37/28 = 1
Explicit subtraction result: 1.32143
7/4 x 3/7 = 21/28
21/28 = 0
Explicit multiplication result: 0.75
7/4 / 3/7 = 12/49
12/49 = 0
Explicit division result: 4.08333
```

*Edited 4 Years Ago by mike_2000_17*: Fixed formatting