程序应该是有点小错误!因为在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;
}