Thread: Code decoding

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    11

    Smile Code decoding

    Hi Everyone,
    I have this code for one of my programs and I am not sure what it does. It was given to me by my professor. It is suppose to simplify fractions. But it is messing up my program. In this program we use overloaded operators and Class objects. It is a fraction calculator program.
    In my code, I add, subtract, multiply and divide the fractions and that part works fine. Then the not simplified fraction (the answer not the ones being added) is sent to this function to be simplified. For some reason it is not working properly after being sent to this function.
    Please can someone explain this function to me so I can better understand what this function does and how it is affecting my code?

    Thanks

    Code:
    void Rational::reduce(  )
    {
       int largest, gcd = 1;  // greatest common divisor;
    
       largest = ( numerator > denominator ) ? numerator: denominator;
    
       for ( int loop = 2; loop <= largest; ++loop )
           if ( numerator % loop == 0 && denominator % loop == 0 )
              gcd = loop;
    
       numerator /= gcd;
       denominator /= gcd;
    }

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    What's the problem anyway? I've adopted it to C, tested, and it works like a charm.

    To the explanation, it's pretty obvious. First it fetches which one of num/demom is larger and then checks for being both divisible by the same number, counting until the largest of two. Then it simplifies them by division.

    That's my version:
    Code:
    #include <stdio.h>
    
    void simple(int *numerator, int *denominator);
    
    int main ( void )
    {
      int n, d;
    
      printf("Enter num and denom: ");
      scanf("%d %d", &n, &d);
      simple(&n, &d);
      printf("%d %d\n", n, d);
    
      return 0;
    }
    
    
    void simple(int *numerator, int *denominator)
    {
       int largest, loop, gcd = 1;  // greatest common divisor;
    
       largest = ( *numerator > *denominator ) ? *numerator: *denominator;
    
       for ( loop = 2; loop <= largest; ++loop )
           if ( *numerator % loop == 0 && *denominator % loop == 0 )
              gcd = loop;
    
       *numerator /= gcd;
       *denominator /= gcd;
    }
    Last edited by GL.Sam; 08-04-2009 at 09:40 PM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    11
    thats the thin, I am not sure why it does not work.
    Here is my implementation of all of the functions.

    Code:
    #include <cstdlib>
    #include <iostream> 
    #include "rational.h"
    using namespace std;
    
    Rational::Rational( int n, int d )
    {
       if (d)
       {
    		numerator = n;
    		denominator = d;
       }
       else
       {
    		numerator = 1;
    		denominator = 1;
       }
    }
    bool Rational::operator!=( const int num )
       { return !( numerator == num ); }
    
    //Fraction reducer function definition
    void Rational::reduce(  )
    {
       int largest, gcd = 1;  // greatest common divisor;
    
       largest = ( numerator > denominator ) ? numerator: denominator;
    
       for ( int loop = 2; loop <= largest; ++loop )
           if ( numerator % loop == 0 && denominator % loop == 0 )
              gcd = loop;
    
       numerator /= gcd;
       denominator /= gcd;
    }
    
    //Input operator function definition
    istream &operator>> (istream &in, Rational &pFractB)
    {
    	char slash;
    	
    	in >> pFractB.numerator >> slash >> pFractB.denominator;
    	
    	return in;
    }
    
    //Output operator function defintion
    ostream &operator<< (ostream &out, Rational &pFractB)
    {
    	if(pFractB.denominator == 1)
    		out << pFractB.numerator << endl;
    	else
    		out << pFractB.numerator << "/" << pFractB.denominator << endl;
    	
    	return out;
    }
    
    
    //Addition operator function definition
    Rational Rational::operator+ (Rational pFractB)
    {
    	
    	Rational tObj;
    	
    	//calculates denominator 
    	tObj.numerator = (denominator * pFractB.numerator) + (numerator * pFractB.denominator);
    	
    	//calculates numerator
    	tObj.denominator = (denominator * pFractB.denominator);
    	
    	tObj.reduce();
    	
    	return tObj;
    	
    }
    
    //Subtraction operator function definition
    Rational Rational::operator- (Rational pFractB)
    {
    	
    	Rational tObj;
    	
    	//calculates denominator
    	tObj.numerator = (denominator * pFractB.numerator) - (numerator * pFractB.denominator);
    
    	//calculates numerator
    	tObj.denominator = (denominator * pFractB.denominator);
    	
    	tObj.reduce();
    
    	return tObj;
    }
    
    //Multiplication operator function definition
    Rational Rational::operator* (Rational pFractB)
    {
    	Rational tObj;
    	
    	//calculates the numerator
    	tObj.numerator = numerator * pFractB.numerator;
    	
    	//calculates the denominator
    	tObj.denominator = denominator * pFractB.denominator;
    	
    	tObj.reduce();
    	
    	return tObj;
    }
    
    //Division operator function definition
    Rational Rational::operator/ (Rational pFractB)
    {
    	Rational tObj;
    	
    	//calculates the numerator
    	tObj.numerator = numerator * pFractB.denominator;
    
    	//calculates the denominator	
    	tObj.denominator = denominator * pFractB.numerator;
    	
    	tObj.reduce();
    	
    	return tObj;
    }
    
    //Greater Than operator function definition
    bool Rational::operator> (Rational &pFractB)
    {
    	bool greaterThan = 0;
    	float tObj;
    	float testObj;
    
    	testObj = numerator / denominator;
    	
    	tObj = pFractB.numerator / pFractB.denominator;
    	
    	if(testObj > tObj)
    		greaterThan = 1;
    		
    	return greaterThan;
    }
    
    //Less Than operator function definition
    bool Rational::operator< (Rational &pFractB)
    {
    	bool lessThan = 0;
    	float tObj;
    	float testObj;
    	testObj = numerator / denominator;
    	
    	tObj = pFractB.numerator / pFractB.denominator;
    	
    	if(testObj < tObj)
    		lessThan = 1;
    		
    	return lessThan;
    }
    This is the client file:

    Code:
    #include<iostream>
    #include"rational.h"
    #include<fstream>
    
    using namespace std;
    
    ofstream outfile;
    ifstream infile;
    
    
    int main()
    {
    	Rational FractA, FractB, FractR;
    
    	infile.open("ratdata.dat");
    	outfile.open("ratout.txt");
    	
    	int cnt = 0;
    	char oper;
    
    	infile >> FractA;
    	while (FractA != 0)
    	{	cnt++;
    		infile >> oper;
    		infile >> FractB;
    
    		if(oper == '+')
    			FractR =  FractA + FractB;
    
    		else if(oper == '-')
    			FractR =  FractA - FractB;
    
    		else if(oper == '*')
    			FractR =  FractA * FractB;
    
    		else if(oper == '/')
    			FractR =  FractA / FractB;
    
    
    		outfile << cnt << ": ";
    		outfile << FractA << "  " << oper 
    				<< "  "  << FractB << " = " 
    				<< FractR << endl << endl;
    		outfile << cnt << ": ";
    		if (FractA > FractB)
    			outfile << FractA << " is greater than " << FractB << endl;
    		else 
    			outfile << FractA << " is NOT greater than " << FractB << endl;
    		outfile << "--------------------------------------" << endl;
    		infile >> FractA;
    
    	} // while
    
    
    	return 0;
    } // main
    and here is the header file:

    Code:
     
    #ifndef RATIONAL_H
    #define RATIONAL_H
    
    #include <iostream> 
    
    using namespace std;
    
    class Rational 
    {
    friend istream &operator>> (istream &in, Rational &pFractB);
    friend ostream &operator<< (ostream &out, Rational &pFractB);
    
    public:
       Rational( int = 1, int = 1 ); // default constructor
       bool operator!=( const int );
       
       Rational operator+ (Rational pFractB);
       Rational operator- (Rational pFractB);
       Rational operator* (Rational pFractB);
       Rational operator/ (Rational pFractB);
       bool operator> (Rational &pFractB);
       bool operator< (Rational &pFractB);
       
    private:
       int numerator;
       int denominator;
       void reduce(  );
    };
    
    #endif
    The program runs, it is just the answers are wrong and the greater and less than functions are not working. I tested to see if the addition was working and it was before going through the reduce function.
    So I though the relationship between the two functions was off somehow.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your / looks the same as *

    Also, when the code doesn't "seem" to work, use a debugger.

    Where you can
    - set a breakpoint at the start of where you suspect trouble is starting
    - examine variables to see that they're correct
    - slowly execute code (one line or one function) at a time until you find an inconsistency.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Salem View Post
    Your / looks the same as *
    I have no idea what that means.

    Anyway, my basic tests indicate that you are subtracting "backwards", but all the simplifications are being done correctly.

    EDIT: And of course your < and > are going to be broken for as long as you try to do integer division. (Hint: a properly written < or > for fractions will not involve division in any way, shape, or form.)
    Last edited by tabstop; 08-04-2009 at 11:02 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, ignore me - they look different now to what they did before - shrug.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    11
    Thanks everyone for your help. I don't have a debugger so things are just a little harder for me when it comes to doing that lol. Thanks for all the tips and suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM