// File: asn2_f01.cpp // // Driver program for writing and testing recursive functions // // #include #include #include #include using namespace std; const int Quit = 8; void DisplayMenu (); int RecordChoice (); double power (double base, int exp); void main () { int choice, n, n1, n2, digits; string str, str1, str2; cout << "This program demonstrates recursive function calls\n" << "plus a few iterative ones\n\n"; DisplayMenu (); choice = RecordChoice (); do { switch (choice) { case 1: cout << "\nThis section converts a string of digits to " << "the \ncorresponding integer iteratively\n"; cout << "Enter a string consisting entirely of digits: "; cin >> str1; // n1 = stringToInt (str1); cout << "The corresponding integer is " << n1 << endl; cout << "\nEnter another string consisting entirely of digits: "; cin >> str2; // n2 = stringToInt (str2); cout << "The corresponding integer is " << n2 << endl << endl; cout << n1 << " + " << n2 << " = " << n1 + n2 << endl << endl; break; case 2: cout << "\nThis section converts a string of digits to " << "the \ncorresponding integer recursively\n"; cout << "Enter a string consisting entirely of digits: "; cin >> str1; // n1 = rstringToInt (str1); cout << "The corresponding integer is " << n1 << endl; cout << "\nEnter another string consisting entirely of digits: "; cin >> str2; // n2 = rstringToInt (str2); cout << "The corresponding integer is " << n2 << endl << endl; cout << n1 << " + " << n2 << " = " << n1 + n2 << endl << endl; break; case 3: cout << "\nThis section determines whether or not a given" << " string is a palindrome" << endl; cout << "Enter the string which can include blanks and " << "press Enter twice\n"; getline (cin, str); /* if (risPalindrome (str)) cout << str << " is a palindrome\n"; else cout << str << " is not a palindrome\n"; */ break; case 4: cout << endl << "This section determines the number of " << "digits in a positive integer" << endl; do { cout << "Enter the positive integer: "; cin >> n; } while (n <= 0); // cout << endl << n << " has " << ndigits (n) << " digit(s)\n\n"; break; case 5: cout << endl << "This section prints the digits of a non-" << "negative integer in reverse" << endl; do { cout << "Enter the non-negative integer: "; cin >> n; } while (n < 0); cout << "The reverse of " << n << " is "; // rprint (n); break; case 6: cout << endl << "This section determines whether or not a " << "given integer is a palindrome" << endl; do { cout << "Enter the non-negative integer to test for " << "palindrome: "; cin >> n; } while (n < 0); /* call the function */ break; case Quit - 1: DisplayMenu (); break; } if (choice != Quit) choice = RecordChoice (); } while (choice < Quit); } void DisplayMenu () { cout << " 1 -- convert a string of digits to an integer iteratively\n"; cout << " 2 -- convert a string of digits to an integer recursively\n"; cout << " 3 -- test string for palindrome recursively\n"; cout << " 4 -- determine number of digits in positive integer\n"; cout << " 5 -- print a number's digits in reverse\n"; cout << " 6 -- test integer for palindrome recursively\n"; cout << " " << Quit - 1 << " -- display menu\n"; cout << " " << Quit << " -- Quit\n"; } int RecordChoice () { int choice; cout << endl << "Enter menu choice (" << Quit - 1 << " to view " << "menu, " << Quit << " to quit): "; do { cin >> choice; } while (choice < 1 || choice > Quit); // specifically read the newline char so that a string entered // following the menu choice is interpreted correctly char ch = cin.get(); return choice; } double power (double base, int exp) { // note that exp must be >= 0 int i; double product = 1; assert (exp >= 0); for (i = 1; i <= exp; i++) product *= base; return product; }