Created
November 29, 2011 14:42
-
-
Save edhedges/1405031 to your computer and use it in GitHub Desktop.
Fraction Computation file
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
/********************************************** | |
* fraction.cpp: this handles all of the actual | |
computations done to the fractions. * | |
***********************************************/ | |
#include <iostream> | |
using namespace std; | |
#include "fraction.h" | |
/********************************************** | |
* setValues function: this function is pretty | |
self explanitory it sets the values for a | |
certain fraction. * | |
***********************************************/ | |
void Fraction :: setValues(int x, int y){ | |
num = x; | |
denom = y; | |
} | |
/********************************************** | |
* plus function: this function adds the two | |
fractions together and returns the answer. * | |
***********************************************/ | |
Fraction Fraction :: plus(Fraction f){ | |
Fraction frac; | |
if(denom == f.denom){ | |
num += f.num; | |
} | |
else{ | |
num = (num * f.denom) + (f.num * denom); | |
denom *= f.denom; | |
} | |
frac.setValues(num, denom); | |
return frac; | |
} | |
/********************************************** | |
* minus function: this function subtracts the | |
two fractions together and returns the answer.* | |
***********************************************/ | |
Fraction Fraction :: minus(Fraction f){ | |
Fraction frac; | |
if(denom == f.denom){ | |
num -= f.num; | |
} | |
else{ | |
num = (num * f.denom) - (f.num * denom); | |
denom *= f.denom; | |
} | |
frac.setValues(num, denom); | |
return frac; | |
} | |
/********************************************** | |
* times function: this function multiplies the | |
two fractions together and returns the answer.* | |
***********************************************/ | |
Fraction Fraction :: times(Fraction f){ | |
Fraction frac; | |
num *= f.num; | |
denom *= f.denom; | |
frac.setValues(num, denom); | |
return frac; | |
} | |
/********************************************** | |
* divide function: this function divides the | |
two fractions together and returns the answer.* | |
***********************************************/ | |
Fraction Fraction :: divide(Fraction f){ | |
Fraction frac; | |
num *= f.denom; | |
denom *= f.num; | |
frac.setValues(num, denom); | |
return frac; | |
} | |
/********************************************** | |
* reduce function: this function reduces the | |
fraction and returns the answer. * | |
***********************************************/ | |
Fraction Fraction :: reduce(){ | |
Fraction frac; | |
bool switcher = false; | |
if(num < 0 && denom < 0){ | |
num *= -1; | |
denom *= -1; | |
switcher = false; | |
} | |
else if(num < 0){ | |
num *= -1; | |
switcher = true; | |
} | |
else if(denom < 0){ | |
denom *= -1; | |
switcher = true; | |
} | |
if(num == 0){ | |
num = 0; | |
denom = 1; | |
frac.setValues(num, denom); | |
return frac; | |
} | |
else if(denom == 0 && num != 0){ | |
frac.setValues(num, denom); | |
return frac; | |
} | |
if(num == denom && num != 0){ | |
num = 1; | |
denom = 1; | |
} | |
else if(num > denom){ | |
for(int i = denom; i > 0; i--){ | |
if(num%i == 0 && denom%i == 0){ | |
num = num/i; | |
denom = denom/i; | |
} | |
} | |
} | |
else if(num < denom){ | |
for(int i = num; i > 0; i--){ | |
if(num%i == 0 && denom%i == 0){ | |
num = num/i; | |
denom = denom/i; | |
} | |
} | |
} | |
if(switcher == true){ | |
num *= -1; | |
} | |
frac.setValues(num, denom); | |
return frac; | |
} | |
/********************************************** | |
* print function: this function prints the | |
fraction to the console. * | |
***********************************************/ | |
void Fraction :: print(){ | |
if(denom == 0){ | |
cout << "No real numbers because of the 0 in the denominator" << endl; | |
} | |
else if(denom == 1 && num == 0){ | |
cout << num << endl; | |
} | |
else if(denom == 1){ | |
cout << num << endl; | |
} | |
else{ | |
cout << num << "/" << denom << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment