⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortmain.cpp

📁 数组排序算法-----这个程序的头文件中包含四种排序方法:泡沫排序法
💻 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 <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 == 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<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);
		}

		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<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);
		}

		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<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);
		}

		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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -