学校一个学生信息排序的题目,然后分成了添加,删除,查找,更改。然后最后一个排序题把前面的都综合起来再加一个排序功能。我前面都是对的,到了排序那题就一直WA。但是测试了好多次都没问题。希望有大神能帮忙看看,谢谢【猴子傻笑】
题目如下
请设计一个简单的学生成绩管理系统,要求系统实现以下功能:
插入一个学生的信息: Insert id name sex year month day x y z, 其中的参数分别为
学号、姓名、性别、出生日期(年、月、日)、三门课的成绩,成绩为浮点数。
List 输出所有学生信息。
查找学生信息:
Find id 查找学号为id的学生信息。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz
把学号为id的学生信息修改为
newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)
删除学生信息 Delete id 删除学号为id的学生信息
按学号从小到大排序 Sort byid
按出生日期从小到大排序 Sort bybirthday
按总成绩从小到大排序 Sort bysum
退出程序: Quit或者Exit
请注意:姓名的长度不超过20。
输入:
输入有多行,每行一条指令,指令格式如下:
Insert id name sex year month day x y z 其中的参数分别为学号、姓名、性别、
出生日期(年、月、日)三门课的成绩,成绩为浮点数。
List 输出所有学生信息。
Find id 查找学号为 id 的学生信息。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz
把学号为id的学生信息修改为
newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)
Delete id 删除学号为id的学生信息
Sort byid 按学号从小到大排序并输出
Sort bybirthday 按出生日期从小到大排序
Sort bysum 按总成绩从小到大排序并输出
Quit或者Exit 输出"Good bye!"后结束程序。
输出:
输出有多行,对应命令的输出如下:
Insert id name sex year month day x y z 插入后在单独的一行中输出"Insert:",
如果链表中不存在相同学号的学生信息,在第二行中显示学生信息,
格式: id name sex year month day x y z ave sum,分别为学号、姓名、性别、
出生日期(年、月、日)和三门课(语文、数学、英语)的成绩,平均成绩和总成绩,
数据之间用一个空格分开,成绩保留1位小数。 否则,输出"Failed"
List 输出"List:"后,按最近一次排序的顺序输出所有学生的信息,格式与插入学生信息后输出的格式相同。
Find id 第一行显示”Find:",第二行显示格式如下: 如果找到学号为id的学生,
则在单独一行中显示学生信息,格式如List。否则在单独一行显示"Failed"。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz
第一行显示"Change:"。如果链表中不存在学号为id的学生,显示"Failed"。
否则修改该学生信息并在单独一行中显示该生信息,显示格式如List命令。
Delete id 第一行显示"Delete:"。如果链表中不存在学号为id的学生,显示"Failed"。
否则删除该学生信息并在单独一行中显示"Deleted"
Sort byid 按学号从小到大排序并输出 输出格式与List相同
Sort bybirthday 按出生日期从小到大排序 输出格式与List相同
Sort bysum 按总成绩从小到大排序并输出 输出格式与List相同。
Quit或者Exit 在单独一行中输出"Good bye!"后结束程序。
输入样例:
Insert 0911001 zhangsan F 1992 3 24 87 78 65
Insert 0911003 Lisi F 1992 5 3 77 72 55
Find 0911002
Find 0911003
Insert 0911001 zhangou M 1992 3 24 98 78 65
Insert 0911002 zhaoliu F 1993 8 8 97 90 55
Change 0911002 zhaoliu M 1990 9 9 90 91 92
Change 0911005 zhaoliu M 1990 9 9 90 91 92
Delete 0911001
Delete 0911006
Insert 0911005 Wangrong F 1990 12 12 68 56 100
Find 0911002
Sort byid
Sort bybirthday
Sort bysum
Quit
输出样例:
Insert:
0911001 zhangsan F 1992 3 24 87.0 78.0 65.0 76.7 230.0
Insert:
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
Find:
Failed
Find:
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
Insert:
Failed
Insert:
0911002 zhaoliu F 1993 8 8 97.0 90.0 55.0 80.7 242.0
Change:
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
Change:
Failed
Delete:
Deleted
Delete:
Failed
Insert:
0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0
Find:
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
Sort:
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0
Sort:
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
Sort:
0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0
0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0
0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0
Good bye!
然后接下来是我写的代码
#include<stdio.h>
#include<string.h>
struct student{
char id[10];
char name[20];
char sex;
int year;
int month;
int day;
double x;
double y;
double z;
double sum_s;
double average_s;
};
void Solve();
int info_add(struct student *info,int num);
int info_find(struct student *info,int num);
int info_delete(struct student *info,int num);
void info_list(struct student *info,int num);
int info_change(struct student *info,int num);
void info_sortid(struct student *info,int num);
void info_sortbirth(struct student *info,int num);
void info_sortsum(struct student *info,int num);
int main()
{
Solve();
return 0;
}
void Solve()
{
char head[6];
struct student info[100];
int num=0;
while(scanf("%s",&head)&&strcmp(head,"Exit")!=0&&strcmp(head,"Quit")!=0)
{
getchar();
if(strcmp(head,"Insert")==0)
{
if(info_add(info,num)==1)
{
num++;
}
}
else if(strcmp(head,"Find")==0)
{
info_find(info,num);
}
else if(strcmp(head,"Delete")==0)
{
if(info_delete(info,num)==1)
{
num=num-1;
}
}
else if(strcmp(head,"List")==0)
{
info_list(info,num);
}
else if(strcmp(head,"Sort")==0)
{
char sortch[10];
scanf("%s",&sortch);
if(strcmp(sortch,"byid")==0)
{
info_sortid(info,num);
}
else if(strcmp(sortch,"bybirthday")==0)
{
info_sortbirth(info,num);
}
else if(strcmp(sortch,"bysum")==0)
{
info_sortsum(info,num);
}
}
else if(strcmp(head,"Change")==0)
{
info_change(info,num);
}
}
printf("Good bye!\n");
}
int info_add(struct student *info,int num)
{
struct student temp;
scanf("%s %s %c %d %d %d %lf %lf %lf",&temp.id,&temp.name,&temp.sex,&temp.year,&temp.month,&temp.day,&temp.x,&temp.y,&temp.z);
temp.sum_s=temp.x+temp.y+temp.z;
temp.average_s=temp.sum_s/3;
for(int i=0;i<num;i++)
{
if(strcmp(temp.id,info[i].id)==0)
{
printf("Insert:\nFailed\n");
return 0;
}
}
info[num]=temp;
printf("Insert:\n%s %s %c %d %d %d ",temp.id,temp.name,temp.sex,temp.year,temp.month,temp.day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",temp.x,temp.y,temp.z,temp.average_s,temp.sum_s);
return 1;
}
int info_find(struct student *info,int num)
{
char id[10];
scanf("%s",&id);
for(int i=0;i<num;i++)
{
if(strcmp(id,info[i].id)==0)
{
printf("Find:\n");
printf("%s %s %c %d %d %d ",info[i].id,info[i].name,info[i].sex,info[i].year,info[i].month,info[i].day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",info[i].x,info[i].y,info[i].z,info[i].average_s,info[i].sum_s);
return -1;
}
}
printf("Find:\nFailed\n");
}
int info_delete(struct student *info,int num)
{
char id[10];
scanf("%s",&id);
for(int i=0;i<num;i++)
{
if(strcmp(id,info[i].id)==0)
{
for(int t=i;t<num;t++)
{
info[t]=info[t+1];
}
printf("Delete:\nDeleted\n");
return 1;
}
}
printf("Delete:\nFailed\n");
return 0;
}
void info_list(struct student *info,int num)
{
printf("List:\n");
for(int i=0;i<num;i++)
{
printf("%s %s %c %d %d %d ",info[i].id,info[i].name,info[i].sex,info[i].year,info[i].month,info[i].day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",info[i].x,info[i].y,info[i].z,info[i].average_s,info[i].sum_s);
}
}
int info_change(struct student *info,int num)
{
struct student temp;
scanf("%s %s %c %d %d %d %lf %lf %lf",&temp.id,&temp.name,&temp.sex,&temp.year,&temp.month,&temp.day,&temp.x,&temp.y,&temp.z);
temp.sum_s=temp.x+temp.y+temp.z;
temp.average_s=temp.sum_s/3;
for(int i=0;i<num;i++)
{
if(strcmp(temp.id,info[i].id)==0)
{
info[i]=temp;
printf("Change:\n%s %s %c %d %d %d ",temp.id,temp.name,temp.sex,temp.year,temp.month,temp.day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",temp.x,temp.y,temp.z,temp.average_s,temp.sum_s);
return -1;
}
}
printf("Change:\nFailed\n");
}
void info_sortsum(struct student *info,int num)
{
struct student temp;
for(int i=0;i<num-1;i++)
{
for(int t=0;t<num-1-i;t++)
{
if(info[t].sum_s>info[t+1].sum_s)
{
temp=info[t];
info[t]=info[t+1];
info[t+1]=temp;
}
}
}
printf("Sort:\n");
for(int i=0;i<num;i++)
{
printf("%s %s %c %d %d %d ",info[i].id,info[i].name,info[i].sex,info[i].year,info[i].month,info[i].day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",info[i].x,info[i].y,info[i].z,info[i].average_s,info[i].sum_s);
}
}
void info_sortbirth(struct student *info,int num)
{
struct student temp;
for(int i=0;i<num-1;i++)
{
for(int t=0;t<num-i-1;t++)
{
if(info[t].year>info[t+1].year)
{
temp=info[t];
info[t]=info[t+1];
info[t+1]=temp;
}
else if(info[t].year==info[t+1].year)
{
if(info[t].month>info[t+1].month)
{
temp=info[t];
info[t]=info[t+1];
info[t+1]=temp;
}
else if(info[t].month==info[t+1].month)
{
if(info[t].day>info[t+1].day)
{
temp=info[t];
info[t]=info[t+1];
info[t+1]=temp;
}
}
}
}
}
printf("Sort:\n");
for(int i=0;i<num;i++)
{
printf("%s %s %c %d %d %d ",info[i].id,info[i].name,info[i].sex,info[i].year,info[i].month,info[i].day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",info[i].x,info[i].y,info[i].z,info[i].average_s,info[i].sum_s);
}
}
void info_sortid(struct student *info,int num)
{
struct student temp;
for(int i=0;i<num-1;i++)
{
for(int t=0;t<num-1-i;t++)
{
if(strcmp(info[t].id,info[t+1].id)>0)
{
temp=info[t];
info[t]=info[t+1];
info[t+1]=temp;
}
}
}
printf("Sort:\n");
for(int i=0;i<num;i++)
{
printf("%s %s %c %d %d %d ",info[i].id,info[i].name,info[i].sex,info[i].year,info[i].month,info[i].day);
printf("%.1lf %.1lf %.1lf %.1lf %.1lf\n",info[i].x,info[i].y,info[i].z,info[i].average_s,info[i].sum_s);
}
}
麻烦各位大神帮忙看看排序的地方有什么错误吗??
代码太多了,0注释,自己才看得懂,我知道head字符串和sortch空间明显不够,insert6个字符,还需要第七个位置存放'\0',char字符串都是以\0结尾,不然会越界,所以你空间再给大一点