Advertisement
Guest User

sequence2.cpp

a guest
Feb 11th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. // FILE: sequence_test.cpp
  2. // An interactive test program for the new sequence class
  3. #include <cctype>       // Provides toupper
  4. #include <iostream>     // Provides cout and cin
  5. #include <cstdlib>      // Provides EXIT_SUCCESS
  6. #include "sequence2.h"  // With value_type defined as double
  7. using namespace std;
  8. using namespace CISP430_A2;
  9.  
  10. // PROTOTYPES for functions used by this test program:
  11. void print_menu( );
  12. // Postcondition: A menu of choices for this program has been written to cout.
  13.  
  14. char get_user_command( );
  15. // Postcondition: The user has been prompted to enter a one character command.
  16. // The next character has been read (skipping blanks and newline characters),
  17. // and this character has been returned.
  18.  
  19. void show_sequence(sequence display);
  20. // Postcondition: The items on display have been printed to cout (one per line).
  21.  
  22. double get_number( );
  23. // Postcondition: The user has been prompted to enter a real number. The
  24. // number has been read, echoed to the screen, and returned by the function.
  25.  
  26.  
  27. int main( )
  28. {
  29.     sequence test; // A sequence that we’ll perform tests on
  30.     char choice;   // A command character entered by the user
  31.    
  32.     cout << "I have initialized an empty sequence of real numbers." << endl;
  33.  
  34.     do
  35.     {
  36.         print_menu( );
  37.         choice = toupper(get_user_command( ));
  38.         switch (choice)
  39.         {
  40.             case '!': test.start( );
  41.                       break;
  42.             case '+': test.advance( );
  43.                       break;
  44.             case '?': if (test.is_item( ))
  45.                           cout << "There is an item." << endl;
  46.                       else
  47.                           cout << "There is no current item." << endl;
  48.                       break;
  49.             case 'C': if (test.is_item( ))
  50.                            cout << "Current item is: " << test.current( ) << endl;
  51.                       else
  52.                           cout << "There is no current item." << endl;
  53.                       break;
  54.             case 'P': show_sequence(test);
  55.                       break;
  56.             case 'S': cout << "Size is " << test.size( ) << '.' << endl;
  57.                       break;
  58.             case 'I': test.insert(get_number( ));
  59.                       break;
  60.             case 'A': test.attach(get_number( ));
  61.                       break;
  62.             case 'R': test.remove_current( );
  63.                       cout << "The current item has been removed." << endl;
  64.                       break;    
  65.             case 'Q': cout << "Ridicule is the best test of truth." << endl;
  66.                       break;
  67.             default:  cout << choice << " is invalid." << endl;
  68.         }
  69.     }
  70.     while ((choice != 'Q'));
  71.  
  72.     return EXIT_SUCCESS;
  73. }
  74.  
  75. void print_menu( )
  76. // Library facilities used: iostream.h
  77. {
  78.     cout << endl; // Print blank line before the menu
  79.     cout << "The following choices are available: " << endl;
  80.     cout << " !   Activate the start( ) function" << endl;
  81.     cout << " +   Activate the advance( ) function" << endl;
  82.     cout << " ?   Print the result from the is_item( ) function" << endl;
  83.     cout << " C   Print the result from the current( ) function" << endl;
  84.     cout << " P   Print a copy of the entire sequence" << endl;
  85.     cout << " S   Print the result from the size( ) function" << endl;
  86.     cout << " I   Insert a new number with the insert(...) function" << endl;
  87.     cout << " A   Attach a new number with the attach(...) function" << endl;
  88.     cout << " R   Activate the remove_current( ) function" << endl;
  89.     cout << " Q   Quit this test program" << endl;
  90. }
  91.  
  92. char get_user_command( )
  93. // Library facilities used: iostream
  94. {
  95.     char command;
  96.  
  97.     cout << "Enter choice: ";
  98.     cin >> command; // Input of characters skips blanks and newline character
  99.  
  100.     return command;
  101. }
  102.  
  103. void show_sequence(sequence display)
  104. // Library facilities used: iostream
  105. {
  106.     for (display.start( ); display.is_item( ); display.advance( ))
  107.         cout << display.current( ) << endl;
  108. }
  109.  
  110. double get_number( )
  111. // Library facilities used: iostream
  112. {
  113.     double result;
  114.    
  115.     cout << "Please enter a real number for the sequence: ";
  116.     cin  >> result;
  117.     cout << result << " has been read." << endl;
  118.     return result;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement