c语言程序主体,C语言函数已有主体
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/*插入数据*/
bool InsertByID()
{//在顺序表L中第i个位置插入新的元素e,i值的合法范围三1<=i<=L.length+1
int i;
ElemType e;
cout<
ShowAllDate();
cout<
cin>>i;
if((i<1)||(i>L.length+1)){cout<
if(L.length==MAXSIZE) {cout<
cout<
cout<
cin>>e.name;
cout<
cin>>e.no;
cout<
cin>>e.sub;
cout<
cin>>e.price;
for(int j=L.length;j>=i;j--){
L.elem[j+1]=L.elem[j];
}
L.elem[i]=e;
++L.length;//更新记录数
cout<
return true;
}
bool DeleteByID()
{//在顺序表L中删除第i个元素,i值的合法范围三1<=i<=L.length
int i;
cout<
cout<
cin>>i;
if((i<1)||(i>L.length)){
cout<
return false;
}
for(int j=i;j<=L.length-1;j++){
L.elem[j]=L.elem[j+1];
}
--L.length;
cout<
return true;
}
/*利用直接插入排序或者折半插入排序按照姓名进行排序;*/
int flag=0;//2表示数据已经更新为姓名排序方案void InsertSort(ElemType *a);
void BInsertSort(ElemType *a);
//姓名排序操作函数
void SelectSort()
{
if(L.length<1){
cout<
return ;
}
cout<
cout<
int n;
cin>>n;
cout<
cout<
cin>>flag;
if(flag!=2){//按姓名排序方式直接显示记录
ElemType a[MAXSIZE];//排序专用临时数组
//把数据拷贝至排序数组中
for(int i=1;i<=L.length;i++) a[i]=L.elem[i];
if(n==2)
BInsertSort(a);
else
InsertSort(a);
//因为默认选择序号1,用户只能操作一次选择,选择错误即默认选择序号1
cout<
cout<
cout<
for(i=1;i<=L.length;i++)
{
cout<
}
cout<
cout<
flag=1;//防止用户输入非1非2数值
}else{//按姓名排序方式改变记录排序方案并显示记录
if(n==2)
BInsertSort(L.elem);
else
InsertSort(L.elem);
//因为默认选择序号1,用户只能操作一次选择,选择错误即默认选择序号1
cout<
cout<
cout<
for(int i=1;i<=L.length;i++)
{
cout<
}
cout<
cout<
}
}
//此处为直接插入排序。对姓名排序
void InsertSort(ElemType a[])
{
for(int i=2;i<=L.length;i++)
if(strcmp(a[i].name,a[i-1].name)<0)
{
a[0]=a[i];
a[i]=a[i-1];
int j;
for(j=i-2;strcmp(a[0].name,a[j].name)<0;j--)
a[j+1]=a[j];
a[j+1]=a[0];
}
}
//此处为折半插入排序。对姓名排序
void BInsertSort(ElemType a[])
{
for(int i=2;i<=L.length;i++)
{
a[0]=a[i];
int low=1,high=i-1;
while(low<=high)
{
int m=(low+high)/2;
if(strcmp(a[0].name,a[m].name)<0) high=m-1;
else low=m+1;
}
for(int j=i-1;j>=high+1;--j) a[j+1]=a[j];
a[high+1]=a[0];
}
}
/*利用快速排序按照成绩进行排序*/
int Partition(ElemType a[],int low,int high)
{//对顺序表a中low..high进行一趟排序,返回枢轴位置
a[0]=a[low];
int pivotkey=a[low].price;
while(low
{
while(low=pivotkey) --high;
a[low]=a[high];
while(low
a[high]=a[low];
}
a[low]=a[0];
return low;
}
//快速排序,对成绩进行排序
void QSort(ElemType a[],int low,int high)
{//调用前置初值:low=1;high=L.length;
//对顺序表a中子序列low..high做快速排序
if(low
int pivotloc=Partition(a,low,high);
QSort(a,low,pivotloc-1);
QSort(a,pivotloc+1,high);
}
}
//成绩排序操作函数
void QuickSort()
{
if(L.length<1){
cout<
return ;
}
cout<
cout<
cin>>flag;
//把数据拷贝至排序数组中
if(flag!=2){//不改变原数据情况下排序并输出结果
ElemType a[MAXSIZE];//排序专用临时数组
for(int i=1;i<=L.length;i++) a[i]=L.elem[i];
//因为默认选择序号1,用户只能操作一次选择,选择错误即默认选择序号1
QSort(a,1,L.length);
cout<
cout<
cout<
for(i=1;i<=L.length;i++)
{
cout<
}
cout<
cout<
flag=1;
}else{
QSort(L.elem,1,L.length);
cout<
cout<
cout<
for(int i=1;i<=L.length;i++)
{
cout<
}
cout<
cout<
flag=3;//在程序内部,2是代表姓名排序后的数据,3代表学号排序结果
}
}