Server IP : 172.16.15.8 / Your IP : 3.144.114.8 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/wcdavis/../jmsanchez/../grpatillo/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
/* File Name: hw8b_212.cpp Author: R.P. Patillo Instructor: Dr. Wang Due Date: April 2, 2008 Compilation: g++ hw8b_212.cpp -o hw8b_212.out Execution: ./hw8b_212.out Goal: This program will initiate "The List Game." */ #include <iostream> using namespace std; void menu(); void menuForList (); template<class T> void process(); const int MAX = 100; template<class T> class List { int length; T data[MAX]; public: List () { length = 0; } int Length ( ) const { return length; } void Insert ( T item ) { data[ length ] = item; length ++; } void Delete () { length --; } bool Erase (T item) { T temp; bool done = false; for(int i=0; i<length&&!done; i++) { if ( item == data[i]) { for(int tempIndex=i; length>tempIndex; tempIndex++) { data[tempIndex] = data[tempIndex+1]; temp = data[tempIndex]; done = true; } } } } bool IsPresent( T item ) const { bool done = false; for(int i=0; i<length&&!done; i++) if( item == data[i]) done = true; return done; } void Sort() { T 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; } } void Print() const { for(int k=0; k<length; k++) cout << "\t" << data[k] << endl; } void Empty() { length = 0; } }; int main() { char ch; menu(); cin >> ch; while(tolower(ch) != 'q') { switch (tolower(ch)) { case 'i': cout << "\n\n\n\tNow play with the integer list ... "; process<int>(); break; case 'f': cout << "\n\n\n\tNow play with the float list ... "; process<float>(); break; case 'c': cout << "\n\n\n\tNow play with the char list ... "; process<char>(); break; case 's': cout << "\n\n\n\tNow play with the string list ... "; process<string>(); break; default: cout << "\n\tInvalid, redo it\n\n"; } menu(); cin >> ch; } cout << "\n\n\tThanks for playing!!!\n\n"; return 0; } //--------------------------------- template<class T> void process() { char selection; List<T> z; T item; menuForList (); cin >> selection; while(tolower(selection) != 'q') { switch (tolower(selection)) { case 'i': cout << "\n\t***********************************\n"<< endl; cout << "\tInput an item: "; cin >> item; z.Insert(item); break; case 'd': cout << "\n\t***********************************\n"<< endl; cout << "\tThe last item has been deleted. \n"; z.Delete(); break; case 'e': cout << "\n\t***********************************\n"<< endl; cout << "\tInput the item you wanted erased: "; cin >> item; z.Erase(item); break; case 'f': cout << "\n\t***********************************\n"<< endl; cout << "\tInput the item you want to be found in the list: "; cin >> item; if(z.IsPresent(item)) cout << "\n\tThat item is within the list!!!\n"; else cout << "\n\tThat item is not within the list.\n"; break; case 'x': cout << "\n\t***********************************\n"<< endl; cout << "\n\tThe list has been completely emptied!!!\n"; z.Empty(); break; case 's': cout << "\n\t***********************************\n"<< endl; cout << "\n\tThe list sorted is: \n"; z.Sort(); z.Print(); break; case 'p': cout << "\n\t***********************************\n"<< endl; cout << "\n\tThe list is: \n"; z.Print(); break; default: cout << "\n\n\tInvalid. Try again ...\n\n"; } menuForList (); cin >> selection; } } //--------------------------------- void menu() { cout << "\n\n\t--- Welcome to the List Game! ---\n"; cout << "\t ========================\n\n"; cout << "\tI Integers\n"; cout << "\tF Floats\n"; cout << "\tC Characters\n"; cout << "\tS General strings\n"; cout << "\tQ Quit\n"; cout << "\t--------------------\n"; cout << "\tYour choice: "; } //---------------------------------- void menuForList () { cout << "\n\t==========================================\n\n" <<endl; cout << "\t I to insert an item to the list...\n"; cout << "\t D to delete the last item...\n"; cout << "\t E to erase a specific item...\n"; cout << "\t F to find a specific item...\n"; cout << "\t X to completely empty the list...\n"; cout << "\t S to sort the list...\n"; cout << "\t P to print the list...\n"; cout << "\t Q to quit the operation...\n"; cout << "\t-------------------- \n\tYou choose: "; }