Source of palint.cpp


  1: // Filename: PALINT.CPP
  2: // Purpose:  Determines whether an integer is palindromic.

  4: #include <iostream>
  5: #include <iomanip>

  7: void DisplayIDInfo(int);
  8: void DescribeProgram();
  9: void DisplayMainMenu(int);
 10: void GetMenuChoice(int, int&);
 11: void Pause(int);

 13: bool IsAPalindrome(/* in */ int n);

 15: int main()
 16: {
 17:     const int INDENT_LEVEL = 20;
 18:     int menuChoice;
 19:     int n;

 21:     DisplayIDInfo(INDENT_LEVEL/2);

 23:     do
 24:     {
 25:         DisplayMainMenu(INDENT_LEVEL);
 26:         GetMenuChoice(INDENT_LEVEL, menuChoice);

 28:         switch (menuChoice)
 29:         {
 30:             case 1:
 31:                 cout << setw(INDENT_LEVEL) << ""
 32:                      << "Program is now terminating. "
 33:                      << endl;
 34:                 break;
 35:             case 2:
 36:                 DescribeProgram();
 37:                 break;
 38:             case 3:
 39:                 cout << setw(INDENT_LEVEL) << ""
 40:                      << "Enter a positive integer: ";
 41:                 cin >> n;  cin.ignore(80, '\n');  cout  << endl;
 42:                 cout << setw(INDENT_LEVEL) << ""
 43:                      << "The integer " << n << " is ";
 44:                 if (IsAPalindrome(n))
 45:                     cout << "a palindrome. ";
 46:                 else
 47:                     cout << "not a palindrome. ";
 48:                 cout << endl << endl;
 49:                 break;
 50:         }

 52:         if (menuChoice != 1)
 53:             Pause(0);

 55:     } while (menuChoice != 1);

 57:     cout << endl;

 59:     return 0;
 60: }
 61: 
 62: void DisplayIDInfo(int indentLevel)
 63: // Pre:  indentLevel contains a nonnegative integer.
 64: // Post: A blank line, and a line containing the programmer's name
 65: //       have been displayed.
 66: {
 67:     cout << endl;
 68:     cout << setw(indentLevel) << ""
 69:          << "This sample program produced by Your_Name_Goes_Here.";
 70:     cout << endl;
 71: }


 74: void DescribeProgram()
 75: // Pre:  none
 76: // Post: A program description has been displayed.
 77: {
 78:     cout << endl;
 79:     cout << "This program determines if a positive "
 80:          << "integer input by the user is palindromic."  << endl;
 81:     cout << endl;
 82: }


 85: void DisplayMainMenu(int indentLevel)
 86: // Pre:  indentLevel contains a nonnegative integer.
 87: // Post: A menu has been displayed, each line indented indentLevel spaces.
 88: {
 89:     cout << endl << endl;
 90:     cout << setw(indentLevel) << "" << "Main Menu ";
 91:     cout << endl << endl;
 92:     cout << setw(indentLevel) << "" << "1. Quit "                 << endl;
 93:     cout << setw(indentLevel) << "" << "2. Get information "      << endl;
 94:     cout << setw(indentLevel) << "" << "3. Check if an integer "
 95:                                     << "is palindromic "          << endl;
 96:     cout << endl << endl;
 97: }


100: void GetMenuChoice(int indentLevel, int& menuChoice)
101: // Pre:  indentLevel contains a nonnegative integer.
102: // Post: A prompt has been displayed, indented indentLevel spaces,
103: //       and menuChoice contains an integer entered by the user.
104: {
105:     cout << setw(indentLevel) << "" << "Enter your choice here: ";
106:     cin >> menuChoice;  cin.ignore(80, '\n');
107:     cout << endl << endl;
108: }


111: void Pause(int indentLevel)
112: // Pre:  The standard input stream cin is empty, and indentLevel 
113: //       contains a nonnegative integer.
114: // Post: A prompt has been displayed, indented indentLevel spaces,
115: //       and the user has pressed <RETURN>.
116: {
117:     char newlineChar;
118:     cout << setw(indentLevel) << "Press <RETURN> to continue ... ";
119:     cin.get(newlineChar);
120:     cout << endl;
121: }


124: bool IsAPalindrome(/* in */ int n)
125: // Pre:  "n" has been initialized with a positive integer
126: // Post: Return-value is true is "n" is palindromic, false otherwise.
127: {
128:     if (n < 10)
129:         return true;
130:     else if (n < 100)
131:         return (n / 10) == (n % 10);
132:     else
133:     {
134:         int firstDigit;
135:         int centerDigits;
136:         int lastDigit;

138:         int divisor = 1;
139:         while (n / divisor >= 10) divisor *= 10;

141:         firstDigit   = n / divisor;
142:         lastDigit    = n % 10;
143:         centerDigits = (n % divisor) / 10;

145:         return (firstDigit == lastDigit) && IsAPalindrome(centerDigits);
146:     }
147: }