📄 grade_manager.c
字号:
#include <stdio.h>
#include <stdlib.h>
#define N 50
struct student_info
{
int sno;
char name[81];
int scores[3];
double average;
};
struct student_info add_student_info();
void display_student_info(struct student_info stu[] , int size);
void sort_by_average(struct student_info stu[],int size);
int show_menu();
void insert_student_info(struct student_info stu[],int size);
void del_student_info(struct student_info stu[],int size);
int main(void)
{
struct student_info students[N];
int count = 0,choice;
char ch = 'Y';
printf("请输入学员的信息");
do
{
students[count] = add_student_info();
count++;
printf("是否继续输入学员信息(y/n):");
fflush(stdin);
} while((ch = getchar()) != 'n' && count < N);
printf("\n排序前学员信息为:");
display_student_info(students,count);
sort_by_average(students,count);
printf("\n排序后学员信息为:");
display_student_info(students,count);
while(1)
{
choice = show_menu();
switch(choice)
{
case 1:
insert_student_info(students,count);
count = (count > N) ? count : (count + 1);
break;
case 2:
del_student_info(students,count);
count = (count <= 0) ? 0 : (count - 1);
break;
case 3:
display_student_info(students,count);
break;
case 4:
exit(1);
break;
default:
printf("请正确输入..");
}
}
}
struct student_info add_student_info()
{
int i,sum = 0;
struct student_info student;
printf("\n请输入该学员的学号:");
scanf("%d",&student.sno);
fflush(stdin);
printf("\n请输入该学员的姓名:");
gets(student.name);
printf("\n请输入该学员的三门考试成绩:");
for(i = 0; i < 3; i++)
{
scanf("%d",&student.scores[i]);
sum += student.scores[i];
}
student.average = sum / 3.0;
return student;
}
void display_student_info(struct student_info stu[] , int size)
{
int i;
for(i = 0; i < size; i++)
{
printf("\n--------------------------------------------\n");
printf("\n学号为:%d",stu[i].sno);
printf("\n姓名为:%s",stu[i].name);
printf("\n平均成绩为:%.1lf",stu[i].average);
printf("\n--------------------------------------------\n\n");
}
}
void sort_by_average(struct student_info stu[],int size)
{
int i,j;
struct student_info temp;
for(i = 0; i < size - 1; i++)
{
for(j = 0; j < size - 1 - i; j++)
{
if(stu[j].average < stu[j + 1].average)
{
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
int show_menu()
{
int choice;
printf("\n1.插入学员信息");
printf("\n2.删除学员信息");
printf("\n3.显示学员信息");
printf("\n4.退出");
printf("\n请选择.....");
scanf("%d",&choice);
return choice;
}
void insert_student_info(struct student_info stu[],int size)
{
struct student_info student;
int i,location = -1;
if(size >= N)
{
printf("\n数组一满,请先删除..");
return;
}
student = add_student_info();
for(i = 0; i < size; i++) //查找相应的位置
{
if(student.average > stu[i].average)
{
break;
}
}
location = i;
for(i = size; i >= location; i--) //移动元素
{
stu[i + 1] = stu[i];
}
stu[location] = student; //插入
}
void del_student_info(struct student_info stu[],int size)
{
int sno,i,location = -1;
if(size <= 0)
{
printf("当前没有学员的信息...");
}
else
{
printf("请输入要删除的学员的学号:");
scanf("%d",&sno);
for(i = 0; i < size; i++) //查找要删除的学号,记录位置
{
if(stu[i].sno == sno)
{
break;
}
}
location = i;
for(i = location; i < size; i++)
{
stu[i] = stu[i + 1];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -