# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int main(void)
{
void stu_save(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明学生信息保存函数
void stu_id_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明学号排序函数
void stu_chinese_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[],float stu_average[]);
//声明语文成绩排序函数
void stu_math_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[],float stu_average[]);
//声明数学成绩排序函数
void stu_english_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[],float stu_average[]);
//声明英语成绩排序函数
void stu_physics_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[],float stu_average[]);
//声明物理成绩排序函数
void stu_chemistry_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[],float stu_average[]);
//声明化学成绩排序函数
void stu_tp_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明总成绩排序函数
void stu_average_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明平均分排序函数
void subject_average(int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], float subject_average[]);
//声明计算科目平均分函数
void stu_add(char stu_name[][8], int sut_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明添加学生信息函数.
int stu_find(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int englsih[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明查找学生信息函数.
int stu_delete(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明删除学生信息函数
int stu_change(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[]);
//声明修改学生信息函数
char student_name[100][8];
//student_name 存放学生名字.
int stu_Num, menu_no, i;
//stu_Num 学生总人数. menu_no 选择菜单序列号. i 循环变量
int stu_ID[100], Chinese[100], Math[100], English[100], Physics[100], Chemistry[100], TP[100];
//stu_ID 存放学号. Chinese 存放语文成绩. Math 存放数学成绩. English 存放英语成绩. Physics 存放物理成绩. Chemistry 存放化学成绩. TP 存放总分.
float stu_Average[100], subject_Average[5];
//stu_Average 存放每个学生的平均分数. subject_Average 存放各门科目的平均分
char c[2], str_stu_Num[2];
c[0]='y';
//c 是否需要重新排序的选择,默认yes
stu_Num:
printf("请输入学生的总人数(1-99): ");
int k;
fflush(stdin);
for(k=0; (str_stu_Num[k]=getchar())!='\n'; k++)
{
if(str_stu_Num[k]<'1' || str_stu_Num[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto stu_Num;
}
}
stu_Num = atoi(str_stu_Num);
if(stu_Num<1 || stu_Num>99)
{
printf("超过总人数范围(1-99),请重新输入!\n");
goto stu_Num;
}
printf("\n");
// 提示用户需要输入的学生个数.
stu_save(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average);
/*for(i=0; i<stu_Num; i++)
{
printf("输入第%d个学生的姓名: ", i+1);
scanf("%s", student_name[i]);
//存入学生名字
printf("输入 [%s] 的学号: ", student_name[i]);
scanf("%d", &stu_ID[i]);
//存入学生学号
printf("输入 [%s] 的[语文]成绩: ", student_name[i]);
scanf("%d", &Chinese[i]);
printf("输入 [%s] 的[数学]成绩: ", student_name[i]);
scanf("%d", &Math[i]);
printf("输入 [%s] 的[英语]成绩: ", student_name[i]);
scanf("%d", &English[i]);
printf("输入 [%s] 的[物理]成绩: ", student_name[i]);
scanf("%d", &Physics[i]);
printf("输入 [%s] 的[化学]成绩: ", student_name[i]);
scanf("%d", &Chemistry[i]);
printf("\n");
//存入各门课程的分数
TP[i] = Chinese[i]+Math[i]+English[i]+Physics[i]+Chemistry[i];
//计算出每个学生的总分.
stu_Average[i] = TP[i]/5.0;
//计算出每个学生的平均分.
}*/
menu:
subject_average(stu_Num, Chinese, Math, English, Physics, Chemistry, subject_Average);
//调用函数计算出各门科目的平均分
menu1:
do
{
printf(" 1.按学号排序 2.按语文成绩排序\n");
printf(" 3.按数学成绩排序 4.按英语成绩排序\n");
printf(" 5.按物理成绩排序 6.按化学成绩排序\n");
printf(" 7.按总成绩排序 8.按平均分排序 \n");
printf(" 9.添加学生信息 10.删除学生信息 \n");
printf(" 11.查找学生信息 12.修改学生信息 \n");
printf("\n");
printf("请选择选项(1-12):");
begin1:
fflush(stdin);
scanf("%d", &menu_no);
//各项功能选择.
menu2:
if(menu_no<1 || menu_no>12)
{
printf("输入有误,请重新输入:");
goto begin1;
}
switch(menu_no)
{
case 1:
printf("按学号进行排序\n");
stu_id_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average); break;
//调用学号排列函数
case 2:
printf("按语文成绩进行排序\n");
stu_chinese_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP,stu_Average); break;
//调用语文成绩排序函数
case 3:
printf("按数学成绩进行排序\n");
stu_math_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average); break;
//调用数学成绩排序函数
case 4:
printf("按英语成绩进行排序\n");
stu_english_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average); break;
//调用英语成绩排序函数
case 5:
printf("按物理成绩进行排序\n");
stu_physics_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average); break;
//调用物理成绩排序函数
case 6:
printf("按化学成绩进行排序\n");
stu_chemistry_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP, stu_Average); break;
//调用化学成绩排序函数
case 7:
printf("按总成绩进行排序\n");
stu_tp_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP,stu_Average); break;
//调用总分排序函数
case 8:
printf("按平均分进行排序\n");
stu_average_sort(student_name, stu_Num, stu_ID, Chinese, Math, English, Physics, Chemistry, TP,stu_Average); break;
//调用平均分排序函数
case 9:
stu_add(student_name, stu_ID, stu_Num, Chinese, Math, English, Physics, Chemistry, TP, stu_Average);
stu_Num = stu_Num+1;
//调用添加学生函数, 添加学生信息后,学生总人数+1;
printf("添加成功!\n");
goto menu;
case 10:
if(stu_delete(student_name, stu_ID, stu_Num, Chinese, Math, English, Physics, Chemistry, TP, stu_Average)>4)
goto menu1;
//调用删除学生信息函数,如果连续五次输入错误则返回菜单项.
stu_Num = stu_Num-1;
printf("删除成功!\n");
//删除学生信息后,学生总人数-1;
goto menu;
case 11:
if(stu_find(student_name, stu_ID, stu_Num, Chinese, Math, English, Physics, Chemistry, TP, stu_Average)>4)
goto menu1;
//调用查找学生短信息函数,如果连续五次输入错误则返回菜单项.
printf("重新选择其它功能(1-12):");
goto begin1;
//读取成功,重新选择其它选项.
case 12:
if(stu_change(student_name, stu_ID, stu_Num, Chinese, Math, English, Physics, Chemistry, TP, stu_Average)>4)
goto menu1;
//调用修改学生信息函数,如果连续五次输入错误则返回菜单项.
printf("修改成功!\n");
goto menu;
//修改成功后返回菜单项.
default:
printf("输入错误,重新选择(1-12):");
goto begin1;
}
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
for(i=0; i<stu_Num; i++)
{
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_ID[i], student_name[i], Chinese[i], Math[i], English[i], Physics[i], Chemistry[i], TP[i], stu_Average[i]);
}
if(stu_Num>0)
{
printf("\n各科目平均分: %.1f\t %.1f\t% .1f\t% .1f\t% .1f\n", subject_Average[0], subject_Average[1], subject_Average[2], subject_Average[3], subject_Average[4]);
}
//输出成绩表
begin:
fflush(stdin);
printf("\nY:返回菜单 N:退出程序 选择其它功能(1-12):");
gets(c);
printf("\n");
menu_no = atoi(c);
if(menu_no>=1 && menu_no<=12)
goto menu2;
else if(strcmp(c,"n")==0|| strcmp(c,"N")==0)
break;
else if(strcmp(c,"y")!=0 && strcmp(c,"Y")!=0)
{
printf("输入有误,请重新输入!\n");
fflush(stdin);
goto begin;
}
//判断c是否满足条件.
}
while(strcmp(c,"y")==0 || strcmp(c,"Y")==0);
//判断是否需要重新排序选择
return 0;
}
void stu_save(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
{//定义学生信息保存函数.
int i,j,k;
char str_stu_id[2], str_chinese[3], str_math[3], str_english[3], str_physics[3], str_chemistry[3];
for(i=0; i<stu_num; i++)
{
printf("输入第[%d]个学生的[姓名]: ", i+1);
scanf("%s", stu_name[i]);
stu_id:
printf("输入 [%s] 的[学号]: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_stu_id[k]=getchar())!='\n'; k++)
{
if(str_stu_id[k]<'1' || str_stu_id[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto stu_id;
}
}
stu_id[i] = atoi(str_stu_id);
if(stu_id[i]<1 || stu_id[i]>99)
{
printf("[学号]超过规定范围(1-99),请重新输入\n");
goto stu_id;
}
for(j=0; j<i; j++)
{
if(stu_id[i]==stu_id[j])
{
printf("[学号]已存在,请重新输入:");
goto stu_id;
}
}
chinese:
printf("输入 [%s] 的[语文]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chinese[k]=getchar())!='\n'; k++)
{
if(str_chinese[k]<'1' || str_chinese[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chinese;
}
}
chinese[i] = atoi(str_chinese);
if(chinese[i]<0 || chinese[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chinese;
}
math:
printf("输入 [%s] 的[数学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_math[k]=getchar())!='\n'; k++)
{
if(str_math[k]<'1' || str_math[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto math;
}
}
math[i] = atoi(str_math);
if(math[i]<0 || math[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto math;
}
english:
printf("输入 [%s] 的[英语]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_english[k]=getchar())!='\n'; k++)
{
if(str_english[k]<'1' || str_english[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto english;
}
}
english[i] = atoi(str_english);
if(english[i]<0 || english[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto english;
}
physics:
printf("输入 [%s] 的[物理]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_physics[k]=getchar())!='\n'; k++)
{
if(str_physics[k]<'1' || str_physics[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto physics;
}
}
physics[i] = atoi(str_physics);
if(physics[i]<0 || physics[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto physics;
}
chemistry:
printf("输入 [%s] 的[化学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chemistry[k]=getchar())!='\n'; k++)
{
if(str_chemistry[k]<'1' || str_chemistry[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chemistry;
}
}
chemistry[i] = atoi(str_chemistry);
if(chemistry[i]<0 || chemistry[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chemistry;
}
printf("\n");
tp[i] = chinese[i]+math[i]+english[i]+physics[i]+chemistry[i];
stu_average[i] = tp[i]/5.0;
}
}
void stu_id_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义学号排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(stu_id[j]>stu_id[j+1])
{
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
//用"起泡法"对学号进行排序
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
//学生名字随学号进行调整
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
//语文成绩随学号进行调整
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
//数学成绩随学号进行调整
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
//英语成绩随学号进行调整
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
//物理成绩随学号进行调整
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
//化学成绩随学号进行调整
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
//总分随学号进行调整
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
//平均分随学号进行调整
}
}
}
}
void stu_chinese_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义语文排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(chinese[j]<chinese[j+1])
{//用"起泡法"对语文成绩排序.下列各项随语文成绩进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_math_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义数学排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(math[j]<math[j+1])
{//用"起泡法"对数学成绩排序.下列各项随数学成绩进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_english_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义英语排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(english[j]<english[j+1])
{//用"起泡法"对英语成绩排序.下列各项随英语成绩进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_physics_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])//
//定义物理排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(physics[j]<physics[j+1])
{//用"起泡法"对物理成绩排序.下列各项随物理成绩进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_chemistry_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义化学排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(chemistry[j]<chemistry[j+1])
{//用"起泡法"对化学成绩排序.下列各项随化学成绩进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_tp_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义总分排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(tp[j]<tp[j+1])
{//用"起泡法"对总分排序.下列各项随总分进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void stu_average_sort(char stu_name[][8], int stu_num, int stu_id[], int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义平均分排序函数
{
int i, j, t;
float ave_t;
char temp[8];
for(i=0; i<stu_num-1; i++)
{
for(j=0; j<stu_num-1-i; j++)
{
if(stu_average[j]<stu_average[j+1])
{//用"起泡法"对平均分排序.下列各项随平均分进行调整
t = stu_id[j+1];
stu_id[j+1] = stu_id[j];
stu_id[j] = t;
strcpy(temp, stu_name[j+1]);
strcpy(stu_name[j+1], stu_name[j]);
strcpy(stu_name[j], temp);
t = chinese[j+1];
chinese[j+1] = chinese[j];
chinese[j] = t;
t = math[j+1];
math[j+1] = math[j];
math[j] = t;
t = english[j+1];
english[j+1] = english[j];
english[j] = t;
t = physics[j+1];
physics[j+1] = physics[j];
physics[j] = t;
t = chemistry[j+1];
chemistry[j+1] = chemistry[j];
chemistry[j] = t;
t = tp[j+1];
tp[j+1] = tp[j];
tp[j] = t;
ave_t = stu_average[j+1];
stu_average[j+1] = stu_average[j];
stu_average[j] = ave_t;
}
}
}
}
void subject_average(int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], float subject_average[])
{//定义计算科目平均分函数.
int i, j, sum;
for(i=0; i<5; i++)
{
switch(i)
{
case 0:
sum = 0;
for(j=0; j<stu_num; j++)
{
sum = chinese[j]+sum;
}
subject_average[i] = (float)sum/stu_num;
break;
case 1:
sum = 0;
for(j=0; j<stu_num; j++)
{
sum = math[j]+sum;
}
subject_average[i] = (float)sum/stu_num;
break;
case 2:
sum = 0;
for(j=0; j<stu_num; j++)
{
sum = english[j]+sum;
}
subject_average[i] = (float)sum/stu_num;
break;
case 3:
sum = 0;
for(j=0; j<stu_num; j++)
{
sum = physics[j]+sum;
}
subject_average[i] = (float)sum/stu_num;
break;
case 4:
sum = 0;
for(j=0; j<stu_num; j++)
{
sum = chemistry[j]+sum;
}
subject_average[i] = (float)sum/stu_num;
break;
}
}
}
void stu_add(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义添加学生信息函数
{
int i, j, k;
char str_stu_id[2], str_chinese[3], str_math[3], str_english[3], str_physics[3], str_chemistry[3];
for(i=stu_num; i<stu_num+1; i++)
{
printf("输入第[%d]个学生的[姓名]: ", i+1);
scanf("%s", stu_name[i]);
stu_id:
printf("输入 [%s] 的[学号]: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_stu_id[k]=getchar())!='\n'; k++)
{
if(str_stu_id[k]<'1' || str_stu_id[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto stu_id;
}
}
stu_id[i] = atoi(str_stu_id);
if(stu_id[i]<1 || stu_id[i]>99)
{
printf("学号超过规定范围(1-99),请重新输入\n");
goto stu_id;
}
for(j=0; j<i; j++)
{
if(stu_id[i]==stu_id[j])
{
printf("学号已存在,请重新输入:");
goto stu_id;
}
}
chinese:
printf("输入 [%s] 的[语文]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chinese[k]=getchar())!='\n'; k++)
{
if(str_chinese[k]<'1' || str_chinese[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chinese;
}
}
chinese[i] = atoi(str_chinese);
if(chinese[i]<0 || chinese[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chinese;
}
math:
printf("输入 [%s] 的[数学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_math[k]=getchar())!='\n'; k++)
{
if(str_math[k]<'1' || str_math[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto math;
}
}
math[i] = atoi(str_math);
if(math[i]<0 || math[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto math;
}
english:
printf("输入 [%s] 的[英语]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_english[k]=getchar())!='\n'; k++)
{
if(str_english[k]<'1' || str_english[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto english;
}
}
english[i] = atoi(str_english);
if(english[i]<0 || english[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto english;
}
physics:
printf("输入 [%s] 的[物理]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_physics[k]=getchar())!='\n'; k++)
{
if(str_physics[k]<'1' || str_physics[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto physics;
}
}
physics[i] = atoi(str_physics);
if(physics[i]<0 || physics[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto physics;
}
chemistry:
printf("输入 [%s] 的[化学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chemistry[k]=getchar())!='\n'; k++)
{
if(str_chemistry[k]<'1' || str_chemistry[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chemistry;
}
}
chemistry[i] = atoi(str_chemistry);
if(chemistry[i]<0 || chemistry[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chemistry;
}
printf("\n");
}
tp[stu_num] = chinese[stu_num]+math[stu_num]+english[stu_num]+physics[stu_num]+chemistry[stu_num];
stu_average[stu_num] = tp[stu_num]/5.0;
}
int stu_delete(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
//定义删除学生信息函数
{
int i, j,flag=0, error=0, str_id;
char delete_id[8], y_n;
printf("请输入你想删除的学生[学号]或[姓名]:");
delete_id:
fflush(stdin);
gets(delete_id);
str_id = atoi(delete_id);
for(i=0; i<stu_num; i++)
{
if(stu_id[i] == str_id)
{
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("确定删除?(Y/N): ");
y_n:
fflush(stdin);
scanf("%c", &y_n);
if(y_n=='y' || y_n=='Y')
{
for(j=i; j<stu_num; j++)
{
flag=1;
stu_id[j] = stu_id[j+1];
strcpy(stu_name[j], stu_name[j+1]);
chinese[j] = chinese[j+1];
math[j] = math[j+1];
english[j] = english[j+1];
physics[j] = physics[j+1];
chemistry[j] = chemistry[j+1];
tp[j] = tp[j+1];
stu_average[j] = stu_average[j+1];
}
return 0;
}
else if(y_n=='n' || y_n=='N')
{
printf("返回菜单项\n");
return 5;
}
else
{
printf("输入错误,请重新输入(Y/N): ");
goto y_n;
}
}
if(strcmp(stu_name[i], delete_id)==0)
{
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("确定删除?(Y/N): ");
y_n_1:
fflush(stdin);
scanf("%c", &y_n);
if(y_n=='y' || y_n=='Y')
{
for(j=i; j<stu_num; j++)
{
flag=1;
stu_id[j] = stu_id[j+1];
strcpy(stu_name[j], stu_name[j+1]);
chinese[j] = chinese[j+1];
math[j] = math[j+1];
english[j] = english[j+1];
physics[j] = physics[j+1];
chemistry[j] = chemistry[j+1];
tp[j] = tp[j+1];
stu_average[j] = stu_average[j+1];
}
return 0;
}
else if(y_n=='n' || y_n=='N')
{
printf("返回菜单项\n");
return 5;
}
else
{
printf("输入错误,请重新输入(Y/N): ");
goto y_n_1;
}
}
}
if(flag==0)
{
error++;
if(error>4)
{
printf("输入错误次数过多,返回菜单项!\n");
return(error);
}
printf("没有该学号,请重新输入:");
goto delete_id;
}
}
int stu_find(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
{//定义查找学生信息函数
int i, flag=0, error=0, str_id;
char find_id[8];
printf("请输入你想查找的学生[学号]或[姓名]:");
find_id:
fflush(stdin);
gets(find_id);
str_id = atoi(find_id);
for(i=0; i<stu_num; i++)
{
if(stu_id[i] == str_id)
{
flag=1;
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("\n");
break;
}
if(strcmp(stu_name[i],find_id)==0)
{
flag=1;
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("\n");
break;
}
}
if(flag==0)
{
error++;
if(error>4)
{
printf("输入错误次数过多,返回菜单项!\n");
return(error);
}
printf("没有该学号,请重新输入:");
goto find_id;
}
}
int stu_change(char stu_name[][8], int stu_id[], int stu_num, int chinese[], int math[], int english[], int physics[], int chemistry[], int tp[], float stu_average[])
{//定义修改学生信息函数.
int i, j, k, flag=0, error=0,str_id;
char change_id[8], str_stu_id[2], str_chinese[3], str_math[3], str_english[3], str_physics[3], str_chemistry[3];
printf("请输入你要修改的学生[学号]或[姓名]:");
change_id:
fflush(stdin);
gets(change_id);
str_id = atoi(change_id);
for(i=0; i<stu_num; i++)
{
if(stu_id[i]==str_id)
{
flag=1;
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("\n");
printf("请重新输入该学生信息:\n");
printf("请输入该学生[姓名]:");
scanf("%s", stu_name[i]);
stu_id:
printf("输入 [%s] 的[学号]: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_stu_id[k]=getchar())!='\n'; k++)
{
if(str_stu_id[k]<'1' || str_stu_id[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto stu_id;
}
}
stu_id[i] = atoi(str_stu_id);
if(stu_id[i]<1 || stu_id[i]>99)
{
printf("[学号]超过规定范围(1-99),请重新输入\n");
goto stu_id;
}
for(j=0; j<i; j++)
{
if(stu_id[i]==stu_id[j])
{
printf("[学号]已存在,请重新输入:");
goto stu_id;
}
}
chinese:
printf("输入 [%s] 的[语文]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chinese[k]=getchar())!='\n'; k++)
{
if(str_chinese[k]<'1' || str_chinese[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chinese;
}
}
chinese[i] = atoi(str_chinese);
if(chinese[i]<0 || chinese[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chinese;
}
math:
printf("输入 [%s] 的[数学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_math[k]=getchar())!='\n'; k++)
{
if(str_math[k]<'1' || str_math[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto math;
}
}
math[i] = atoi(str_math);
if(math[i]<0 || math[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto math;
}
english:
printf("输入 [%s] 的[英语]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_english[k]=getchar())!='\n'; k++)
{
if(str_english[k]<'1' || str_english[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto english;
}
}
english[i] = atoi(str_english);
if(english[i]<0 || english[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto english;
}
physics:
printf("输入 [%s] 的[物理]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_physics[k]=getchar())!='\n'; k++)
{
if(str_physics[k]<'1' || str_physics[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto physics;
}
}
physics[i] = atoi(str_physics);
if(physics[i]<0 || physics[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto physics;
}
chemistry:
printf("输入 [%s] 的[化学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chemistry[k]=getchar())!='\n'; k++)
{
if(str_chemistry[k]<'1' || str_chemistry[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chemistry;
}
}
chemistry[i] = atoi(str_chemistry);
if(chemistry[i]<0 || chemistry[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chemistry;
}
printf("\n");
tp[i] = chinese[i]+math[i]+english[i]+physics[i]+chemistry[i];
stu_average[i] = tp[i]/5.0;
return(error);
}
if(strcmp(stu_name[i],change_id)==0)
{
flag=1;
printf("\n学号\t姓名\t语文\t数学\t英语\t物理\t化学\t总分\t平均分\n");
printf("%3d\t%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%6.1f\n", stu_id[i], stu_name[i], chinese[i], math[i], english[i], physics[i], chemistry[i], tp[i], stu_average[i]);
printf("\n");
printf("请重新输入该学生信息:\n");
printf("请输入该学生姓名:");
scanf("%s", stu_name[i]);
stu_id_1:
printf("输入 [%s] 的[学号]: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_stu_id[k]=getchar())!='\n'; k++)
{
if(str_stu_id[k]<'1' || str_stu_id[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto stu_id_1;
}
}
stu_id[i] = atoi(str_stu_id);
if(stu_id[i]<1 || stu_id[i]>99)
{
printf("[学号]超过规定范围(1-99),请重新输入\n");
goto stu_id_1;
}
for(j=0; j<i; j++)
{
if(stu_id[i]==stu_id[j])
{
printf("[学号]已存在,请重新输入:");
goto stu_id_1;
}
}
chinese_1:
printf("输入 [%s] 的[语文]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chinese[k]=getchar())!='\n'; k++)
{
if(str_chinese[k]<'1' || str_chinese[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chinese_1;
}
}
chinese[i] = atoi(str_chinese);
if(chinese[i]<0 || chinese[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chinese_1;
}
math_1:
printf("输入 [%s] 的[数学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_math[k]=getchar())!='\n'; k++)
{
if(str_math[k]<'1' || str_math[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto math_1;
}
}
math[i] = atoi(str_math);
if(math[i]<0 || math[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto math_1;
}
english_1:
printf("输入 [%s] 的[英语]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_english[k]=getchar())!='\n'; k++)
{
if(str_english[k]<'1' || str_english[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto english_1;
}
}
english[i] = atoi(str_english);
if(english[i]<0 || english[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto english;
}
physics_1:
printf("输入 [%s] 的[物理]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_physics[k]=getchar())!='\n'; k++)
{
if(str_physics[k]<'1' || str_physics[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto physics_1;
}
}
physics[i] = atoi(str_physics);
if(physics[i]<0 || physics[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto physics_1;
}
chemistry_1:
printf("输入 [%s] 的[化学]成绩: ", stu_name[i]);
fflush(stdin);
for(k=0; (str_chemistry[k]=getchar())!='\n'; k++)
{
if(str_chemistry[k]<'1' || str_chemistry[k]>'9')
{
printf("非法输入,请重新输入!\n");
goto chemistry_1;
}
}
chemistry[i] = atoi(str_chemistry);
if(chemistry[i]<0 || chemistry[i]>150)
{
printf("超过分数范围(0-150),请重新输入\n");
goto chemistry_1;
}
printf("\n");
tp[i] = chinese[i]+math[i]+english[i]+physics[i]+chemistry[i];
stu_average[i] = tp[i]/5.0;
return(error);
}
}
if(flag==0)
{
error++;
if(error>4)
{
printf("输入错误次数过多,返回菜单项!\n");
return(error);
}
printf("没有该学号或姓,请重新输入:");
goto change_id;
}
}
学生成绩统计程序
最新推荐文章于 2022-07-08 00:15:00 发布