Server IP : 172.16.15.8 / Your IP : 18.119.122.140 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/cjabbott/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// File name: assign8b.cpp // Author: Christie Abbott // Instructor: Dr. Wang // Due Date: April 16, 2008 // Compiling: g++ assign8b.cpp // Execution: ./a.out // // Goal: The program will allow the user to insert a name, find // a name, print the names in a list, sort the list, and quit. // #include <iostream> using namespace std; const int MAX = 100; template <class T> class List { T data[MAX]; int length; public: List() { length = 0; } int Length() const { return length; } void Insert(T aData) { data[length] = aData; length ++; } void Print() const { for( int k = 0; k < length; k ++ ) cout << data[k] << endl; } bool IsPresent(T aData) const { bool done = false; for( int k =0; k < length &&! done; k ++) if( data[k] == aData) done = true; return done; } void SelSort() { 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 menu(); void menu2(); template<class T> void process(); int main() { char ch; menu(); cin >> ch; while( toupper(ch) != 'Q') { switch ( toupper(ch) ) { case 'I': cout << "\n\n\n\tNow play with the integer list ... "; process<int>(); 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; } return 0; } //-------------------------------------------------------------------- void menu() { cout << "\n\n\t---Welcome!!---"; cout << "\n\t===============\n"; cout << "\tI Integers\n"; cout << "\tC Characters\n"; cout << "\tX Single String\n"; cout << "\n\t Choose a data type: "; } //--------------------------------------------------------------------- void menu2() { cout << "\n\t===============\n"; cout << "\tN Insert\n"; cout << "\tF Find\n"; cout << "\tP Print\n"; cout << "\tR Sort\n"; cout << "\tQ Quit\n"; cout << "\n\t Choose an operation: "; } //--------------------------------------------------------------------- template<class T> void process() { char datum; List<T> c; T item; menu2(); cin >> datum; while( toupper(datum) != 'Q' ) { switch ( toupper(datum) ) { case 'N': cout << "Input a item: "; cin >> item; c.Insert(item); cout << "The list is:\n "; c.Print(); break; case 'R': c.SelSort(); cout << "The list is: "; c.Print(); break; case 'P': cout << "The list is: "; c.Print(); break; case 'F': cout << "Input an item to be found: "; cin >> item; if( c.IsPresent(item) ) cout << item << " is in the list.\n"; else cout << item << " isn't in the list.\n"; break; default: cout << "\nInvalid input.\n"; cout << "\n\nType any key to continue..\n"; getchar(); } menu2(); cin >> datum; } }