I am a fairly new programmer with limited knowledge in C++. I am currently writing a program for my class that involves fractions. I was doing good writing one piece at a time and making it work until the instructor threw a wrench into my plans and asked me to output in both fraction form (I.E: a/b) and numeric form (I.E: 111.23). This is a homework assignment and it is already late. I hate turning in late assignments but, I hate even worse not being able to complete the assignment. It will probably be the death of me someday. Please help me solve this problem. The following is what I have so far.
#include <iostream>
#include <iomanip>
using namespace ::std; //allows user to keep from entering "std::" in front of every cin,cout, or endl
/**********************************************************************/
class rational //Begin Rational Class
{
private:
int num;
int denom;
public:
rational::rational( int n = 0 , int d = 0 ) : num( n ), denom( d ) //Begin Constructor
{
for ( int x = 2; x < num; x++ )
{
if ( ((num%x) == 0 ) && ((denom%x) == 0) ) // Greatest Common Denominator, Used to Simplify fractions
{
num /= x;
denom /= x;
x--;
}
}
} //end of Constructor
/**********************************************************************/
rational rational::operator+ ( const rational &X ) // Completes the addition of the fractions
{
int newnum = (X.denom * num) + (denom * X.num);
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/
rational rational::operator- ( const rational &X ) const // Completes the subtraction of the fractions
{
int newnum = ( num * X.denom ) - ( X.num * denom );
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/
rational rational::operator* ( const rational &X ) const // Completes the multiplication of the fractions
{
int newnum = X.num * num;
int newdenom = X.denom * denom;
return rational( newnum , newdenom );
}
/**********************************************************************/
rational rational::operator/ ( const rational &X ) const // Completes the division of the fractions
{
int newnum = num * X.denom;
int newdenom = X.num * denom;
return rational( newnum , newdenom );
}
friend ostream &operator << ( ostream&, const rational& );
friend istream &operator >> ( istream&, rational& );
}; // End of Rational Class
/**********************************************************************/
istream &operator >> ( istream &in, rational &X ) //Input, ignores the / of the fraction when input
{
in >> X.num;
in.ignore();
in >> X.denom;
return in;
}
/**********************************************************************/
ostream &operator << ( ostream &out , const rational &X ) //Output format a/b
{
//out << static_cast< double >( X.num ) / X.denom ;
out << X.num << "/" << X.denom;
return out;
}
/**********************************************************************/
int main() //Beginning of Main
{
rational ans, frac1, frac2;
cout << "Welcome to the Fraction Arthmetic Program!\n" << endl;
cout << "Enter your 1st fraction (EX: a/b): ";
cin >> frac1;
cout << "Enter your 2nd fraction (EX: a/b): ";
cin >> frac2;
ans = frac1 + frac2;
cout << "\nAddition of: "<< frac1 << " + " << frac2 << " = " << ans << endl;
ans = frac1 - frac2;
cout << "Subtraction of: "<< frac1 << " - " << frac2 << " = " << ans << endl;
ans = frac1 * frac2;
cout << "Multiplication of: "<< frac1 << " * " << frac2 << " = " << ans << endl;
ans = frac1 / frac2;
cout << "Division of: "<< frac1 << " / " << frac2 << " = " << ans << "\n" << endl;
return 0;
} //End of Main
If I have not properly posted my code please help me out on this too. I think this is the way it is supposed to go, but if I am wrong please tell me so that in future posts I can correct myself. I am not trying to offend anyone. Thank You in advance.