C++几种简单排序及类的操作(冒泡,选择,插入)

程序应该是有点小错误!因为在C++2008上可以运行,但在VC6.0上就不能运行,调试了一段时间,但还是没解决,我也不知道为什么。如有知道的朋友帮忙指正下,十分感谢!!!


//"small.h"头文件,类Array的“接口”部分
#include <iostream>
using namespace std;

class Array  //定义类Array
{
public:
	friend void input(Array& num);  //友元函数,提取输入数据

	void get(istream& ins);  //成员函数,提取输入数据

	void BubblingSort1();  //成员函数,冒泡排序

	void BubblingSort2();  //成员函数,冒泡排序

	void ChoiceSort();   //成员函数,选择排序

	void InsertSort();   //成员函数,插入排序

	friend ostream& operator <<(ostream& out,const Array& s);   //重载"<<"操作符

private:
	int a[5];   //私有变量
};

//实现文件"small.cpp",类Array的实现部分
#include <iostream>
#include "small.h"
using namespace std;

void input(Array& num)
{
	cout << "Please input five numbers:" << endl;
	for(int i = 0;i < 5;i++)   //for循环输入数据
		cin >> num.a[i];
}
void Array::get(istream& ins)
{
	cout << "Please input five numbers:" << endl;
	for(int i = 0;i < 5;i++)   //for循环输入数据
		ins >> a[i];
}
void Array::BubblingSort1()
{
	int i,j,temp;
	for(i = 0;i < 5 - 1;i++)     //实现冒泡排序法
		for(j = 0;j < 5 - 1 - i;j++)
			if(a[j] > a[j+1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
}
void Array::BubblingSort2()
{
	int i,j,temp;
	for(i = 0;i < 5 - 1;i++)//实现冒泡排序法
		for(j=5 - i;j > 0;j--)
			if(a[j] > a[j-1])
			{
				temp = a[j];
				a[j] = a[j-1];
				a[j-1] = temp;
			}
}
void Array::ChoiceSort()
{
	int i,j,temp;
	for(i = 0;i < 5 - 1;i++)//实现选择排序法
		for(j = i + 1;j < 5;j++)
			if(a[i] > a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
}
void Array::InsertSort()
{
	int i,j,temp;
	for(i = 1;i < 5;i++)//实现插入排序法
	{
		temp = a[i];
		for(j = i - 1;j >= 0 && temp < a[j];j--)
			a[j+1] = a[j];
		a[j+1] = temp;
	}
}
ostream& operator <<(ostream& out,const Array& s)
{
	out << "The result is:" << endl;
	for(int i = 0;i < 5;i++)   //重载"<<"操作符
		out << s.a[i] << " ";
	out << endl;
	return out;
}

#include <iostream>
#include "small.h"
using namespace std;

int main()
{
	Array sw;
	char m,n;

	do
	{
	    cout << "Please choice the way to sort:" << endl;      //通过用户输入不同的字母来选择不同的排序方式
	    cout << "a.BubblingSort1(from small to big)" << endl;  //a 为冒泡排序法按从小到大,左到右排序,屏幕输出
	    cout << "b.BubblingSort2(from big to small)" << endl;  //b 为冒泡排序法按从大到小,右到左排序,文件输出
	    cout << "c.ChoiceSort" << endl;                        //c 为选择排序法按从小到大排序,屏幕输出
	    cout << "d.InsertSort" << endl;                        //d 为插入排序法按从小到大排序,文件输出
	    cout << "Input a,b,c or d : ";
	    cin >> m;

		if(m == 'a')
		{
			input(sw);
			sw.BubblingSort1();
			cout << sw;
		}
		if(m == 'b')
		{
			input(sw);
			sw.BubblingSort2();
			cout << sw;
		}
		if(m == 'c')
		{
			sw.get(cin);
			sw.ChoiceSort();
			cout << sw;
		}
		if(m == 'd')
		{
			sw.get(cin);
			sw.InsertSort();
			cout << sw;
		}
		cout << "If you want to do it again," << endl;
		cout << "press y for yes,n for no." << endl;
		cout << "Then press return:";
		cin >> n;
	}while(n == 'Y' || n == 'y');   //判断是否重复
	system("pause");

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

考证的二狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值