Created
April 27, 2013 14:32
-
-
Save 0x000000AC/5473340 to your computer and use it in GitHub Desktop.
Fraction header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prevent multiple inclusions of header file | |
#include <iostream> | |
#ifndef RATIONAL_H | |
#define RATIONAL_H | |
using namespace std; | |
class Rational | |
{ | |
public: | |
Rational() //initialization constructor. | |
{ | |
numerator = 1; | |
denominator = 1; | |
} | |
Rational(int n, int d) | |
{ | |
numerator = n; | |
if (d == 0) | |
{ | |
cout << "CAN'T DIVIDE BY ZERO" << endl; | |
exit(0); // exit the program. can be re-written later for error | |
} | |
else | |
denominator = d; | |
} | |
Rational Sum(Rational otherFraction) | |
{ | |
int n = numerator * otherFraction.denominator + otherFraction.numerator * denominator; | |
int d = denominator * otherFraction.denominator; | |
return Rational( n / gcd(n,d), d / gcd(n,d) ); | |
} | |
Rational Difference(Rational otherFraction) | |
{ | |
int n = numerator * otherFraction.denominator - otherFraction.numerator * denominator; | |
int d = denominator * otherFraction.denominator; | |
return Rational( n / gcd(n,d), d / gcd(n,d)); | |
} | |
Rational Product(Rational otherFraction) | |
{ | |
int n = numerator * otherFraction.numerator; | |
int d = denominator * otherFraction.denominator; | |
return Rational(n / gcd(n,d), d / gcd(n,d)); | |
} | |
Rational Division(Rational otherFraction) | |
{ | |
int n = numerator * otherFraction.denominator; | |
int d = denominator * otherFraction.numerator; | |
return Rational( n / gcd(n,d), d / gcd(n,d)); | |
} | |
int gcd(int n, int d) | |
{ | |
int remainder; | |
while (d != 0) | |
{ | |
remainder = n % d; | |
n = d; | |
d = remainder; | |
} | |
return n; | |
} | |
void print() | |
{ | |
if (denominator == 1) // Simplify if denominator is one | |
cout << numerator << endl; | |
else | |
cout << "\n" << numerator << " / " << denominator << endl; | |
} | |
private: | |
int numerator; | |
int denominator; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment