Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <deque>
- #include <cstdlib>
- #include <string>
- #include <iterator>
- using namespace std;
- #define clrscr() system("clear");
- const int ciQuit = 7;
- const string strMenu =
- "\n 1. Add a string to the end of the vector"
- "\n 2. Display AND remove the first element of the array"
- "\n 3. Display AND remove the last element of the array"
- "\n 4. Display the number of elements in the vector"
- "\n 5. Print the contents of the vector "
- "\n 6. Print the contents of the vector backwards"
- "\n 7. Quits > ";
- void pause_display()
- {
- cout << "Press Enter to continue!" << endl;
- cin.get();
- }
- bool check_entry(int iEntry, int iFirst, int iLast)
- {
- return iEntry >= iFirst && iEntry <= iLast ? true : false;
- }
- int get_menu(const string& strMenu, int iFirst, int iLast)
- {
- int iInput = -1;
- bool bContinue = false;
- do {
- clrscr();
- bContinue = false;
- iInput = -1;
- cout << strMenu;
- cin >> iInput;
- while(cin.get() != '\n');
- bContinue = check_entry(iInput, iFirst, iLast);
- if (!bContinue) cout << "Cannot process >>>" << iInput << "<<<" << endl;
- } while(!bContinue);
- return iInput;
- }
- void get_string(string& strGet)
- {
- cout << endl << " Enter string > ";
- getline(cin, strGet);
- }
- void perform(int iMenu, deque<string> &v)
- {
- string strEntry;
- if (!(iMenu == 1 || iMenu == ciQuit)) {
- if (v.size() == 0) {
- cout << "The vector has no elemements!" << endl;
- pause_display();
- return;
- }
- }
- switch(iMenu) {
- case 1:
- // cout << "Add a string to the end of the vector. " << endl;
- get_string(strEntry);
- v.push_back(strEntry);
- break;
- case 2:
- //cout << "Display and remove the first element of the vector. " << endl;
- cout <<v[0] << endl;
- v.pop_front();
- break;
- case 3:
- cout <<v[v.size() - 1] << endl;
- v.pop_back();
- // cout << "Display and remove the last element of the vector. " << endl;
- break;
- case 4:
- //cout << "Display the number of elements in the vector. " << endl;
- cout << "The vector has " << v.size() << " elements!" << endl;
- break;
- case 5:
- // cout << "Print the contents of the vector. " << endl;
- std::copy(v.begin(),v.end(),std::ostream_iterator<string>(std::cout,"\n"));
- break;
- case 6:
- // cout << "Print the contents of the vector backwards. " << endl;
- std::copy(v.rbegin(),v.rend(),std::ostream_iterator<string>(std::cout,"\n"));
- break;
- default:
- break;
- }
- if (!(iMenu == 1 || iMenu == ciQuit)) {
- pause_display();
- }
- return;
- }
- int main()
- {
- deque<string> v;
- int iInput = 0;
- while (iInput != ciQuit) {
- iInput = get_menu(strMenu, 1, ciQuit);
- perform(iInput, v);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement