#include <stdio.h>
#include <string.h>
#define MAX 20
//菜单初始化函数
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.按成绩排序:0降序1升序=====\n");
printf("\t\t=====6.查找学生是否在班级中=====\n");
printf("\t\t=====0.退出=====\n");
}
//定义结构体类型
struct stu
{
int id; //学号
char name[20];//名字
char sex[5];//性别
int score;//成绩
};
//学生信息录入函数
int scan_stu(struct stu stu[],int *n){
printf("请输入学生个数:");
scanf("%d",n); //输入学生个数
for (int i = 0; i < *n; i++) // 遍历所有学生信息
{
printf("输入学生 %d 的姓名:", i + 1);
scanf("%s",stu[i].name);
printf("输入学生 %d 的学号:", i + 1);
scanf("%d",&stu[i].id);
printf("输入学生 %d 的性别:", i + 1);
scanf("%s",stu[i].sex);
printf("输入学生 %d 的成绩:", i + 1);
scanf("%d",&stu[i].score);
}
}
//查看学生信息函数
void print_stu(struct stu stu[],int n){
printf("姓名\t学号\t性别\t成绩\n");
for (int i = 0; i < n; i++)
{
printf("%s\t%d\t%s\t%d\n",stu[i].name,stu[i].id,stu[i].sex,stu[i].score);
}
}
//查找成绩最高最低的学生信息
void maxminstu(struct stu stu[],int n){
int max=0;
int min=0;
for (int i = 0; i < n; i++)
{
if(stu[i].score >stu[max].score){
max = i;
}
if(stu[i].score <stu[min].score){
min = i;
}
}
printf("最高成绩的名字为%s,学号为%d,性别为%s,成绩为%d\n",stu[max].name,stu[max].id,stu[max].sex,stu[max].score);
printf("最低成绩的名字为%s,学号为%d,性别为%s,成绩为%d\n",stu[min].name,stu[min].id,stu[min].sex,stu[min].score);
}
//求学生总成绩和平均成绩
void sumavestu(struct stu stu[],int n){
float sum=0;
float ave=0;
for (int i = 0; i < n; i++)
{
sum += stu[i].score;
}
ave = sum/n;
printf("学生的总成绩为%.2f,平均成绩为%.2f\n",sum,ave);
}
//学生成绩排序函数1升序 0降序
void sortstu(struct stu stu[],int n,int flag){
if( flag == 1){
for(int i = 1; i < n; i++){
for(int j = 0; j < n-i; j++){
if(stu[j].score < stu[j+1].score){
struct stu temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}else if(flag == 0 ){
for(int i = 1; i < n; i++){
for(int j = 0; j < n-i; j++){
if(stu[j].score > stu[j+1].score){
struct stu temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
printf("\n");
print_stu(stu,n);
printf("\n");
}
//按名字寻找班级中是否有该同学
int seachstu(struct stu stu[],int n,char *seach){
for (int i = 0; i < n; i++)
{
if (strcmp(stu[i].name,seach) ==0){
return 1;
}
}
return 0;
}
int main(int argc, char const *argv[])
{
int menu=0;
struct stu student[MAX];
int n=0;
while (1)
{
print_menu();
printf("请输入要使用的功能:");
scanf("%d",&menu);
switch (menu)
{
case 1:
scan_stu(student,&n);
break;
case 2:
print_stu(student, n);
break;
case 3:
maxminstu(student,n);
break;
case 4:
sumavestu(student,n);
break;
case 5:{
int flag=0;
printf("请输入你要查询的排序:升序1降序0:");
scanf("%d",&flag);
sortstu(student,n,flag);}
break;
case 6:{
char seach[MAX]="";
printf("请输入你要查找的学生姓名:");
scanf("%s",&seach);
if(seachstu(student,n,seach)){
printf("%s该名字存在在班级中\n",seach);
}else
{
printf("%s该名字不存在在班级中\n",seach);
}
}
break;
case 0: goto END;
default:printf("输入的功能有误,请重新输入\n");
}
}
END:
return 0;
}