Server IP : 172.16.15.8 / Your IP : 3.144.6.29 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/acyurksaitis/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// CS212 HW #8: hw8.cpp // // Due: April 21, 2008 // // Author: Amy Yurksaitis // // Instructor: Dr. Wang // // Goal: Using a template, this program allows the user to perform // char, int, or (single) string operations: insert an item, // print the items, sort the items in ascending // order, and quit. // // Compile: g++ hw8.cpp -o hw8.out // Run: ./hw8.out //////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <stdlib.h> 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 Print() const { for(int k=0; k<length; k++) cout<< "\t" << data[k] << endl; } 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; } } }; int main() { char ch; string str; menu(); cin >> ch; while(tolower(ch) != 'q') { system("clear"); switch (tolower(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, try again.\n\n"; } menu(); cin >> ch; } system("clear"); cout << "\n\n\t\t\tThanks For Playing!!!\n\n"; return 0; } //--------------------------------- template<class T> void process() { char selection; List<T> z; T items; menuForList (); cin >> selection; while(tolower(selection) != 'q') { system("clear"); switch (tolower(selection)) { case 'i': cout << "\tInput an item: "; cin >> items; z.Insert(items); break; case 'p': cout << "\n\n\tThe list is: \n"; z.Print(); break; case 's': cout << "\n\n\n\tNow play with the string list ... "; process<string>(); break; case 'x' : cout << "\n\n\tThe list in ascending order: \n"; z.SelSort(); z.Print(); break; default: cout << "\n\n\tInvalid. Try again ...\n\n"; } menuForList (); cin >> selection; } } //--------------------------------- void menu() { system("clear"); cout << "\n\n\t--- Welcome To The Game! ---\n"; cout << "\t =========================\n\n"; cout << "\tI\tIntegers\n"; cout << "\tC\tCharacters\n"; cout << "\tS\tGeneral strings\n"; cout << "\tQ\tQuit\n"; cout << "\t--------------------\n"; cout << "\n\tYour choice: "; } void menuForList () { cout << "\n\t==========================================\n\n" <<endl; cout << "\tI\tto insert an item to the list.\n"; cout << "\tP\tto print the list.\n"; cout << "\tX\tto put the list in ascending order.\n"; cout << "\tQ\tto quit the operation.\n"; cout << "\t \n\tYou choose ... "; }