Server IP : 172.16.15.8 / Your IP : 3.135.206.212 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: April 2, 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 full name; 2) find a name; 3) print the name list; 4) sort the list to ascending order; 5) quit */ #include <iostream> using namespace std; const int MAX = 100; class List { string first[MAX]; string last[MAX]; string middle[MAX]; int length; 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 f, string m, string l) { first[length] = f; middle[length] = m; last[length] = l; length++; } bool IsPresent(string str) const; void SelSort(); void Print() const { for(int i=0; i<length; i++) cout << first[i] << " " << middle[i] << " " << last[i] << "\n"; cout << endl; } }; bool List :: IsPresent(string str) const { bool done = false; for(int i=0; i<length&&!done; i++) if( str == first[i]) { cout << "\nIt's the first name of: " << first[i] << " " << middle[i] << " " << last[i] << endl; } else if( str == middle[i]) { cout << "\nIt's the middle name of: " << first[i] << " " << middle[i] << " " << last[i] << endl; } else if( str == last[i]) { cout << "\nIt's the last name of: " << first[i] << " " << middle[i] << " " << last[i] << endl; } for(int i=0; i<length&&!done; i++) if( str == first[i]) done = true; else if( str == middle[i]) done = true; else if( str == last[i]) done = true; return done; } void List :: SelSort() { string temp; string temp3; string temp2; int passCount; int minIndex; for (passCount=0; passCount<length-1; passCount ++) { minIndex = passCount; for (int k=passCount+1; k<length; k++) { if (last[minIndex] == last[k] ) for ( int z=passCount+1; z<length; z++) { if (first[minIndex] > first[k] ) minIndex = k; } else if (last[minIndex] > last[k] ) minIndex = k; } temp = first[minIndex]; temp2 = middle[minIndex]; temp3 = last[minIndex]; last[minIndex] = last[passCount]; first[minIndex] = first[passCount]; middle[minIndex] = middle[passCount]; last[passCount] = temp3; first[passCount] = temp; middle[passCount] = temp2; } } int main() { List list1; string f, m, l, str; char op; cout << "\n*********************************\n" << endl; cout << "Input a full name (first name middle intial & last name):" << endl; cin >> f >> m >> l; list1.Insert(f, m, l); cout << "\n*********************************\n" << endl; cout << "Input I to input another full name... \nInput F to find a name... \nInput P to print out your current name list... \nInput S to sort your current name 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 full name:" << endl; cin >> f >> m >> l; list1.Insert(f, m, l); } else if(op == 'F' || op == 'f') { cout << "\n*********************************\n" << endl; cout << "Input the name you want to be found within your list:" << endl; cin >> str; if(list1.IsPresent(str)) cout << "\nThat name is within the list!!!\n"; else cout << "\nThat name is not within the list.\n"; } else if (op == 'P' || op == 'p') { cout << "\n*********************************\n" << endl; cout << "The current list of names 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: {I to insert, 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 full name... \nInput F to find a name... \nInput P to print out your current name list... \nInput S to sort your current name list in ascending order... \nInput Q to quit... \n-------------------\nYour command:\t"; cin >> op; } cout << "\n\nThanks for playing!!!\n\n"; return 0; }