diff --git a/assignmentTwo/fraction b/assignmentTwo/fraction new file mode 100755 index 0000000..ceef2c6 Binary files /dev/null and b/assignmentTwo/fraction differ diff --git a/assignmentTwo/fraction.cpp b/assignmentTwo/fraction.cpp index e69de29..674b3e1 100644 --- a/assignmentTwo/fraction.cpp +++ b/assignmentTwo/fraction.cpp @@ -0,0 +1,131 @@ +/* +Program Name: Fraction +Process: ??? +Function Input/Parameters: ??? +Function Output/Parameters: ??? +Function Output/Returned: ??? +Device Input/Device: ??? +Device Output/Monitor: ??? +Dependencies: ??? +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +// class definitions +class Fraction +{ +private: + int numerator; + int denominator; +public: + Fraction() + { + // default constructor, initializes to 0/1 + numerator = 0; + denominator = 1; + } + Fraction(int numeratorInput, int denominatorInput) + { + numerator = numeratorInput; + denominator = denominatorInput; + } + ~Fraction() + { + // default destructor + } + // accessor functions + // mutator functions + // operators + friend bool operator==(const Fraction &fractionOne, const Fraction &fractionTwo) + { + // return true if the numerator and the denominator are equal + // return ((fractionOne.numerator == fractionTwo.numerator) && (fractionOne.denominator == fractionTwo.denominator)); // old version before hints + return ((fractionOne.numerator * fractionTwo.denominator) == (fractionTwo.numerator * fractionOne.denominator)); + } + friend Fraction operator+(const Fraction &fractionOne, const Fraction &fractionTwo) + { + return Fraction(((fractionOne.numerator * fractionTwo.denominator) + (fractionTwo.numerator * fractionOne.denominator)), + (fractionOne.denominator * fractionTwo.denominator)); + } + friend Fraction operator-(const Fraction &negateFraction) + { + return Fraction(-negateFraction.numerator, negateFraction.denominator); + } + friend Fraction operator-(const Fraction &fractionOne, const Fraction &fractionTwo) + { + return Fraction(((fractionOne.numerator * fractionTwo.denominator) - (fractionTwo.numerator * fractionOne.denominator)), + (fractionOne.denominator * fractionTwo.denominator)); + } + friend Fraction operator*(const Fraction &fractionOne, const Fraction &fractionTwo) + { + return Fraction((fractionOne.numerator * fractionTwo.numerator), (fractionOne.denominator * fractionTwo.denominator)); + } + friend Fraction operator/(const Fraction &fractionOne, const Fraction &fractionTwo) + { + return Fraction((fractionOne.numerator * fractionTwo.denominator), (fractionOne.denominator * fractionTwo.numerator)); + } + friend ostream &operator<<(ostream &out, const Fraction &fractionOut) + { + return out << fractionOut.numerator << "/" << fractionOut.denominator; + } + friend istream &operator>>(istream &in, Fraction &fractionIn) + { + // get a temporary string to read into. + string inStr; + // read in the string + in >> inStr; + // find position of the '/' character in the string (had to use the size_t to prevent errors/warnings.) + size_t slashPos = inStr.find("/"); + // check to see if the slash was found, if it was, load the data. + if(slashPos != string::npos) + { + // set the data in. + fractionIn.numerator = atoi(inStr.substr(0, slashPos).c_str()); + fractionIn.denominator = atoi(inStr.substr(slashPos + 1).c_str()); + } + return in; + } +}; + +// function definitions + +// main function +int main() +{ + Fraction fiveFifths(5, 5); + Fraction threeFifths(3, 5); + Fraction sevenTenths(7, 10); + Fraction fourTenths(4, 10); + Fraction userFraction; + // let user know the fractions. + cout << "The fractions are: " << endl; + cout << "\t" << fiveFifths << endl; + cout << "\t" << threeFifths << endl; + cout << "\t" << sevenTenths << endl; + cout << "\t" << fourTenths << endl; + // ask the user to input a fraction. + cout << "Enter in a fraction in numerator/denominator format: [n/d]" << endl; + cin >> userFraction; + cin.ignore(); + // print back out the fraction + cout << "Your fraction is: " << endl; + cout << "\t" << userFraction << endl; + // add seven tenths and four tenths + cout << sevenTenths << " + " << fourTenths << " is " << sevenTenths + fourTenths << endl; + // subtract five fifths from three fifths + cout << fiveFifths << " - " << threeFifths << " is " << fiveFifths - threeFifths << endl; + // multiply three fifths and four tenths + cout << threeFifths << " * " << fourTenths << " is " << threeFifths * fourTenths << endl; + // divide four tenths and three fifths. + cout << fourTenths << " / " << threeFifths << " is " << fourTenths / threeFifths << endl; + return 0; +} diff --git a/assignmentTwo/userInventory.cpp b/assignmentTwo/userInventory.cpp index cbe3827..5238b4a 100644 --- a/assignmentTwo/userInventory.cpp +++ b/assignmentTwo/userInventory.cpp @@ -282,6 +282,5 @@ int main() cout << endl; cout << "Number of bundle objects: " << UserInventory::getBundleCount() << endl; } - // code here. return 0; }