/*********************************************************** PROJECT: FractionADT FILE: testFractKBD.cpp - a testing program for ADT Fraction AUTHORS: Designed by R. Fecteau & W.W. Kirchherr Revised by Dale Roberts LAST REVISED: 09.12.2003 DBR ************************************************************/ #include #include "fraction.h" using std::cout; using std::endl; int main() { Fraction f, g, half(1,2), sum, diff, prod, quotient, neg, answer; bool done = false; int readResult, cmpResult; // Print instructions for the user cout << "To enter a fraction enter two integers separated by one space." << endl; cout << "To indicate end of data enter 9999 for the numerator" << endl; cout << "of the first fraction. Use any denominator except 0." << endl << endl; // This loop always executes at least once, so use the do/while // post-test loop. do { cout << "Enter your first fraction > "; readResult = Fraction::getFract( cin, f ); if (readResult == 0 ) { cout << "Error entering fraction" << endl; } else if ( f.getNumerator() == 9999 ) { done = true; } else { cout << "Enter another fraction > "; readResult = Fraction::getFract(cin, g); if ( readResult == 0 ) { cout << "Error enterering fraction" << endl; } else { // Perform Calculations neg = Fraction::negateFract(f); sum = Fraction::addFract(f,g); diff = Fraction::subFract(f,g); prod = Fraction::mulFract(f,g); quotient = Fraction::divFract(f,g); cmpResult = Fraction::compareFract( f, g ); /* (f + g) - ( f * -(1/2) ) */ answer = Fraction::subFract( Fraction::addFract( f,g ), Fraction::mulFract(f,Fraction::negateFract(half))); /* Display output */ cout << endl; cout << "F1 = "; Fraction::putFract(cout, f); cout << endl; cout << "F2 = "; Fraction::putFract(cout, g); cout << endl; cout << "Neg = "; Fraction::putFract(cout,neg); cout << endl; cout << "Sum = "; Fraction::putFract(cout,sum); cout << " Diff = "; Fraction::putFract(cout,diff); cout << " Prod = "; Fraction::putFract(cout,prod); cout << " Quot = "; Fraction::putFract(cout,quotient); cout << endl; if ( cmpResult == 0 ) { cout << "equal" << endl; } else if (cmpResult < 0 ) { cout << "less" << endl; } else { cout << "Greater" << endl; } cout << "Try one nested: " << endl; cout << "(f + g) - ( f * -(1/2) ) = "; Fraction::putFract(cout,answer); cout << endl; cout << "==========================================" << endl; } } } while (!done); return 0; }