www.pudn.com > ArraySort.zip > sortmain.cpp


/* 
	Author:	David Martinjak 
	Date:	December, 2001 
	Email:	martinfd@muohio.edu 
 
	This is a driver program I wrote to implement 
	the usage of the sort class header file.  Your 
	needs and intentions may differ from the results 
	of this source code. 
 
	This software is open source code, and is provided 
	as-is with no warranties whatsoever.  Please feel 
	free to make modifications and use for your own 
	intents and purposes. 
 
  */ 
 
 
#include  
#include  
#include "sort.h" 
 
int typemenu(int &n); 
int sortmenu(); 
 
void main() { 
 
	int i = 0, num = 0, choice = 0; 
	choice = typemenu(num);	// find what type of data to store 
 
 
	if (choice == 1) {	 
		long in;					// used to store data in the array 
		long * l = new long [num];	// dynamically create the array 
		cout << endl << endl; 
		for (i = 0; i < num; i++) {	// get user input for array elements 
			cout << "element " << i + 1 << ":  "; 
			cin >> in; 
			l[i] = in; 
		} 
 
		sort s(num);			// create sort object 
		choice = sortmenu();		// determine which sort to use 
 
		if (choice == 1) { 
			s.bubble(l); 
		 
		} else if (choice == 2) { 
			s.insertion(l); 
 
		} else if (choice == 3) { 
			s.quick(l, 0, num - 1); 
 
		} else 
		{ 
			s.selection(l); 
		} 
 
		for (i = 0; i < num; i++) { 
			cout << l[i] << ' '; 
		} 
 
		cout << endl; 
		delete [] l;				// return allocated memory to system 
	} 
 
	else if (choice == 2) { 
		double in;						// used to store data in the array 
		double * d = new double [num];	// dynamically create the array 
		cout << endl << endl; 
		for (i = 0; i < num; i++) {		// get user input for array elements 
			cout << "element " << i + 1 << ":  "; 
			cin >> in; 
			d[i] = in; 
		} 
 
		sort s(num);		// create sort object 
		choice = sortmenu();		// determine which sort to use 
 
		if (choice == 1) { 
			s.bubble(d); 
		 
		} else if (choice == 2) { 
			s.insertion(d); 
 
		} else if (choice == 3) { 
			s.quick(d, 0, num - 1); 
 
		} else 
		{ 
			s.selection(d); 
		} 
 
		for (i = 0; i < num; i++) { 
			cout << d[i] << ' '; 
		} 
 
		cout << endl; 
		delete [] d;				// return allocated memory to system 
	} 
 
 
	else if (choice == 3) { 
		int in;						// used to store array elements 
		int * iA = new int [num];	// dynamically create array 
		cout << endl << endl; 
		for (i = 0; i < num; i++) {	// retrieve array elements 
			cout << "element " << i + 1 << ":  "; 
			cin >> in; 
			iA[i] = in; 
		} 
 
		sort s(num);			// create sort object 
		choice = sortmenu();		// determine which sort to use 
 
		if (choice == 1) { 
			s.bubble(iA); 
		 
		} else if (choice == 2) { 
			s.insertion(iA); 
 
		} else if (choice == 3) { 
			s.quick(iA, 0, num - 1); 
 
		} else 
		{ 
			s.selection(iA); 
		} 
 
		for (i = 0; i < num; i++) { 
			cout << iA[i] << ' '; 
		} 
 
		cout << endl; 
		delete [] iA;				// return allocated memory to system 
	} 
 
 
	else { 
		cerr << "** Your choice was invalid." << endl; 
		exit(-1); 
		 
	} 
 
 
	cout << endl << endl;	// output buffer 
	cin.get(); 
	cin.ignore(); 
 
} 
 
 
 
int typemenu(int &n) { 
	 
	int choice; 
 
	cout << "Enter the type to store:" << endl << endl; 
	cout << "\t1.  long" << endl; 
	cout << "\t2.  double" << endl; 
	cout << "\t3.  integer" << endl << endl; 
	cout << "> What type (1 - 3):  "; 
	cin >> choice; 
 
	cout << "\n> How many elements would you like to store:  "; 
	cin >> n; 
 
	return choice; 
} 
 
 
int sortmenu() { 
 
	int choice; 
 
	cout << endl << endl; 
	cout << "Enter the sort to use:" << endl << endl; 
	cout << "\t1.  bubble" << endl; 
	cout << "\t2.  insertion" << endl; 
	cout << "\t3.  quick" << endl; 
	cout << "\t4.  selection" << endl << endl; 
	cout << "> What type (1 - 4):  "; 
	cin >> choice; 
	cout << endl << endl; 
 
	return choice; 
}