1、在原来作业的基础上添加一个计算平均值和按照平均值排序以及删除指定学号的学生信息
2、重新整理输出格式
代码
#include<stdio.h>
typedef struct student
{
int num;
char name[128];
float chinese;
float math;
float english;
float avg;
}STU;
int count=0;
void student_add(STU *stu);
void student_math(STU *stu);
void student_chinese(STU *stu);
void student_english(STU *stu);
void student_avg(STU *stu);
void student_clear(STU *stu);
void student_display(STU *stu);
int main(int argc, char const *argv[])
{
STU stu[100];
int input;
while(1)
{
printf("***********功能选择*************\n");
printf("*******1.添加学生信息***********\n");
printf("*******2.以语文成绩进行排序*****\n");
printf("*******3.以数学成绩进行排序*****\n");
printf("*******4.以英语成绩进行排序*****\n");
printf("*******5.以平均成绩进行排序*****\n");
printf("*******6.删除学生信息***********\n");
printf("*******7.退出程序**************\n");
printf("请输入要进行的操作>");
scanf("%d",&input);
switch(input)
{
case 1:student_add(stu);
break;
case 2:student_chinese(stu);
break;
case 3:student_math(stu);
break;
case 4:student_english(stu);
break;
case 5:student_avg(stu);
break;
case 6:student_clear(stu);
break;
case 7:
printf("程序正常退出\n");
return 0;
break;
default:
printf("输入错误,请重新输入\n");
break;
}
student_display(stu);
}
return 0;
}
void student_add(STU *stu)
{
int a;
while(1)
{
printf("请输入要添加的学生学号>");
scanf("%d",&stu[count].num);
printf("请输入要添加的学生姓名>");
scanf("%s",stu[count].name);
printf("请输入要添加的学生语文成绩>");
scanf("%f",&stu[count].chinese);
printf("请输入要添加的学生数学成绩>");
scanf("%f",&stu[count].math);
printf("请输入要添加的学生英语成绩>");
scanf("%f",&stu[count].english);
printf("此学生的平均成绩为:\n");
stu[count].avg=(stu[count].chinese+stu[count].math+stu[count].english)/3;
printf("%g\n",stu[count].avg);
count++;
printf("学生信息录入完毕,请选择是否继续添加 1:添加 0:返回上一级菜单>");
scanf("%d",&a);
if(a!=1)
break;
}
}
void student_english(STU *stu)
{
int i, j;
STU tmp;
for(i = 0; i < count-1; i++)
{
for(j = 0; j < count-i-1; j++)
{
if(stu[j].english< stu[j+1].english)
{
tmp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = tmp;
}
}
}
}
void student_chinese(STU *stu)
{
int i, j;
STU tmp;
for(i = 0; i < count-1; i++)
{
for(j = 0; j < count-i-1; j++)
{
if(stu[j].chinese< stu[j+1].chinese)
{
tmp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = tmp;
}
}
}
}
void student_math(STU *stu)
{
int i, j;
STU tmp;
for(i = 0; i < count-1; i++)
{
for(j = 0; j < count-i-1; j++)
{
if(stu[j].math< stu[j+1].math)
{
tmp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = tmp;
}
}
}
}
void student_avg(STU *stu)
{
int i, j;
STU tmp;
for(i = 0; i < count-1; i++)
{
for(j = 0; j < count-i-1; j++)
{
if(stu[j].avg< stu[j+1].avg)
{
tmp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = tmp;
}
}
}
}
void student_clear(STU *stu)
{
int i, j;
int a=0,b=0;
STU tmp;
for(i = 0; i < count; i++)
{
if(stu[i].num==a)
{
b=i;
break;
}
}
for(i=b; i < count; i++)
{
stu[i]=stu[i+1];
}
count--;
}
void student_display(STU *stu)
{
printf("总共%d个学员:\n",count);
for(int i=0;i<count;i++)
{
printf("学号:%d 姓名:%s 语文成绩:%.2f 数学成绩:%.2f 英语成绩:%.2f 平均成绩:%.2f\n",stu[i].num,stu[i].name,stu[i].chinese,stu[i].math,stu[i].english,stu[i].avg);
}
}
运行结果
***********功能选择*************
*******1.添加学生信息***********
*******2.以语文成绩进行排序*****
*******3.以数学成绩进行排序*****
*******4.以英语成绩进行排序*****
*******5.以平均成绩进行排序*****
*******6.删除学生信息***********
*******7.退出程序**************
请输入要进行的操作>1
请输入要添加的学生学号>101
请输入要添加的学生姓名> 刘雯洁
请输入要添加的学生语文成绩>95
请输入要添加的学生数学成绩>87
请输入要添加的学生英语成绩>79
此学生的平均成绩为:
87
学生信息录入完毕,请选择是否继续添加 1:添加 0:返回上一级菜单>1
请输入要添加的学生学号>102
请输入要添加的学生姓名>小明
请输入要添加的学生语文成绩>80
请输入要添加的学生数学成绩>90
请输入要添加的学生英语成绩>96
此学生的平均成绩为:
88.6667
学生信息录入完毕,请选择是否继续添加 1:添加 0:返回上一级菜单>1
请输入要添加的学生学号>103
请输入要添加的学生姓名>小马
请输入要添加的学生语文成绩>78
请输入要添加的学生数学成绩>89
请输入要添加的学生英语成绩>90
此学生的平均成绩为:
85.6667
学生信息录入完毕,请选择是否继续添加 1:添加 0:返回上一级菜单>1
请输入要添加的学生学号>104
请输入要添加的学生姓名>小丽
请输入要添加的学生语文成绩>99
请输入要添加的学生数学成绩>98
请输入要添加的学生英语成绩>66
此学生的平均成绩为:
87.6667
学生信息录入完毕,请选择是否继续添加 1:添加 0:返回上一级菜单>0
总共4个学员:
学号:101 姓名:刘雯洁 语文成绩:95.00 数学成绩:87.00 英语成绩:79.00 平均成绩:87.00
学号:102 姓名:小明 语文成绩:80.00 数学成绩:90.00 英语成绩:96.00 平均成绩:88.67
学号:103 姓名:小马 语文成绩:78.00 数学成绩:89.00 英语成绩:90.00 平均成绩:85.67
学号:104 姓名:小丽 语文成绩:99.00 数学成绩:98.00 英语成绩:66.00 平均成绩:87.67
***********功能选择*************
*******1.添加学生信息***********
*******2.以语文成绩进行排序*****
*******3.以数学成绩进行排序*****
*******4.以英语成绩进行排序*****
*******5.以平均成绩进行排序*****
*******6.删除学生信息***********
*******7.退出程序**************
请输入要进行的操作>5
总共4个学员:
学号:102 姓名:小明 语文成绩:80.00 数学成绩:90.00 英语成绩:96.00 平均成绩:88.67
学号:104 姓名:小丽 语文成绩:99.00 数学成绩:98.00 英语成绩:66.00 平均成绩:87.67
学号:101 姓名:刘雯洁 语文成绩:95.00 数学成绩:87.00 英语成绩:79.00 平均成绩:87.00
学号:103 姓名:小马 语文成绩:78.00 数学成绩:89.00 英语成绩:90.00 平均成绩:85.67
***********功能选择*************
*******1.添加学生信息***********
*******2.以语文成绩进行排序*****
*******3.以数学成绩进行排序*****
*******4.以英语成绩进行排序*****
*******5.以平均成绩进行排序*****
*******6.删除学生信息***********
*******7.退出程序**************
请输入要进行的操作>6
总共3个学员:
学号:104 姓名:小丽 语文成绩:99.00 数学成绩:98.00 英语成绩:66.00 平均成绩:87.67
学号:101 姓名:刘雯洁 语文成绩:95.00 数学成绩:87.00 英语成绩:79.00 平均成绩:87.00
学号:103 姓名:小马 语文成绩:78.00 数学成绩:89.00 英语成绩:90.00 平均成绩:85.67
***********功能选择*************
*******1.添加学生信息***********
*******2.以语文成绩进行排序*****
*******3.以数学成绩进行排序*****
*******4.以英语成绩进行排序*****
*******5.以平均成绩进行排序*****
*******6.删除学生信息***********
*******7.退出程序**************
请输入要进行的操作>6
总共2个学员:
学号:101 姓名:刘雯洁 语文成绩:95.00 数学成绩:87.00 英语成绩:79.00 平均成绩:87.00
学号:103 姓名:小马 语文成绩:78.00 数学成绩:89.00 英语成绩:90.00 平均成绩:85.67
***********功能选择*************
*******1.添加学生信息***********
*******2.以语文成绩进行排序*****
*******3.以数学成绩进行排序*****
*******4.以英语成绩进行排序*****
*******5.以平均成绩进行排序*****
*******6.删除学生信息***********
*******7.退出程序**************
请输入要进行的操作>7
程序正常退出