C++运行时提示“.exe已停止工作”
各位大神,这是我写的程序,编译时没错误,也没警告,就是运行时提示“.exe已停止工作”,求大神帮忙解决!
题目是:设计一维数组的排序类Sort
设计要求:
具备“直接插入排序”、“折半插入排序”、“冒泡排序”三种排序方法;能够对int/float/double/char 等数据类型进行排序; 运用虚函数、类模板等技术手段。统计完成排序所花费的时间。
#include <iostream.h>
#include <stdio.h>
#include<string.h>
#include <time.h>
//定义基类Sort
template <typename Type>
class Sort
{
protected:
int i, j, length, x;
Type *a;
public:
//定义构造函数
Sort(int s=0, int n=0,Type b[]=NULL) //将基类的构造函数定义为创建数组函数,创建一个长度为n的一维数组,a[0]=0,且不参与排序(创建数组函数)
{
for(i=1;i<=n;i++)
{
a[i] = b[i];
}
x = s;
length = n;
}
void create()
{
printf("Please input data:\n");
switch(x)
{
case 1:for (i=1;i<=length;i++) //i<=k
{
scanf("%d",&a[i]);
};
break;
case 2:for (i=1;i<=length;i++) //i<=k
{
scanf("%f",&a[i]);
};
break;
case 3:for (i=1;i<=length;i++) //i<=k
{
scanf("%lf",&a[i]);
};
break;
case 4:for (i=1;i<=length;i++) //i<=k
{
scanf("%s",&a[i]);
};
break;
}
}
virtual void Sorts(Type a[], int length) //排序函数定义为虚函数
{
cout<<"这是排序函数"<<endl;
}
void print(Type a[]) //输出函数,输出数组a[1]...a[length]的值
{
printf("创建的一维数组为:");
for (i=1; i<=length; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
};
//定义派生类DInsertSort,直接插入排序
template <typename Type>
class DInsertSort : public Sort<Type>
{
public:
//构造函数
DInsertSort<Type>(int s, int n,Type b[]):Sort<Type>(s,n)
{
for(i=1;i<=n;i++)
{
a[i] = b[i];
}
};
Type *Sorts() //定义排序函数
{
int i,j;
for (i=2;i < length+1;i++)
{
a[0]=a[i];
for (j=i-1;a[0] < a[j];j--) //a[1]...a[i-1]是有序数列
{
a[j+1] = a[j];
}
a[j+1] = a[0];
}
return a;
}
};
//定义派生类BInsertSort,折半插入排序
template< typename Type >
class BInsertSort : public Sort< Type >
{
public:
//构造函数
BInsertSort<Type>(int s, int n,Type b[]):Sort<Type>(s,n)
{
for(i=1;i<=n;i++)
{
a[i] = b[i];
}
}
Type *Sorts ( /*Type a[],int length*/) //折半插入排序
{
int low,high,mid;
for (i=2; i<length; i++)
{
a[0]=a[i]; //将a[i]暂存到a[0]
low=1;
high=i-1;
while (low<=high) //在有序数列 a[1]...a[i-1]中折半查找插入位置;
{
mid=(low+high)/2; //折半
if (a[0] < a[mid])
{
high=mid-1; //插入点在左半部分
}
else
{
low=mid+1; //插入点在右半部分
}
}
for (j=i-1; j>=high+1;j--)
{
a[j+1] = a[j]; //记录后移
}
a[high+1] = a[0]; //记录后移
}
return a;
}
};
//定义派生类BubbleSort,冒泡排序
template< typename Type >
class BubbleSort : public Sort< Type >
{
public:
//构造函数
BubbleSort<Type>(int s, int n,Type b[]):Sort<Type>(s,n)
{
for(i=1;i<=n;i++)
{
a[i] = b[i];
}
}
Type *Sorts ( /*Type a[]*/ ) //冒泡排序函数
{
Type temp ;
for ( i = length; i > 1; i -- )
{
for ( j = 1; j < i; j ++ )
{
if ( a[j] > a[j+1] )
{
temp = a[j] ;
a[j] = a[j+1] ;
a[j+1] = temp ; //第一次循环结束以后最大的数会出现在最右边的位置
}
}
}
return a;
}
};
void main()
{
int x,length,a1[30];
float a2[30];
double a3[30];
char a4[30];
do{
printf("请输入数值的类型:\n1.int 2.float 3.double 4.char\n");
scanf("%d",&x);
}while(x>4);
printf("输入数组长度:");
scanf("%d",&length);
if (x=1)
{
int *m1,*m2,*m3;
double It1,It2,It3,It4,It5,It6;
DInsertSort<int> A1(x,length,a1);
A1.create();
It1=clock();
m1=A1.Sorts();
It2=clock();
A1.print(m1);
printf("int型数组用直接插入法排序所用的时间为:%lf seconds\n",It2-It1);
BInsertSort<int> A2(x,length,a1);
A2.create();
It3=clock();
m2=A2.Sorts();
It4=clock();
A2.print(m2);
printf("int型数组用折半插入法排序所用的时间为:%lf seconds\n",It4-It3);
BubbleSort<int> A3(x,length,a1);
A3.create();
It5=clock();
m3=A3.Sorts();
It6=clock();
A3.print(m3);
printf("int型数组用冒泡法排序所用的时间为:%lf seconds\n",It6-It5);
}
else if(x=2)
{
float *m1,*m2,*m3;
double Ft1,Ft2,Ft3,Ft4,Ft5,Ft6;
DInsertSort<float> B1(x,length,a2);
B1.create();
Ft1=clock();
m1=B1.Sorts();
Ft2=clock();
B1.print(m1);
printf("float型数组用直接插入法排序所用的时间为:%lf seconds\n",Ft2-Ft1);
BInsertSort<float> B2(x,length,a2);
B2.create();
Ft3=clock();
m2=B2.Sorts();
Ft4=clock();
B2.print(m2);
printf("float型数组用折半插入法排序所用的时间为:%lf seconds\n",Ft4-Ft3);
BubbleSort<float> B3(x,length,a2);
B3.create();
Ft5=clock();
m3=B3.Sorts();
Ft6=clock();
B3.print(m3);
printf("float型数组用冒泡法排序所用的时间为:%lf seconds\n",Ft6-Ft5);
}
else if(x=3)
{
double *m1,*m2,*m3;
double Dt1,Dt2,Dt3,Dt4,Dt5,Dt6;
DInsertSort<double> C1(x,length,a3);
C1.create();
Dt1=clock();
m1=C1.Sorts();
Dt2=clock();
C1.print(m1);
printf("double型数组用直接插入法排序所用的时间为:%lf seconds\n",Dt2-Dt1);
BInsertSort<double> C2(x,length,a3);
C2.create();
Dt3=clock();
m2=C2.Sorts();
Dt4=clock();
C2.print(m2);
printf("double型数组用折半插入法排序所用的时间为:%lf seconds\n",Dt4-Dt3);
BubbleSort<double> C3(x,length,a3);
C3.create();
Dt5=clock();
m3=C3.Sorts();
Dt6=clock();
C3.print(m3);
printf("double型数组用冒泡法排序所用的时间为:%lf seconds\n",Dt6-Dt5);
}
else
{
char *m1,*m2,*m3;
double Ct1,Ct2,Ct3,Ct4,Ct5,Ct6;
DInsertSort<char> D1(x,length,a4);
D1.create();
Ct1=clock();
m1=D1.Sorts();
Ct2=clock();
D1.print(m1);
printf("char型数组用直接插入法排序所用的时间为:%lf seconds\n",Ct6-Ct5);
BInsertSort<char> D2(x,length,a4);
D2.create();
Ct3=clock();
m2=D2.Sorts();
Ct4=clock();
D2.print(m2);
printf("double型数组用折半插入法排序所用的时间为:%lf seconds\n",Ct6-Ct5);
BubbleSort<char> D3(x,length,a4);
D3.create();
Ct5=clock();
m3=D3.Sorts();
Ct6=clock();
D3.print(m3);
printf("double型数组用冒泡法排序所用的时间为:%lf seconds\n",Ct6-Ct5);
}
}