设计结构体数组,结构中包含学生数据为:学号、姓名、物理分数、外语分数、计算机分数、数学分数。设计各个函数,分别实现以下功能:
(1)录入:输入学生数据
(2)显示:所有学生信息
(3)统计:统计每科的最高分、最低分,输出不及格人数、不及格学生的数据
设计菜单,通过选择菜单调用以上各函数
/*
输入样例:
1 li 95 96 93 98
2 ji 96 93 93 51
3 yi 98 51 63 47
4 fu 65 87 25 98
5 po 87 98 69 69
6 wu 85 89 35 39
7 pi 85 84 75 98
8 nl 85 93 28 98
9 ok 96 32 85 98
10 pl 66 68 95 98
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 10
typedef struct student
{
int num;
char name[30];
int Math;
int English;
int Program;
int Physics;
float Final_Score;
} STUDENT;
int guake = 60;
float Math_average = 0;
float English_average = 0;
float Program_average = 0;
float Physics_average = 0;
float Math_sum = 0;
float English_sum = 0;
float Program_sum = 0;
float Physics_sum = 0;
int Math_count = 0;
int English_count = 0;
int Program_count = 0;
int Physics_count = 0;
int i = 0, command;
int gua_count = 0;
void showMenu();
void Input_data(STUDENT myclass[]);
void Show_info(STUDENT myclass[]);
void tongji(STUDENT myclass[]);
void show_stu(STUDENT myclass[], char name[]);
void sort_danke(STUDENT arr[], char name[], int size);
int main(void)
{
char name[30];
STUDENT myclass[N];
showMenu();
printf("请选择:");
scanf("%d", &command);
printf("\n");
while (1) //根据command的值执行不同的值
{
switch (command)
{
case 1:
{
printf("学生信息录入:\n");
Input_data(myclass);
};
break;
case 2:
{
printf("学生信息显示成功!\n");
Show_info(myclass);
printf("学生信息显示成功!\n");
};
break;
case 3:
tongji(myclass);
break;
case 4:
{
printf("请输入您要按哪一门单科成绩排序(请输入Math,English,Program,Physics):\n");
scanf("%s", &name);
sort_danke(myclass, name, N);
};
break;
case 0:
exit(0); //退出系统
}
printf("按任意健继续");
getch(); //让程序暂停一下,按任意键继续
system("cls"); //清除屏幕
showMenu();
printf("请选择:");
scanf("%d", &command);
}
system("pause");
return 0;
}
void showMenu()
{
printf("------学生信息管理系统V2.0--------\n");
printf("* 1. 学生信息录入 *\n");
printf("* 2. 学生信息显示 *\n");
printf("* 3. 含有不及格成绩的学生 *\n");
printf("* 4. 单科最高分最低分 *\n");
printf("* 0. 退出系统 *\n");
printf("----------------------------------\n");
}
void Input_data(STUDENT myclass[])
{
printf("请输入学生的:学号,姓名,数学成绩,英语成绩,计算机成绩,物理成绩\n");
for (i = 0; i < N; i++)
{
printf("第%d名学生:", i + 1);
scanf("%d%s%d%d%d%d", &myclass[i].num, &myclass[i].name, &myclass[i].Math, &myclass[i].English, &myclass[i].Program, &myclass[i].Physics);
if (myclass[i].Program > 100 || myclass[i].Program < 0 || myclass[i].Math > 100 || myclass[i].Math < 0 || myclass[i].English > 100 || myclass[i].English < 0 || myclass[i].Physics < 0 || myclass[i].Physics > 100 )
{
printf("输入有误,请重新输入!\n");
printf("请输入学生的:学号,姓名,数学成绩,英语成绩,计算机成绩,物理成绩\n");
printf("第%d名学生:", i + 1);
scanf("%d%s%d%d%d%d ", &myclass[i].num, &myclass[i].name, &myclass[i].Math, &myclass[i].English, &myclass[i].Program, &myclass[i].Physics);
}
myclass[i].Final_Score = (myclass[i].Math + myclass[i].English + myclass[i].Physics + myclass[i].Program) / 4;
}
}
void Show_info(STUDENT myclass[])
{
for (i = 0; i < N; i++)
{
printf("第%d个学生的信息:学号:%d 姓名:%s 数学成绩:%d 英语成绩:%d 编程成绩:%d 物理成绩:%d 平均成绩:%.2f\n", i + 1, myclass[i].num, myclass[i].name, myclass[i].Math, myclass[i].English, myclass[i].Program, myclass[i].Physics, myclass[i].Final_Score);
}
}
void tongji(STUDENT myclass[])
{
printf("挂科的学生信息:\n");
for (i = 0; i < N; i++)
{
if (myclass[i].Math < guake || myclass[i].English < guake || myclass[i].Program < guake || myclass[i].Physics < guake)
{
gua_count++;
printf("学号:%d 姓名:%s 数学成绩:%d 英语成绩:%d 编程成绩:%d 物理成绩:%d 平均成绩:%.2f\n", myclass[i].num, myclass[i].name, myclass[i].Math, myclass[i].English, myclass[i].Program, myclass[i].Physics, myclass[i].Final_Score);
}
}
printf("挂科人数:%d", gua_count);
}
void sort_danke(STUDENT arr[], char name[], int size)
{
int flag = 0;
for (i = 0; i < N; i++)
{
if (strcmp(name, "Math") == 0)
{
flag = 1;
}
if (strcmp(name, "English") == 0)
{
flag = 2;
}
if (strcmp(name, "Program") == 0)
{
flag = 3;
}
if (strcmp(name, "Physics") == 0)
{
flag = 4;
}
}
int j, i, temp_num = 0, t = 0;
int Math = 0;
int English = 0;
int Program = 0;
int Physics = 0;
int Computer_exam = 0;
float Final_Score = 0;
char temp_name[30];
for (i = 0; i < size - 1; i++) // size-1是因为不用与自己比较,所以比的数就少一个
{
int count = 0;
for (j = 0; j < size - 1 - i; j++) // size-1-i是因为每一趟就会少一个数比较
{
if (flag == 1)
{
if (arr[j].Math < arr[j + 1].Math) //这是降序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
temp_num = arr[j].num;
arr[j].num = arr[j + 1].num;
arr[j + 1].num = temp_num;
Math = arr[j].Math;
arr[j].Math = arr[j + 1].Math;
arr[j + 1].Math = Math;
English = arr[j].English;
arr[j].English = arr[j + 1].English;
arr[j + 1].English = English;
Program = arr[j].Program;
arr[j].Program = arr[j + 1].Program;
arr[j + 1].Program = Program;
Physics = arr[j].Physics;
arr[j].Physics = arr[j + 1].Physics;
arr[j + 1].Physics = Physics;
Final_Score = arr[j].Final_Score;
arr[j].Final_Score = arr[j + 1].Final_Score;
arr[j + 1].Final_Score = Final_Score;
for (t = 0; t < 30; t++)
{
temp_name[t] = arr[j].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j].name[t] = arr[j + 1].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j + 1].name[t] = temp_name[t];
}
count = 1;
}
}
if (flag == 2)
{
if (arr[j].English < arr[j + 1].English) //这是降序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
temp_num = arr[j].num;
arr[j].num = arr[j + 1].num;
arr[j + 1].num = temp_num;
Math = arr[j].Math;
arr[j].Math = arr[j + 1].Math;
arr[j + 1].Math = Math;
English = arr[j].English;
arr[j].English = arr[j + 1].English;
arr[j + 1].English = English;
Program = arr[j].Program;
arr[j].Program = arr[j + 1].Program;
arr[j + 1].Program = Program;
Physics = arr[j].Physics;
arr[j].Physics = arr[j + 1].Physics;
arr[j + 1].Physics = Physics;
Final_Score = arr[j].Final_Score;
arr[j].Final_Score = arr[j + 1].Final_Score;
arr[j + 1].Final_Score = Final_Score;
for (t = 0; t < 30; t++)
{
temp_name[t] = arr[j].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j].name[t] = arr[j + 1].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j + 1].name[t] = temp_name[t];
}
count = 1;
}
}
if (flag == 3)
{
if (arr[j].Program < arr[j + 1].Program) //这是降序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
temp_num = arr[j].num;
arr[j].num = arr[j + 1].num;
arr[j + 1].num = temp_num;
Math = arr[j].Math;
arr[j].Math = arr[j + 1].Math;
arr[j + 1].Math = Math;
English = arr[j].English;
arr[j].English = arr[j + 1].English;
arr[j + 1].English = English;
Program = arr[j].Program;
arr[j].Program = arr[j + 1].Program;
arr[j + 1].Program = Program;
Physics = arr[j].Physics;
arr[j].Physics = arr[j + 1].Physics;
arr[j + 1].Physics = Physics;
Final_Score = arr[j].Final_Score;
arr[j].Final_Score = arr[j + 1].Final_Score;
arr[j + 1].Final_Score = Final_Score;
for (t = 0; t < 30; t++)
{
temp_name[t] = arr[j].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j].name[t] = arr[j + 1].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j + 1].name[t] = temp_name[t];
}
count = 1;
}
}
if (flag == 4)
{
if (arr[j].Physics < arr[j + 1].Physics) //这是降序排法,前一个数和后一个数比较,如果前数大则与后一个数换位置
{
temp_num = arr[j].num;
arr[j].num = arr[j + 1].num;
arr[j + 1].num = temp_num;
Math = arr[j].Math;
arr[j].Math = arr[j + 1].Math;
arr[j + 1].Math = Math;
English = arr[j].English;
arr[j].English = arr[j + 1].English;
arr[j + 1].English = English;
Program = arr[j].Program;
arr[j].Program = arr[j + 1].Program;
arr[j + 1].Program = Program;
Physics = arr[j].Physics;
arr[j].Physics = arr[j + 1].Physics;
arr[j + 1].Physics = Physics;
Final_Score = arr[j].Final_Score;
arr[j].Final_Score = arr[j + 1].Final_Score;
arr[j + 1].Final_Score = Final_Score;
for (t = 0; t < 30; t++)
{
temp_name[t] = arr[j].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j].name[t] = arr[j + 1].name[t];
}
for (t = 0; t < 30; t++)
{
arr[j + 1].name[t] = temp_name[t];
}
count = 1;
}
}
}
if (count == 0) //如果某一趟没有交换位置,则说明已经排好序,直接退出循环
break;
}
if(flag==1)
{
printf("%s这门课的最高分为:%d,最低分为:%d", name, arr[0].Math, arr[N - 1].Math);
}
if(flag==2)
{
printf("%s这门课的最高分为:%d,最低分为:%d", name, arr[0].English, arr[N - 1].English);
}
if(flag==3)
{
printf("%s这门课的最高分为:%d,最低分为:%d", name, arr[0].Program, arr[N - 1].Program);
}
if(flag==4)
{
printf("%s这门课的最高分为:%d,最低分为:%d", name, arr[0].Physics, arr[N - 1].Physics);
}
}