Fraction Multiply(Fraction & lhs, Fraction & rhs) { Fraction ret; ret.mNum = lhs.mNum * rhs.mNum; ret.mDen = lhs.mDen * rhs.mDen; ret.Reduce(); return ret; }
int main() { Fraction a; Fraction b; Fraction c; int op; char opchar; init_graphics(); // Initialize graphics in msoftcon.h a.SetAll(1); // 1 indicates that it's the 1st Fraction b.SetAll(2); // 2 indicates that it's the 2nd Fraction cout << "Enter an operation to perform { + - / * } "; cin >> opchar; switch (opchar) { case '+': op = FRAC_ADD; break; case '-': op = FRAC_SUB; break; case '*': op = FRAC_MUL; break; case '/': op = FRAC_DIV; break; default: // If operator is illegal shut program down cout << "Invalid operator." << endl; return(0); } if((a.den == 0) || (b.den == 0)) // Fraction undefined if so cout << "\n\nFraction is undefined."; else { // Else Function can be worked with c = Fraction_Do_Op(op, a, b); cout << "\n\n"; c.Reduce(); // Reduce to simplify Fraction cout << "Fraction value: " << c.num << "/" << c.den << endl; cout << "Decimal value: " << FracDecVal(c) << endl; c.DisplayMixed(); } cout << "Press any key to continue" << endl; cout.flush(); getch(); return(0); }
Fraction Subtract(Fraction & lhs, Fraction & rhs) { Fraction ret; if (lhs.mDen != rhs.mDen) { int right_denom = rhs.mDen; int left_denom = lhs.mDen; lhs.mNum = lhs.mNum * right_denom; lhs.mDen = lhs.mDen * right_denom; rhs.mNum = rhs.mNum * left_denom; rhs.mDen = rhs.mDen * left_denom; } ret.mNum = lhs.mNum - rhs.mNum; ret.mDen = rhs.mDen; ret.Reduce(); return ret; }