/* testfunc17.cpp CIS 150 2008-06-10 David Klick Recursive and iterative greatest common divisor implementations. */ #include #include #include using std::cin; using std::cout; int gcd_r(int a, int b); // recursive gcd function int gcd_i(int a, int b); // iterative gcd function int getInt(char* prompt, int min, int max); char getChoice(char* prompt, char* allowed); void cont(); int main(int argc, char* argv[]) { char ans; // holds user answer for "run again?" int n1, n2, result; cout << "GCD calculator\n"; do { // get numbers from user for GCD calc n1 = getInt("Enter an integer greater than 0: ", 1, INT_MAX); n2 = getInt("Enter another integer greater than 0: ", 1, INT_MAX); // calculate and display recursive GCD result = gcd_r(n1, n2); cout << "The GCD of " << n1 << " and " << n2 << " is " << result << " (recursive)\n"; // calculate and display non-recursive GCD result = gcd_i(n1, n2); cout << "The GCD of " << n1 << " and " << n2 << " is " << result << " (non-recursive)\n"; // find out if user wants to run gcd again ans = getChoice("Would you like to calculate another GCD? (Y/N) ", "YyNn"); } while (ans == 'Y'); // say "Goodnight" Gracie cout << "Normal program termination.\n"; cont(); return 0; } /* int getInt(char* prompt, int min, int max); Arguments: prompt: a pointer to a C-style string to be used as a prompt min: the minimum value allowed to be entered max: the maximum value allowed to be entered Returns: an integer entered by the user Prompts user with message passed in as argument and returns the integer value entered. The number must be between min and max (inclusive) or the user will be reprompted to enter another number. Note: For simplicity, no error checking on input is done, therefore this function may crash the program on invalid input. */ int getInt(char* prompt, int min, int max) { int num; do { cout << prompt; cin >> num; if (num < min) cout << "Error: value below minimum allowed\n"; else if (num > max) cout << "Error: value above maximum allowed\n"; } while (nummax); return num; } /* char getChoice(char* prompt, char* allowed); Arguments: char* prompt: a C-style string containing the prompt to show the user char* allowed: a C-style string containing all allowed user responses (each valid response being a single character) Returns: the user's choice (a character) Displays a prompt and then waits for the user to enter an allowed choice (a single character). Once a valid choice has been made, the function returns it to the calling function. */ char getChoice(char* prompt, char* allowed) { char ans; do { cout << prompt; cin >> ans; ans = toupper(ans); // convert answer to upper case if (strchr(allowed, ans) == NULL) cout << "Error: Invalid choice\n"; } while (strchr(allowed, ans) == NULL); return ans; } /* int gcd_r(int a, int b); Arguments: int a: an arbitrary integer value int b: an arbitrary integer value Returns: the greatest common divisor of a and b Calculates and returns the greatest common divisor of two integers using a recursive algorithm. */ int gcd_r(int a, int b) { if (a%b == 0) return b; else return gcd_r(b, a%b); } /* int gcd_i(int a, int b); Arguments: int a: an arbitrary integer value int b: an arbitrary integer value Returns: the greatest common divisor of a and b Calculates and returns the greatest common divisor of two integers using a non-recursive (and quite inefficient) algorithm. */ int gcd_i(int a, int b) { int i = a= 2) { if (a%i==0 && b%i==0) return i; else i--; } return i; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }