rendered paste body
#include <iostream>
#include <cmath>
using namespace std;
class Rational {
public:
Rational (int n, int d);
Rational ();
void input(istream& in);
void output(ostream& out);
bool equal (Rational ratio2);
bool less (Rational ratio2);
Rational neg();
Rational add(Rational ratio2);
Rational sub(Rational ratio2);
Rational mul(Rational ratio2);
Rational div(Rational ratio2);
private:
int n;
int d;
};
int main () {
Rational ratio1, ratio2, ratio3;
bool isequal, isless;
cout << "Enter your first rational number, numerator first followed by denominatior:\n";
ratio1.input(cin);
cout << "Enter your second rational number, same method:\n";
ratio2.input(cin);
cout << "Your first ratio is:";
ratio1.output(cout);
cout << endl << endl;
cout << "Your second ratio is:";
ratio2.output(cout);
cout << endl << endl;
cout << "Let's see if the two ratios are equal..." << endl;
isequal = ratio1.equal(ratio2);
if ( isequal == true )
cout << "The fractions are equal.\n";
else
cout << "The fractions are not equal.\n\n";
cout << "How about if the first ratio is smaller than the second one..." << endl;
isless = ratio1.less(ratio2);
if ( isless == true )
cout << "The first fraction is lesser than the second.\n";
else
cout << "The first fraction is greater than the second.\n\n";
cout << "If you were to add the two ratios, (a/b) + (c/d) =\n";
ratio3 = ratio1.add(ratio2);
ratio3.output(cout);
cout << endl;
cout << "If you were to subtract the two ratios, (a/b) - (c/d) =\n";
ratio3 = ratio1.sub(ratio2);
ratio3.output(cout);
cout << endl;
cout << "Multiplying the two ratios, (a/b) * (c/d) =\n";
ratio3 = ratio1.mul(ratio2);
ratio3.output(cout);
cout << endl;
cout << "Dividing the two ratios, (a/b) / (c/d) =\n";
ratio3 = ratio1.div(ratio2);
ratio3.output(cout);
cout << endl;
system("Pause.");
return 0;
}
void Rational::input(istream& in)
{
char slash;
in >> n;
in >> slash;
in >> d;
while ( d <= 0)
{
cout << "Invalid value for a denominator. Please post a positive value." << endl;
cout << "For the denominator ONLY." << endl;
in >> d;
}
}
Rational::Rational()
{
n = 0;
d = 0;
}
Rational::Rational(int a, int b)
{
n = a;
d = b;
}
void Rational::output(ostream& out)
{
out << n << "/" << d;
}
Rational Rational::neg(){
Rational ratio3;
ratio3.n = -(n);
ratio3.d = d;
return ratio3;
}
Rational Rational::add(Rational ratio2){
Rational ratio3;
ratio3.n = (n * ratio2.d) + (d * ratio2.n);
ratio3.d = (d * ratio2.d);
return ratio3;
}
Rational Rational::sub(Rational ratio2){
Rational ratio3;
ratio3.n = (n * ratio2.d) - (d * ratio2.n);
ratio3.d = (d * ratio2.d);
return ratio3;
}
Rational Rational::mul(Rational ratio2){
Rational ratio3;
ratio3.n = (n * ratio2.n);
ratio3.d = (d * ratio2.d);
return ratio3;
}
Rational Rational::div(Rational ratio2){
Rational ratio3;
ratio3.n = (n * ratio2.d);
ratio3.d = (d * ratio2.n);
return ratio3;
}
Rational Rational::equal(Rational ratio2){
bool equal;
if ( n = ratio2.n && d = ratio2.d )
equal = true;
else
equal = false;
return equal;
}
Rational Rational::less(Rational ratio2){
bool less;
if ( ( n * ratio2.d ) < ( d * ratio2.n ) )
less = true;
else
less = false;
return less;
}