Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.145.105.149
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/assignment5b.cpp
//      Miles Long
//      Instructor: Dr. Wang
//      3/31/08
//      filename: assignment5.cpp
//      Program that allows you to insert, sort, find, and print strings.
//

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX=100;

class List
{ 
	int length;
	string data[MAX];

public:
	List() // empty
	{ length = 0; }
	int Length() const
	{ return length; }
	bool IsFull() const
	{ return ( length == MAX ); }
	bool IsEmpty() const
	{ return ( length == 0 ); }
	void Print() const // as an array
	{
		for ( int k=0; k<length; k++ )
		cout << data[k] << ' ';
		cout << endl;
	}

	void Insert ( string item ) // to the last
	{ 
	  	data[ length ] = item;
	  	length ++;
	}
	bool IsPresent ( string item ) const
	{
		bool done = false;
		for ( int k=0; k<length && ! done; k++ )
			if ( data[k] == item )
				done = true;
		return done;
	}
	void SelSort ( );

};

	// implementation
	void List :: SelSort ( )

	{ 
		string 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.assign( data[minIndex]);
		data[minIndex].assign(data[passCount]);
		data[passCount].assign(temp);
	}
}
// function prototypes
void menu ();
void process ( char, List& );

int main()
{	
	List myList;	//empty object
	char menu_int;
	string garbage; 


	menu(); // read first data
	cin >> menu_int; // ch: 1, 2, 3, 4, 5
	getline( cin, garbage ); // get rid of the newline character in the buffer
	while ( menu_int != '5' )
	{
		process(menu_int, myList);
		menu(); // read next data
		cin >> menu_int; // ch: 1, 2, 3, 4
		getline( cin, garbage ); // get rid of the newline
	}

	cout << "Press any key to exit the program. " << endl;
	getchar();
	return 0;
}

void menu ()
{
	cout << "1 insert a string\n";
	cout << "2 find a string\n";
	cout << "3 print the list\n";
	cout << "4 sort the list\n";
	cout << "5 quit the operation\n";
	cout << "\nYou choose: ";
}

void process ( char menu_int, List& myList )
{

	string input_string;
	switch ( menu_int )
		{ 		
			case '1':
				cout <<"Enter string to insert: ";
				//getline(cin, input_string);
				cin >> input_string;
				myList.Insert(input_string);
				// for debug
				cout << "The list is: ";
				myList.Print();
				cout << "Type any key to continue ..";
				getchar();
				break;
			case '4':
				myList.SelSort();
				// for debug
				cout << "The list is: ";
				myList.Print();
				cout << "\n\nType any key to continue ..";
				getchar();
				break;
			case '3':
				cout << "The list is: ";
				myList.Print();
				cout << "\n\nType any key to continue ..";
				getchar();
				break;
			case '2':
				cout << "Input string to be found: ";
				//getline(cin, input_string);
				cin >> input_string;
				if ( myList.IsPresent(input_string) )
				cout << input_string << " is in the list.\n";
			else
				cout << input_string
					<< " isn't in the list.\n";
				cout << "\n\nType any key to continue ..";
				getchar();
				break;
			default:
				cout << "\nInvalid input.\n" ;
				cout << "\n\nType any key to continue ..";
				getchar();
	}
	return;
}

Stv3n404 - 2023