www.gusucode.com > 一个VC++数组排序算法源代码示例源码程序 > 一个VC++数组排序算法源代码示例源码程序/code/sortmain.cpp

    /*
	Author:	David Martinjak
	Date:	December, 2001
	Email:	martinfd@muohio.edu
	Download by http://www.NewXing.com
	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 <stdlib.h>
#include <iostream.h>
#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 == LONG) {	
		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<long> 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);
		}
*/

		s.SelectSortType(sortmenu(), l);

		for (i = 0; i < num; i++) {
			cout << l[i] << ' ';
		}

		cout << endl;
		delete [] l;				// return allocated memory to system
	}

	else if (choice == DOUBLE) {
		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<double> 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);
		}
*/
		s.SelectSortType(sortmenu(), d);

		for (i = 0; i < num; i++) {
			cout << d[i] << ' ';
		}

		cout << endl;
		delete [] d;				// return allocated memory to system
	}


	else if (choice == INTERGER) {
		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<int> 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);
		}
*/
		s.SelectSortType(sortmenu(), 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 << "\t0.  long" << endl;
	cout << "\t1.  double" << endl;
	cout << "\t2.  integer" << endl << endl;
	cout << "> What type (0 - 2):  ";
	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 << "\t0.  bubble" << endl;
	cout << "\t1.  insertion" << endl;
	cout << "\t2.  quick" << endl;
	cout << "\t3.  selection" << endl << endl;
	cout << "> What type (0 - 3):  ";
	cin >> choice;
	cout << endl << endl;

	return choice;
}