From 331b001c7c42a23441c83a317c236e8000348a8e Mon Sep 17 00:00:00 2001 From: cdez Date: Mon, 23 Sep 2024 17:35:28 -0700 Subject: [PATCH] Finished the fraction program/class structure. Completed Homework 2. --- assignmentTwo/fraction | Bin 0 -> 18120 bytes assignmentTwo/fraction.cpp | 131 ++++++++++++++++++++++++++++++++ assignmentTwo/userInventory.cpp | 1 - 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100755 assignmentTwo/fraction diff --git a/assignmentTwo/fraction b/assignmentTwo/fraction new file mode 100755 index 0000000000000000000000000000000000000000..ceef2c6a437c15d5ab95d99d9db45decc5be4128 GIT binary patch literal 18120 zcmeHPe{@^beZNxd*fB}$1WeL|fe3^V(kT8F6WBtOI5ILM!L#EMLbF#{l5Gu3GSXA9 zTS{F5T~wi+(8Fj->6VStwdc^YWP|Ob=>bfEIO%DbX;0hibUXDpO(&0(nF1w)4(;>3 z_kN$AUZfsKw|_d{9O-+%pYMsnjk~5~iX- z@nly`Rn0r;l7&i-Da)<)JW4Jcbcc?gsojop)3i?6HEk-Td7YPT85CqXE`rI& zj%kCoW6JrYoX}^Zj>k!xAfv+6X*Zb{$I_6fL|AWWxGxW^UG{z!I-mXrRz>?8LtdhYV}>tFQrero*n#Xs8k^xu*W z#Z5M(Lxue55G}qDHPTV}c-ZYi&l5&v89q~ymt(0%Yut^87IsLm%vTQ_EG>h79#}cN z4ndT|&s4y_U%~%N75vv%(Eq0j{`XeEyDRvguAuL!(C(%RcxMIw)NdYqoMoayyE`l3 zXtu1Mk5<4Rt`O%t@W09#4EXubUm{kDPi#@b<>0AogFfLtpJYXY#)V-F#u9NOWd@U` zVF;sZ*A62bNk#^vDKnDXwWDJ=5s&N&_76vtth{8;&fdgu${!U5YUcHZ5gHwBZfeT_JyO5MTa|X8%4|_L^;xczC#+p1%jSt;$;h z?jpfh*DhmMli}}eN6>qA-umHNZBM29;mRM2$+lZV(YOqsE~LA&r#I1S_@$(!Ihsfr zXgp$I)<_1EV@5O{H4(B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I$u{guNBo<6n}{7JoD{>CDIjMSKp&F2@Cz z9`dlR7ne&1bD)58OFY_bgLWHV?*Hwf6nn7hVsNsF&d1mVNLg&yIj{`6&(t{;+;nky z|9tp^O>}SCo02gY{izgt-ZPAWl(01}kI7hRr2C0ZV^uo$RN@<_SnMN89SACOF3M*+ z1p@axQa8s{H(t8k=k4{9bA9pV~2Ig%s78&8-K?_fOb9Z4o|(99gOLH+3{&aXzp8p0W)b@ZH+ z>O;ZQkno1b;%GuqGpQu|a9j<40xn?;$dZxaAQ|Y|$gnBA@^12?>K#mgrAjIi5?(Vh z3YH$6&`!vQgEumyAC*I4cw-LD;3QRKG!z*z@f4?Hgwm^eiYgymN8`a*6z-Kn3|QgC z<2#0@a+x=o4+qgY3%zo?wVCr=I^SN(d!Ya47|>HIYt@OwY=?Wox8o#(Kx zW<^c$x>{1`_pRNYe_u28lxWj$)zWf1SnNPbaQwV}#I#AP<8@3HXZ%5+w8X>qyuQTL zrv(%v<&N$3>z;@L!%qmyp4Y3G^7<51WO3&IZlJXG!|n5W4^wCStjF{|*wb1P^SqwK zbV9bOa!he@{2CTHR4Aux&+A)EJvv}#{`mU8sO`6EhrC|Kl-JkDo_@M;`)vPLz^DoK zk5_3`H05t&(`=}z!@c;L!yZ2hRMC|6HR=pc9Cg??u$`t;Y^W*w zWBN^pJ^!9%dL=J(I_%l)a}Im{z0H)@X<6TS|GcE_x&MVnYijyA%`)NsWBz-nQ2yEe zfJaG~-m6&a+u8n4AhFxmUa3@>{+fig;%xs6WOnvA)@!DV?LY+Y6soZBmr)Q+B|1%wG;;!nl2(Pf3<*d!9d8 zUyGR)SLyb(x`ng-H5Q^O6e|@w<*}zE>f5Isj_Qep)??{!0vtbI*8*T1yRuZ@$LPU^ c@-uC(xMI7^(`{bP{&9V-m-Zali-Q&a1NKQ +#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; }