/******************************************************* PROGRAM NAME - greedy (Problem 2) FILE NAME = greedy.cpp PROGRAMMER NAME - Justin C. Miller USAGE – type 'greedyam' to run me COMPILE – type 'make' to use my makefile DATE - Started 09-17-2003 BUGS - I think everything works fine! DESCRIPTION - This program runs the greedy algorithm using the rational class implemented with the BigInt instead of int. *******************************************************/ // uncomment the next line to debug this program // #DEBUG // include files #include #include // include my own files #include "bigint.h" #include "rational.h" using namespace std ; // function prototypes rational recipcrocal(rational) ; void greedy(rational) ; int main(int argc, char * argv[]){ rational user_input ; cout << "Please input your rational number ( [integer][space]/[space][integer] ) :" ; cin >> user_input ; cout << "\nEgyptian Fractions..." << endl ; cout << user_input << " = " ; greedy(user_input) ; cout << endl << endl ; // the code below was just to test if the // rational class still worked after I // changed everything over to BigInt #ifdef DEBUG rational a ; cout << "Please input a rational number: " ; cin >> a ; cout << "You entered: " << a << endl ; BigInt top(27) ; BigInt bot(6) ; rational b(top,bot) ; cout << "B was 27 / 6 but should now be normalized: " << b << endl ; cout << a << " + " << b << " = " << (a + b) << endl ; cout << a << " - " << b << " = " << (a - b) << endl ; cout << a << " * " << b << " = " << (a * b) << endl ; cout << a << " / " << b << " = " << (a / b) << endl ; cout << "The ceiling of " << a << " is " << a.ceiling() << endl ; cout << "The ceiling of " << b << " is " << b.ceiling() << endl ; cout << "The floor of " << a << " is " << a.floor() << endl ; cout << "The floor of " << b << " is " << b.floor() << endl ; #endif return EXIT_SUCCESS ; }//end of main /************************************************************** Function reciprocal DESCRIPTION: returns the reciprocal of a rational number PARAMETERS: i: rational number you want flipped PRE: i: is not zero POST: returns the reciprocal of i ***************************************************************/ rational reciprocal(rational i){ BigInt zero(0) ; if(i.numerator() == zero){ cerr << "Can't reciprocate zero!" << endl ; exit(EXIT_FAILURE) ; }//if return rational(i.denominator(), i.numerator()) ; }//end of reciprocal /************************************************************** Function greedy DESCRIPTION: prints out the egyptian fractions of a rational number PARAMETERS: user_input: c++ string PRE: none POST: none ***************************************************************/ void greedy(rational user_input){ rational zero(0) ; rational dummy ; bool first_time = true ; do{ dummy = reciprocal(user_input) ; dummy = dummy.ceiling() ; dummy = reciprocal(dummy) ; if(first_time) first_time = false ; else cout << " + " ; cout << dummy ; user_input -= dummy ; }while(user_input.compare(zero) != 0) ; }//end of greedy