# Code decoding

• 08-04-2009
ash4741
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; }```
• 08-04-2009
GL.Sam
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; }```
• 08-04-2009
ash4741
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.
• 08-04-2009
Salem
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.
• 08-04-2009
tabstop
Quote:

Originally Posted by Salem
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.)
• 08-05-2009
Salem
Yeah, ignore me - they look different now to what they did before - shrug.
• 08-05-2009
ash4741
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.