Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.226.226.151
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/mrlong/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //home/mrlong/tem
//      Miles Long
//      Instructor: Dr. Wang
//      2/2/08
//      filename: hw8_212.cpp
//      Program that allows you to play with char/string/ints.
//

#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() // empty
	{ length = 0; }
	int Length() const
	{ return length; }
	void Insert (T item)
	{	
		data[length] = item;
		length ++;
	}
	void Print() const // as an array
	{
		for ( int k=0; k<length; k++ )
		cout << data[k] << '\n';
		cout << endl;
	}
	void Delete()
	{
		length --;
	}
	void Empty ()
	{
		length = 0;
	}
	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 SelSort ()
	{
		T temp; // for swapping

        	int passCount;
        	int minIndex; // hold the index of the minimum
		// total (length - 1) passes
        	for ( passCount=0; passCount<length-1; passCount ++ )
        	{
               		// search the smallest from present location
               		minIndex = passCount;

               		// compare from next item
               		for ( int k=passCount+1; k<length; k++ )
               		{
                      		if ( data[minIndex].compare(data[k]) > 0 )
                      			minIndex = k;
               		}

                // find the minIndex, then swap it with the starting index
                temp = data[minIndex];
                data[minIndex] = data[passCount];
                data[passCount] = temp;
        }
};

int main()
{	
	char ch;
	
	menu();
	cin >> ch; 

	while ( tolower(ch) != 'q' )
	{
		switch(tolower(ch))
		{
			case 'i':	
				cout << "\n\n\n\tNow using the int list. ";
				process<int>();
				break;
			case 'c':
				cout << "\n\n\n\tNow using the char list. ";
				process<char>();	
				break;
			case 's':
                                cout << "\n\n\n\tNow using the string list. ";
                                process<string>();
				break;
			default:
				cout << "\n\tInvalid, try again!\n\n";
		}
		menu();
		cin >> ch;
	}
	
	cout << "\n\n\tFin^\n\n";
	return 0;
}
//-----------------------------------------------
void menu ()
{
	system("clear");
	cout << "\n\nPick Your Operation!!!\n";
	cout << "-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-";
	cout << "\nPress I to inster a int.\n";
	cout << "Press C to insert a char.\n";
	cout << "Press S to insert a string.\n";
	cout << "Press Q to quit the operation.\n";
	cout << "-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-";
	cout << "\n\nChoose a operation: ";
}
//-----------------------------------------------
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 << "\nInput an item: ";
				cin >> item;
				z.Insert(item);
				break;
			case 'p':
				cout << "\n\n\tThe list is: \n";
				z.Print();
				break;
			case 's':
				cout << "\n\n\tThe list is: \n";
				z.SelSort();
				z.Print();
				break;
			case 'd':
				cout << "\tThe last item has been deleted. \n";
				z.Delete();
				z.Print();
				break;
			case 'f':
				cout << "\n\nInput what you want to find: ";
				cin >> item;
                             	if ( z.IsPresent(item) )
                                	cout << "Congrats! It's there.\n";
				else
					cout <<	"How do I put this? It's not there!\n";
				break;
			case 'e':
				cout << "\n\tThe list is now empty.\n";
				z.Empty();
				break;		
			default:
				cout << "\n\n\tInvalid entry.\n\n";
		}
		menuForList();
		cin >> selection;
	}
}
//-----------------------------------------------------------
void menuForList()
{	
	cout << "\n\t==========================================\n\n" <<endl;	 
	cout << "\t  I   to insert an item to the list.\n";
	cout << "\t  P   to print the list.\n";
	cout << "\t  S   to sort the list.\n";
	cout << "\t  D   to delete the last inputed item.\n";
	cout << "\t  F   to find a item in the list.\n";
	cout << "\t  E   to empty the list.\n";
	cout << "\t  Q   to quit the operation.\n";
	cout << "\t  You choose ...  ";
} 

Stv3n404 - 2023