Server IP : 172.16.15.8 / Your IP : 18.217.246.148 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/grpatillo/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
/* Author: R.P. Patillo Instructor: Dr. Wang Due Date: March 26, 2008 Compilation: g++ hw5_212.cpp -o hw5_212.out Execution: ./hw5_212.out Goal: This program will perform the following operations using a loop: 1) insert a string; 2) find a string; 3) print the string list; 4) sort the list to ascending order; 5) quit */ #include <iostream> using namespace std; const int MAX = 100; class List { string str; int length; string data[MAX]; // assuming: integer list public: List() // empty: length = 0 { length = 0; } int Length() { return length; } bool IsEmpty() { return length == 0; } bool IsFull() const { return length == MAX; } void Insert(string str) { data[length] = str; length++; } bool IsPresent(string str) const; // prototype void SelSort(); // prototype void Print() const { for(int i=0; i<length; i++) cout << data[i] << "\n"; cout << endl; } }; bool List :: IsPresent(string str) const { bool done = false; for(int i=0; i<length&&!done; i++) if( str == data[i]) done = true; return done; } void List :: SelSort() { string temp; int passCount; int minIndex; for (passCount=0; passCount<length-1; passCount ++) { minIndex = passCount; for (int k=passCount+1; k<length; k++) { if (data[minIndex] > data[k] ) minIndex = k; } temp = data[minIndex]; data[minIndex] = data[passCount]; data[passCount] = temp; } } int main() { List list1; string str; char op; cout << "\n*********************************\n" << endl; cout << "Input a string:" << endl; cin >> str; list1.Insert(str); cout << "\n*********************************\n" << endl; cout << "Input I to input another string... \nInput F to find a string... \nInput P to print out your current string list... \nInput S to sort your current string list in ascending order... \nInput Q to quit... \n-------------------\nYour command:\t"; cin >> op; while(op != 'Q' || op != 'q') { if (op == 'I' || op == 'i') { cout << "\n*********************************\n" << endl; cout << "Input another string:" << endl; cin >> str; list1.Insert(str); } else if(op == 'F' || op == 'f') { cout << "\n*********************************\n" << endl; cout << "Input the string you want to be found within your list:" << endl; cin >> str; if(list1.IsPresent(str)) cout << "\nThat string is within the list!!!\n"; else cout << "\nThat string is not within the list.\n"; } else if (op == 'P' || op == 'p') { cout << "\n*********************************\n" << endl; cout << "The current list of strings is: " << endl; list1.Print(); } else if (op == 'S' || op == 's') { cout << "\n*********************************\n" << endl; list1.SelSort(); cout << "The sorted list is: " << endl; list1.Print(); } else if (op == 'Q' || op == 'q') break; else { cout << "\n*********************************\n" << endl; cout << "Please input a valid command: {F to find, P to print, S to sort or Q to quit}" << endl; cin >> op; } cout << "\n*********************************\n" << endl; cout << "Input I to input another string... \nInput F to find a string... \nInput P to print out your current string list... \nInput S to sort your current string list in ascending order... \nInput Q to quit... \n-------------------\nYour command:\t"; cin >> op; } cout << "\n\nThanks for playing!!!\n\n"; return 0; }