Fraction Program - Reducing it to the lowest terms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d0ugg
    New Member
    • Jan 2007
    • 41

    Fraction Program - Reducing it to the lowest terms

    Hello everyone,

    I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms.
    However, I'm not sure how the algorithm of reducing fractions works.
    Here is part of my code:

    Code:
    //Header File
    
    #include <iostream>
    using namespace std;
    
    class fraction
    {
    private:
    	int numerator;
    	int denominator;
    public:
    	fraction() : numerator(0), denominator(0)  //Constructor (no args) 
    	{ }
    
    	fraction(int num, int den=1); //(Fraction F2) : numerator(num), denominator(den) //Constructor (two args)
    	
    	friend fraction operator +(fraction f1, fraction f2);
    	friend fraction operator-(fraction f1, fraction f2);
    	friend fraction operator*(fraction f1, fraction f2);
    	friend fraction operator/(fraction f1, fraction f2);
    	friend istream& operator >> (istream& s, fraction& e);
    	friend ostream& operator<< (ostream& out, fraction& f);
    };
    
    //fraction.cpp
    
    #include <iostream>
    #include <string>
    #include "fraction.h"
    using namespace std;
    
    
    fraction operator+(fraction f1, fraction f2)
    {
        fraction temp;
    
        temp.numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
        temp.denominator = f1.denominator * f2.denominator;
    
        return temp;
     }
    
    
    ostream& operator<<(ostream& out, fraction& f)
    {
    	out << f.numerator << "/" << f.denominator;
    	return out;
    }
    
    istream& operator >> (istream& s, fraction& f)
    {
    	cout << "\nPlease type the numerator: ";
    	cin >> f.numerator;
    	cout << "Please type the denominator: ";
    	cin >> f.denominator;
    	return s;
    }
    
    //calc.cpp
    
    #include <iostream>
    #include <string>
    #include "fraction.h" 																											
    using namespace std;
    
    int main() 
    { 
    	fraction f1;
    	fraction f2;
    	fraction answer;
    	fraction f3;
    
    
    	char symbol = ' ';
    	string nome;
    
       while (true)
       { 
    	cout << "A\tADD\n";
    	cout << "S\tSUBTRACT\n";
    	cout << "M\tMULTIPLY\n";
    	cout << "D\tDIVIDE\n";
    	cout << "E\tEXIT\n";
    	cout << "\nWhat operation do you want to use? ";
    		cin >> symbol;
    		if (symbol == 'e' || symbol == 'E')
    			exit(0);
    
         switch (symbol) 
         { 
    	 case 'A':
    		 cout << "\nPlease type values for fraction 1 and fraction 2\n";
    		 cin >> f1 >> f2;
    		 cout << "\nFraction 1 = " << f1 << "\nFraction 2 = " << f2;
    		 f3 = f1 + f2;
    		 cout << "\n\bThe sum of F1 and F2 is: " << f3 << "\n" << endl;
    		 break;
     
       }
    
       return 0;
    }
    Thanks for all the support,

    Doug
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    How do you reduce a fraction in real life?

    Hint: Look up something called the greatest common divisor.

    Comment

    • d0ugg
      New Member
      • Jan 2007
      • 41

      #3
      Thanks for the help.
      I already got the algorithm to reduce the fraction.

      My header file is looking like this now

      Code:
      // This program was created by Douglas Alves
      // CS 1410
      
      #include <iostream>
      using namespace std;
      
      class fraction
      {
      private:
      	int numerator;
      	int denominator;
      public:
      	fraction() : numerator(0), denominator(0)  //Constructor (no args) 
      	{ }
      
      	fraction(int num, int den=1); //(Fraction F2) : numerator(num), denominator(den) //Constructor (two args)
      	
      	friend fraction operator +(fraction f1, fraction f2);
      	friend fraction operator-(fraction f1, fraction f2);
      	friend fraction operator*(fraction f1, fraction f2);
      	friend fraction operator/(fraction f1, fraction f2);
      	friend istream& operator >> (istream& s, fraction& e);
      	friend ostream& operator<< (ostream& out, fraction& f);
      	int GreatestCommonDivisor(int n, int d);
      	void Simplify() 
      	{
          
          int gcd, absNum = abs(numerator);
          if(numerator != 0 && absNum != 1 && denominator != 1) {
              gcd = GreatestCommonDivisor(absNum, denominator);
              if(gcd > 1) {
                  numerator = numerator / gcd;
                  denominator = denominator / gcd;
              }
          }
      }
      };
      -

      The compiler is giving me this error:

      fraction.obj : error LNK2019: unresolved external symbol "public: int __thiscall fraction::Great estCommonDiviso r(int,int)" (?GreatestCommo nDivisor@fracti on@@QAEHHH@Z) referenced in function "public: void __thiscall fraction::Simpl ify(void)" (?Simplify@frac tion@@QAEXXZ)


      I'm not sure what I did wrong..
      I would appreciate any help.

      Thanks once again,

      ~doug

      Comment

      • d0ugg
        New Member
        • Jan 2007
        • 41

        #4
        Nevermind.. I already found the error.
        Thank you for the help,

        ~doug

        Comment

        • labx
          New Member
          • Sep 2011
          • 1

          #5
          will you mind putting the full source code please?
          I also want to know how to get the lowest term of a fraction.

          Comment

          Working...