1、使用结构体完成学生(学号、姓名、性别、成绩)管理系统
1> 使用菜单实现
2> 功能1:完成对学生信息的录入,确定人数,完成输入
2> 功能2:完成对学生信息的输出
3> 功能3:输出成绩最高和最低学生的信息
4> 功能4:输出学生的总成绩和平均成绩
5> 功能5:对学生信息按成绩进行排序,根据传入的升降序,确定排序功能、
6> 功能6:提示并输入要查找的学生名字,查找该名字是否存在于该班级
7> 功能0:退出
#include <stdio.h>
#include <string.h>
#define MAX 10
int studentsNumber = 0;
struct Xuesheng
{
char name[20];
char gender[10];
char id[20];
int score;
};
void print_menu();
void enterStudents(struct Xuesheng *xuesheng);
void viewStudents(struct Xuesheng *xuesheng);
void maxminStudents(struct Xuesheng *xuesheng);
void sum_average_Students(struct Xuesheng *xuesheng);
void paixuStudents(struct Xuesheng *xuesheng);
void searchname_Students(struct Xuesheng *xuesheng);
int main(int argc, char const *argv[])
{
struct Xuesheng xuesheng[MAX];
memset(xuesheng, 0, sizeof(xuesheng));
int n = 0;
while (1)
{
print_menu();
printf("请输入需要实现的功能:");
scanf("%d", &n);
getchar();
switch (n)
{
case 1:
printf("请输入学生个数:");
scanf("%d", &studentsNumber);
getchar();
enterStudents(xuesheng);
break;
case 2:
viewStudents(xuesheng);
break;
case 3:
maxminStudents(xuesheng);
break;
case 4:
sum_average_Students(xuesheng);
break;
case 5:
paixuStudents(xuesheng);
break;
case 6:
searchname_Students(xuesheng);
break;
case 0:
goto END;
default:
break;
}
}
END:
return 0;
}
// 视图
void print_menu()
{
printf("\t\t======1.录入学生信息=========================\n");
printf("\t\t======2.查看学生信息=========================\n");
printf("\t\t======3.输出成绩最高和最低的学生信息=========\n");
printf("\t\t======4.输出学生的总成绩和平均成绩===========\n");
printf("\t\t======5.按成绩进行升序排序===================\n");
printf("\t\t======6.查找学生姓名=========================\n");
printf("\t\t======0.退出=================================\n");
}
// 1.录入学生信息
void enterStudents(struct Xuesheng *xuesheng)
{
for (int i = 0; i < studentsNumber; i++)
{
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", xuesheng[i].name);
printf("请输入第%d个学生的性别:", i + 1);
scanf("%s", xuesheng[i].gender);
printf("请输入第%d个学生的学号:", i + 1);
scanf("%s", xuesheng[i].id);
printf("请输入第%d个学生的成绩:", i + 1);
scanf("%d", &xuesheng[i].score);
printf("\n");
}
}
// 2.查看学生信息
void viewStudents(struct Xuesheng *xuesheng)
{
printf("姓名\t性别\t学号\t成绩\n");
for (int i = 0; i < studentsNumber; i++)
{
printf("%s\t%s\t%s\t%d\n", xuesheng[i].name, xuesheng[i].gender, xuesheng[i].id, xuesheng[i].score);
}
printf("\n");
}
//3.输出成绩最高和最低的学生信息
void maxminStudents(struct Xuesheng *xuesheng)
{
struct Xuesheng max = xuesheng[0];
struct Xuesheng min = xuesheng[0];
for(int i = 0;i < studentsNumber;i++)
{
if(xuesheng[i].score > max.score)
{
max = xuesheng[i];
}
if(xuesheng[i].score < min.score)
{
min = xuesheng[i];
}
}
printf("成绩最高的学生:\n");
printf("姓名\t性别\t学号\t成绩\n");
printf("%s\t%s\t%s\t%d\n\n", max.name, max.gender, max.id, max.score);
printf("成绩最低的学生:\n");
printf("姓名\t性别\t学号\t成绩\n");
printf("%s\t%s\t%s\t%d\n\n", min.name, min.gender, min.id, min.score);
}
//4.输出学生的总成绩和平均成绩
void sum_average_Students(struct Xuesheng *xuesheng)
{
float sum = 0;
float average = 0;
for(int i = 0;i < studentsNumber;i++)
{
sum += xuesheng[i].score;
}
average = sum / studentsNumber;
printf("学生总成绩是:%.2lf\n",sum);
printf("学生平均成绩是:%.2lf\n\n",average);
}
//5.按成绩进行升序排序
void paixuStudents(struct Xuesheng *xuesheng)
{
for(int i = 1;i < studentsNumber;i++)
{
for(int j = 0;j < studentsNumber - i;j++)
{
if(xuesheng[j].score > xuesheng[j+1].score)
{
struct Xuesheng temp = xuesheng[j];
xuesheng[j] = xuesheng[j+1];
xuesheng[j+1] = temp;
}
}
}
viewStudents(xuesheng);
}
//6.查找学生姓名
void searchname_Students(struct Xuesheng *xuesheng)
{
printf("请输入你需要查找的学生姓名:");
char studentName[20] = {"0"};
int count = 0;
scanf("%s",studentName);
for(int i = 0;i < studentsNumber;i++)
{
if(strcmp(studentName,xuesheng[i].name) == 0)
{
count++;
}
}
if(count != 0)
{
printf("该学生在这个班里\n\n");
}
else
{
printf("该学生不在这个班里\n\n");
}
}
输出结果如下: