| | |
Need help with a Reduce Fraction as part of a Rational Class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 10
Reputation:
Solved Threads: 0
I'm lost based on the code provided, how to reduce a fraction down. I've looked on the internet and yes most are using global variables; I however, can not for my class. It must be within the function. And there needs to be two calls outside of each of the operators to the reduce() function. Where am I going wrong?
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; #include "Rational.h" // the setNumerator function sets the numerator value for the object. void Rational::setNumerator (int numer) { numeratorValue = numer; } // setDenominator is responsible for changing the denominatorValue data // member. It does some basic error checking to make sure we don.t try // to set a 0 as the denominator. Our solution to this is to set the // denom to 1 after outputting an error message void Rational::setDenominator (int denom) { if (denom != 0) { denominatorValue = denom; } else { cerr << "Illegal denominator. Using 1 instead" << endl; denominatorValue = 1; } return; } // specific constructor, called when one or two arguments are given // in object instantiation. Sets rational to appropriate value Rational::Rational (int numer, int denom) { setNumerator(numer); setDenominator(denom); reduce(); } // getNumerator and getDenominator are simple, private inspector // member functions that return the values of the numerator and // denominator for our object. int Rational::getNumerator() const { return numeratorValue; } int Rational::getDenominator() const { return denominatorValue; } // add() performs addition of two Rational objects. One object // is doing the addition, the other object (the argument) is being // added. This function creates a very (very!) temporary third // Rational object as it.s setting itself up to return a value. It // returns the newly created Rational object. This is one of // the few times you see a constructor called explicitly. Rational Rational::add (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); Rational result(a*d + b*c, b*d); result.reduce(); return result; } // multiply() performs multiplying two Rational objects. One object // is doing the multiplying, the other object (the argument) is being // multiplied. This function creates a very (very!) temporary third // Rational object as it.s setting itself up to return a value. It // returns the newly created Rational object. This is one of // the few times you see a constructor called explicitly. Rational Rational::multiply (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); Rational result(a*c, b*d); result.reduce(); return result; } // insert() takes an ostream as the only argument. An ostream // is an output stream, like cout. by making this an argument, you // could output Rational numbers to files or other devices or streams. void Rational::insert (ostream &streamout) const { streamout << getNumerator() << "/" << getDenominator(); return; } // extract() takes an istream as the only argument. An istream // is an input stream, like cin. by making this an argument, you // could input Rational numbers from files or other devices or // streams. void Rational::extract (istream &streamin) { int numer; int denom; char slash; streamin >> numer >> slash >> denom; setNumerator(numer); setDenominator(denom); return; } // Here we implement the + operator. all this "function" does is end // up calling the add() behavior of the rational number class. // by overloading the operator, we are providing the .natural // interface. for the object that we are looking for to make working // with Rational objects intuitive. Rational operator+ (const Rational &left, const Rational &right) { return left.add(right); } // and here is the implementation for the * operator. We simply end up // calling the multiply() behavior of a rational object. Rational operator* (const Rational &left, const Rational &right) { return left.multiply(right); } // here is the implementation for the << operator. We simply end // up calling the insert() behavior of a rational object. Because // of the "nature of output" in C++ (to be explained in more detail in 1620) // we need to return the ostream out of the function. This is // a critical step in overloading the << operator. ostream& operator<< (ostream &output, const Rational &rat) { rat.insert(output); return output; } istream& operator>> (istream &input, Rational &rat) { rat.extract(input); return input; } Rational Rational::subtract (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); Rational result(a*d - b*c, b*d); result.reduce(); return result; } Rational operator- (const Rational &left, const Rational &right) { return left.subtract(right); } Rational Rational::divide (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); Rational result(a*d, b*c); result.reduce(); return result; } Rational operator/ (const Rational &left, const Rational &right) { return left.divide(right); } bool Rational::lessThan (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if (a*d < c*b) return true; else return false; } bool Rational::lessThanEqual (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if ( a*d <= c*b) return true; else return false; } bool Rational::greaterThan (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if (a*d > c*b) return true; else return false; } bool Rational::greaterThanEqual (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if (a*d >= c*b) return true; else return false; } bool Rational::equalTo (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if ((a==c) && (b==d)) return true; else return false; } bool Rational::notEqualTo (const Rational &r) const { int a = getNumerator(); int b = getDenominator(); int c = r.getNumerator(); int d = r.getDenominator(); if ((a!=c) || (b!=d)) return true; else return false; } void Rational::reduce() { int x = getNumerator(); int a; x = a; int y = getDenominator(); int b; y = b; int i; while (i = (a % b)) { a = b; b = i; } x /= b; y /= b; } bool operator< (const Rational &left, const Rational &right) { return left.lessThan(right); } bool operator<= (const Rational &left, const Rational &right) { return left.lessThanEqual(right); } bool operator> (const Rational &left, const Rational &right) { return left.greaterThan(right); } bool operator>= (const Rational &left, const Rational &right) { return left.greaterThanEqual(right); } bool operator== (const Rational &left, const Rational &right) { return left.equalTo(right); } bool operator!= (const Rational &left, const Rational &right) { return left.notEqualTo(right); }
C++ Syntax (Toggle Plain Text)
void Rational::reduce() { int x = getNumerator(); int a; x = a;
C++ Syntax (Toggle Plain Text)
int y = getDenominator(); int b; y = b;
C++ Syntax (Toggle Plain Text)
int i; while (i = (a % b))
C++ Syntax (Toggle Plain Text)
{ a = b; b = i; } x /= b; y /= b; }
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
I just wrote this for my class:
//written by Ben Hinrichs 2009
#include <iostream.h>
class RationalNumber
{
public:
RationalNumber()
{
doTheMath();
}
void doTheMath()
{
cout << "Enter an int: ";
cin >> a;
cout << "Enter a denominator: ";
cin >> b;
if (b <= 0)
cout << "Enter a valid denominator\n";
else
{
c = a;
d = b;
cout << c << "/" << d << " reduces to ";
if(b % a == 0)
{
gcf = a;
c = a / gcf;
d = b / gcf;
cout << c << "/" << d << endl;
}
else
{
gcf = 0;
do
{
modul = b % a;
gcf = a;
b = a;
a = modul;
} while(modul != 0);
c = c / gcf;
d = d / gcf;
cout << c << "/" << d << endl;
}
}
}
private:
int a, b, c, d, modul, gcf;
};
int main()
{
RationalNumber Ponies;
return 0;
} Last edited by Xantox; Jul 14th, 2009 at 12:53 pm.
![]() |
Similar Threads
- Need help with GCD algorithm (C++)
- Rational Number Constructor (C++)
- C++ problem (C++)
- c++ classes (C++)
- Derived class problem (C)
- a program about fractions (C++)
Other Threads in the C++ Forum
- Previous Thread: no match for operator= in a map?
- Next Thread: Creating a simple series ?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






