Posted By

DrPepper on 10/05/11


Tagged


Versions (?)

[CISP430] pqtest.cpp


 / Published in: C++
 

  1. // FILE: pqtest.cpp
  2. // An interactive test program for the new List ADT.
  3. #include <ctype.h> // Provides toupper
  4. //#include <iostream.h> // Provides cout and cin
  5. #include <stdlib.h> // Provides EXIT_SUCCESS and size_t
  6. #include "pqueue1.h" // With Item defined as int
  7.  
  8. #include <iostream>
  9.  
  10. using std::cout;
  11. using std::cin;
  12. using std::endl;
  13. // using std::flush;
  14.  
  15. // PROTOTYPES for functions used by this test program:
  16. void print_menu( );
  17. // Postcondition: A menu of choices for this program has been written to cout.
  18.  
  19. char get_user_command( );
  20. // Postcondition: The user has been prompted to enter a one character command.
  21. // The next character has been read (skipping blanks and newline characters),
  22. // and this character has been returned.
  23.  
  24. int get_number(const char message[ ]);
  25. // Postcondition: The user has been prompted to enter an integer. The
  26. // number has been read, echoed to the screen, and returned by the function.
  27.  
  28.  
  29. int main( )
  30. {
  31. PriorityQueue test; // A PriorityQueue that we'll perform tests on
  32. char choice; // A command character entered by the user
  33.  
  34. cout << "I have initialized an empty Priority Queue." << endl;
  35. cout << "The data entered into this Priority Queue will be integers,\n";
  36. cout << "and each item also has an unsigned int for its priority." << endl;
  37.  
  38. do
  39. {
  40. print_menu( );
  41. choice = toupper(get_user_command( ));
  42. switch (choice)
  43. {
  44. case 'E': if (test.is_empty( ))
  45. cout << "The Priority Queue is empty." << endl;
  46. else
  47. cout << "The Priority Queue is not empty." << endl;
  48. break;
  49. case 'G': if (!test.is_empty( ))
  50. cout << "Front item is: " << test.get_front( ) << endl;
  51. else
  52. cout << "There is no current item." << endl;
  53. break;
  54. case 'I': test.insert(
  55. get_number("Please enter the next item: "),
  56. (unsigned int) get_number("The item's priority: ")
  57. );
  58. break;
  59. case 'S': cout << "The size is " << test.size( ) << endl;
  60. break;
  61. case 'Q': cout << "Ridicule is the best test of truth." << endl;
  62. break;
  63. default: cout << choice << " is invalid. Sorry." << endl;
  64. }
  65. }
  66. while ((choice != 'Q'));
  67.  
  68. return EXIT_SUCCESS;
  69. }
  70.  
  71. void print_menu( )
  72. // Library facilities used: iostream.h
  73. {
  74. cout << endl; // Print blank line before the menu
  75. cout << "The following choices are available: " << endl;
  76. cout << " E Print the result from the is_empty( ) function" << endl;
  77. cout << " G Print the result from the get_front( ) function" << endl;
  78. cout << " I Insert a new item with the insert(...) function" << endl;
  79. cout << " S Print the result from the size( ) function" << endl;
  80. cout << " Q Quit this test program" << endl;
  81. }
  82.  
  83. char get_user_command( )
  84. // Library facilities used: iostream.h
  85. {
  86. char command;
  87.  
  88. cout << "Enter choice: ";
  89. cin >> command; // Input of characters skips blanks and newline character
  90.  
  91. return command;
  92. }
  93.  
  94. int get_number(const char message[ ])
  95. // Library facilities used: iostream.h
  96. {
  97. int result;
  98.  
  99. cout << message << endl;
  100. cin >> result;
  101. cout << result << " has been read." << endl;
  102. return result;
  103. }

Report this snippet  

You need to login to post a comment.