求助,为什么不能正确执行啊!!希望各位大神帮忙看看改改吧!!!
#include<stdio.h>#include <string.h>
#include <process.h>//exit(0)
typedef char* str;
#define size 3
str name[size]={"*"}; //stack溢出,因此要求这样设这外部变量
double score[size]={0};
void input(str *name,double *score,int n);//接收姓名和成绩
void output(str *name,double *score,int n);//输出姓名和成绩
void sort_direct(double *score,int n);//直接排序
void sort_bubble(double *score,int n);//冒泡排序
void find_line(str *name,double *score,char *stu_name);//直接查找
void sort_by_name(str *name);//对姓名进行排序
void find_devide(str *name,double *score,char *stu_name);//二分法查找
void input(str *name,double *score,int n)//接收姓名和成绩
{
int i;
printf("请输入%d个学生的姓名和成绩:\n",n);
for (i=0;i<n;i++)
{
scanf("%s%lf",&name[i],&score[i]);
if (i<n-1)
{
printf("请输入学生姓名和成绩:\n");
}
}
}
void output(str *name,double *score,int n)//输出姓名和成绩
{
int i;
printf("\n%d个学生的姓名和成绩为:\n",n);
for (i=0;i<n;i++)
{
printf("%s\t%f\n",&name[i],score[i]);
}
}
void sort_direct(double *score,int n)//直接排序
{
int min_allo,i,j;
double temp;
for (i=0;i<n-1;i++)
{
min_allo=i;
for (j=i+1;j<n;j++)
{
if (score[j]<score[min_allo])
min_allo=j;
}
temp=score[i];
score[i]=score[min_allo];
score[min_allo]=temp;
str name_swap; //交换姓名
name_swap=name[i];
strcpy(name[i],name[min_allo]);
strcpy(name[min_allo],name_swap);
}
output(name,score,n);//输出排序后的数组
}
void sort_bubble(double *score,int n)//冒泡排序
{
double temp;
for (int i=0;i<n-1;i++)
{
for (int j=0;j<n-1;j++)
{
if (score[j+1]<score[j])
{
temp=score[j+1];
score[j+1]=score[j];
score[j]=temp;
str name_swap; //交换姓名
name_swap=name[j];
strcpy(name[j],name[j+1]);
strcpy(name[j+1],name_swap);
}
}
}
output(name,score,n);//输出排序后的数组
}
void find_line(str *name,double *score,char *stu_name)//直接查找
{
for (int i=0;(strcmp(name[i],stu_name)!=0)&&i<size;i++)//两个数组的比较有问题
;
if (i==size)
printf("查找失败\n");
else
{
printf("该同学的成绩为:\n");
printf("%f\n",score[i-1]);
}
}
void sort_by_name(str *name)//对姓名进行排序
{
int i,j,min_allo;
str temp;
for (i=0;i<size-1;i++)
{
min_allo=i;
for (j=i+1;j<size;j++)
{
if (strcmp(name[j],name[i])<0)
min_allo=j;
}
temp=name[min_allo];
name[min_allo]=name[i];
name[i]=temp;
double score_swap; //交换成绩
score_swap=score[min_allo];
score[min_allo]=score[i];
score[i]=score_swap;
}
}
void find_devide(str *name,double *score,char *stu_name)//二分法查找
{
int mid,low=0,high=size-1;
while (low<=high)
{
mid=(low+high)/2;
if ((strcmp(name[mid],stu_name))==0)
{
printf("该同学的成绩为:\n");
printf("%f\n",score[mid]);
break;
}
else if ((strcmp(name[mid],stu_name))<0)
low=low+1;
else
high=high-1;
}
}
void main()
{
char choose_sort;
char stu_name[20];
input(name,score,size);
sort_by_name(name);
output(name,score,size);
do
{
printf("请选择排序方法(0-直接排序/1-冒泡排序):\n");
scanf("%d",&choose_sort);
} while(!(choose_sort=='0'||choose_sort=='1'));
if (choose_sort==0)
sort_direct(score,size);
else
sort_bubble(score,size);
head:printf("\n请输入想要查找的学生的姓名:\n");
scanf("%s",stu_name);
do
{
printf("请选择查找方法(0-直接查找/1-折半查找/q-退出):\n");
scanf("%d",&choose_sort);
} while(!(choose_sort=='0'||choose_sort=='1'||choose_sort=='q'));
if (choose_sort=='0')
{
find_line(name,score,stu_name);
goto head;
}
else if (choose_sort=='1')
{
sort_by_name(name);
find_devide(name,score,stu_name);
goto head;
}
else
exit(0);
}
程序为什么在使用了那个strcmp()/strcpy()之后,程序就不能正确执行了啊???