Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <deque>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <iterator>
  6. using namespace std;
  7.  
  8. #define clrscr() system("clear");
  9.  
  10. const int ciQuit = 7;
  11. const string strMenu =
  12.     "\n 1. Add a string to the end of the vector"
  13.     "\n 2. Display AND remove the first element of the array"
  14.     "\n 3. Display AND remove the last element of the array"
  15.     "\n 4. Display the number of elements in the vector"
  16.     "\n 5. Print the contents of the vector "
  17.     "\n 6. Print the contents of the vector backwards"
  18.     "\n 7. Quits > ";
  19.  
  20. void pause_display()
  21.     {
  22.     cout << "Press Enter to continue!" << endl;
  23.     cin.get();
  24.     }
  25.    
  26. bool check_entry(int iEntry, int iFirst, int iLast)
  27.     {
  28.     return iEntry >= iFirst && iEntry <= iLast ? true : false;
  29.     }
  30.    
  31. int get_menu(const string& strMenu, int iFirst, int iLast)
  32.     {
  33.     int iInput = -1;
  34.     bool bContinue = false;
  35.     do {
  36.         clrscr();
  37.         bContinue = false;
  38.         iInput = -1;
  39.         cout << strMenu;
  40.         cin >> iInput;
  41.         while(cin.get() != '\n');
  42.         bContinue = check_entry(iInput, iFirst, iLast);
  43.         if (!bContinue) cout << "Cannot process >>>" << iInput << "<<<" << endl;
  44.         } while(!bContinue);
  45.     return iInput;
  46.     }  
  47. void get_string(string& strGet)
  48.     {
  49.     cout << endl << "   Enter string > ";
  50.     getline(cin, strGet);
  51.     }
  52.    
  53. void perform(int iMenu, deque<string> &v)
  54.     {
  55.     string strEntry;
  56.     if (!(iMenu == 1 || iMenu == ciQuit)) {
  57.         if (v.size() == 0) {
  58.             cout << "The vector has no elemements!" << endl;
  59.             pause_display();
  60.             return;
  61.             }
  62.         }
  63.     switch(iMenu) {
  64.         case 1:
  65.             // cout << "Add a string to the end of the vector. " << endl;
  66.             get_string(strEntry);
  67.             v.push_back(strEntry);
  68.             break;
  69.         case 2:
  70.             //cout << "Display and remove the first element of the vector. " << endl;
  71.             cout <<v[0] << endl;
  72.             v.pop_front();
  73.             break;
  74.         case 3:
  75.             cout <<v[v.size() - 1] << endl;
  76.             v.pop_back();
  77.             // cout << "Display and remove the last element of the vector. " << endl;
  78.             break;
  79.         case 4:
  80.             //cout << "Display the number of elements in the vector. " << endl;
  81.             cout << "The vector has " << v.size() << " elements!" << endl;
  82.             break;
  83.         case 5:
  84.             // cout << "Print the contents of the vector. " << endl;
  85.             std::copy(v.begin(),v.end(),std::ostream_iterator<string>(std::cout,"\n"));
  86.             break;
  87.         case 6:
  88.             // cout << "Print the contents of the vector backwards. " << endl;
  89.             std::copy(v.rbegin(),v.rend(),std::ostream_iterator<string>(std::cout,"\n"));
  90.             break;
  91.         default:
  92.             break;
  93.         }
  94.     if (!(iMenu == 1 || iMenu == ciQuit)) {
  95.         pause_display();
  96.         }
  97.     return;
  98.     }  
  99.    
  100. int main()
  101.     {
  102.     deque<string> v;
  103.     int iInput = 0;
  104.    
  105.     while (iInput != ciQuit) {
  106.         iInput = get_menu(strMenu, 1, ciQuit);
  107.         perform(iInput, v);
  108.         }
  109.     return 0;
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement