收官加优化
某班有最多不超过30人(具体人数由键盘输入)参加期末考试,考试科目为3门。定义结构体类型,用结构体数组作函数参数,编程实现如下菜单驱动的学生成绩管理系统:
- 录入每个学生的学号、姓名和各科考试成绩;
- 计算每门课程的总分和平均分;
- 算每个学生的总分和平均分;
- 按每个学生的总分由高到低排出名次表;
- 按学号由小到大排出成绩表;
- 姓名的字典顺序排出成绩表;
- 按学号查询学生排名及其考试成绩;
- 按姓名查询学生排名及其考试成绩;
- 按优秀(90-100)、良好(80-89)、中等(70-79)、及格(60-69)、不及格(0-59)5个类别,统计每个类别的人数以及所占的百分比;
- 输出每个学生的学号、姓名、各科考试成绩,总分、平均分,以及每门课程的总分和平均分。
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define N 30
int count = 0;
typedef struct student
{
char name[10];
char ide[10];
float score[3];
float sum;
float ave;
}STU;
int Menu(void);
void Input(STU stu[],int n);
void Caculate(STU stu[],float grade[][2],int n);
void Caculate2(STU stu[],int n);
void Sorthl(STU stu[],int n);
void Sorthl2(STU stu[],int n);
void Sortnumlh(STU stu[],int n);
void SortName(STU stu[],int n);
void Select(STU stu[],int n);
void SelectName(STU stu[],int n);
void Statistics(STU stu[],int n);
void Printf(STU stu[],float grade[][2],int n);
int main() {
char itemSelected;
int n;
float grade[3][2];
STU stu[N];
do{
printf("Please input the number of students:");
scanf("%d",&n);
}while(n<0||n>30);
while(1)
{
itemSelected = Menu();
switch(itemSelected)
{
case 1:
Input(stu,n);
break;
case 2:
Caculate(stu,grade,n);
break;
case 3:
Caculate2(stu,n);
break;
case 4:
Sorthl(stu,n);
break;
case 5:
Sortnumlh(stu,n);
break;
case 6:
SortName(stu,n);
break;
case 7:
Select(stu,n);
break;
case 8:
SelectName(stu,n);
break;
case 9:
Statistics(stu,n);
break;
case 10:
Printf(stu,grade,n);
break;
case 0:
printf("Exit procedure................!\n");
return 0;
default:printf("Input error!\n");
}
}
return 0;
}
//1.menu
int Menu(void)
{
int k;
printf("---------------学生成绩管理系统V5.0------------\n");
printf("|1.Input record |\n");
printf("|2.Caculate total and average score of course |\n");
printf("|3.Calculate the total and average score |\n");
printf("|4.Sort in descending order by total scores |\n");
printf("|5.Sort in ascending order by number |\n");
printf("|6.Sort by dictionary |\n");
printf("|7.Search by number |\n");
printf("|8.Search by name |\n");
printf("|9.Statistic analysis |\n");
printf("|10.list record |\n");
printf("|0.Exit |\n");
printf("-----------------------------------------------\n");
do{
printf("Please enter your choice:");
scanf("%d",&k);
}while(k<0||k>10);
return k;
}
//2.record
void Input(STU stu[],int n)
{
int i,j;
for(i = 0; i<n ; i++)
{
printf("Please input the student's name:");
scanf("%s",stu[i].name);
printf("Please input the student's ID:");
scanf("%s",stu[i].ide);
for(j = 0;j < 3;j++ )
{
printf("Project%ld:",j+1);
scanf("%f",&stu[i].score[j]);
}
}
printf("\n");
count++;
}
//3.1 caculate
void Caculate(STU stu[],float grade[][2],int n)
{
int i,j;
float sum;
if(count!=1)Input(stu,n);
for(j = 0;j < 3;j++)
{
sum = 0;
for(i = 0;i < n;i++)
{
sum = sum +stu[i].score[j];
}
grade[j][0] = sum;
grade[j][1] = sum/n;
}
}
//3.2 caculate2
void Caculate2(STU stu[],int n)
{
int i,j;
float s;
if(count!=1)Input(stu,n);
for ( i = 0; i < n; i++)
{
s = 0;
for(j = 0;j < 3;j++)
{
s += stu[i].score[j];
}
stu[i].ave = s/3;
stu[i].sum = s;
}
}
//4.sort1&print
void Sorthl(STU stu[],int n)
{
Sorthl2(stu,n);
printf("Ranking from high to low by total score\n");
for(int i = 0;i < n;i++)
{
printf("Name:%s AllScore:%.2f\n",stu[i].name,stu[i].sum);
}
}
//4.2
void Sorthl2(STU stu[],int n)
{
Caculate2(stu,n);
int i,j,k;
STU temp;
for(i = 0; i < n; i++)
{
k = i;
for(j = i+1;j < n; j++)
{
if(stu[j].sum > stu[k].sum) k = j;
}
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
//5.sort by Id
void Sortnumlh(STU stu[],int n)
{
if(count!=1)Input(stu,n);
STU temp;
int i,j,k;
for(i = 0; i < n; i++)
{
k = i;
for(j = i+1;j < n; j++)
{
if(strcmp(stu[j].ide,stu[k].ide)>0) k = j;
}
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
printf("Ranking from low to high by num is\n");
for(i = 0; i < n; i++)
{
printf("ID:%s Name:%s project1:%.2f project2:%.2f project3:%.2f\n",stu[i].ide,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
}
//6.sort by name
void SortName(STU stu[],int n)
{
if(count!=1)Input(stu,n);
STU temp;
int i,j,k;
for(i = 0; i < n; i++)
{
k = i;
for(j = i+1;j < n; j++)
{
if(strcmp(stu[j].name,stu[k].name)<0) k = j;
}
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
printf("Ranking from low to high by num is\n");
for(i = 0; i < n; i++)
{
printf("Name:%s ID:%s project1:%.2f project2:%.2f project3:%.2f\n",stu[i].name,stu[i].ide,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
}
//7.select by Id
void Select(STU stu[],int n)
{
char num[10];
Sorthl2(stu,n);
printf("Please input the student's ID:");
scanf("%s",num);
for(int i=0;i<n;i++)
{
if(strcmp(stu[i].ide,num)==0)
printf("ID:%s Name:%s Ranking:%d TotalScore:%.2f \n",stu[i].ide,stu[i].name,i+1,stu[i].sum);
}
}
//8.select by Name
void SelectName(STU stu[],int n)
{
char nam[10];
Sorthl2(stu,n);
printf("Please input the student's Name:");
scanf("%s",nam);
for(int i=0;i<n;i++)
{
if(strcmp(stu[i].name,nam)==0)
printf("Name:%s ID:%s Ranking:%d TotalScore:%f \n",stu[i].name,stu[i].ide,i+1,stu[i].sum);
}
}
//9.statistics
void Statistics(STU stu[],int n)
{
if(count!=1)Input(stu,n);
int excellent,good,medium,pass,fail;
int i,j;
for(j = 0;j < 3; j++)
{
excellent = good = medium = pass = fail = 0;
for(i=0;i<n;i++)
{
if(stu[i].score[j]>=90&&stu[i].score[j]<=100)excellent++;
if(stu[i].score[j]>=80&&stu[i].score[j]<=89)good++;
if(stu[i].score[j]>=70&&stu[i].score[j]<=79)medium++;
if(stu[i].score[j]>=60&&stu[i].score[j]<=69)pass++;
if(stu[i].score[j]>=0 &&stu[i].score[j]<=59)fail++;
}
printf("Project%d:\n",j+1);
printf("Excellent %d excellent total %4.1d%% people\n",excellent,(excellent*100)/n);
printf("Good %d good total %4.1d%% people\n",good,(good*100)/n);
printf("Mediun %d medium total %4.1d%% people\n",medium,(medium*100)/n);
printf("Pass %d pass total %4.1d%% people\n",pass,(pass*100)/n);
printf("Fail %d fail total %4.1d%% people\n",fail,(fail*100)/n);
}
}
//10.print
void Printf(STU stu[],float grade[][2],int n)
{
Caculate(stu,grade,n);
Caculate2(stu,n);
for(int i=0;i<n;i++)
{
printf("Name:%s ID:%s project1:%.2f project2:%.2f project3:%.2f\n",stu[i].name,stu[i].ide,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
printf("TotalScore:%.2f Average:%.2f\n",stu[i].sum,stu[i].ave);
}
printf("Total points for each course:\n");
printf("P1:%.2f P2:%.2f P2:%.2f\n",grade[0][0],grade[1][0],grade[2][0]);
printf("Average points for each course:\n");
printf("P1:%.2f P2:%.2f P2:%.2f\n",grade[0][1],grade[1][1],grade[2][1]);
}