#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 10
#define STU_MAX 30
#define COURSE_MAX 6
struct Stu {
long num;
char name[MAX_LEN];
float score[COURSE_MAX];
float sum;
float aver;
} stu[STU_MAX];
int STU_NUM,COURSE_NUM;
int Menu() {
int itemSelected;
printf("Management for Students' scores\n");
printf("1.Input record\n");
printf("2.Caculate total and average score of every course\n");
printf("3.Caculate total and average score of every student\n");
printf("4.Sort in descending order by total score of every student\n");
printf("5.Search by number\n");
printf("6.List record \n");
printf("0.Exit\n");
printf("Please input your choice:\n");
scanf("%d",&itemSelected);
return itemSelected;
}
void ReadScore() {
int i,j;
printf("Input student's ID,name and score:\n");
for(i=0; i<STU_NUM; i++) {
scanf("%ld%s",&stu.num,stu.name);
for(j=0; j<COURSE_NUM; j++) {
scanf("%f",&stu.score[j]);
}
}
}
void AverSumofEveryCourse() {
int i,j;
float sum[COURSE_MAX],aver[COURSE_MAX];
for(j=0; j<COURSE_NUM; j++) {
sum[j]=0;
for(i=0; i<STU_NUM; i++)
sum[j]+=stu.score[j];
if(STU_NUM>0) aver[j]=1.0*sum[j]/STU_NUM;
else aver[j]=0;
printf("course %d: sum=%.0f,aver=%.2f\n",j+1,sum[j],aver[j]);
}
}
void AverSumofEveryStudent() {
int i,j;
for(i=0; i<STU_NUM; i++) {
stu.sum=0;
for(j=0; j<COURSE_NUM; j++)
stu.sum+=stu.score[j];
if(COURSE_NUM>0) stu.aver=1.0*stu.sum/COURSE_NUM;
else stu.aver=0;
printf("student %d: sum=%.0f, aver=%.0f\n", i+1,stu.sum,stu.aver);
}
}
void SearchbyNum() {
long number;
int i,j;
printf("Input the number you want to search:\n");
scanf("%ld",&number);
for(i=0; i<STU_NUM; i++) {
if(stu.num==number) {
printf("%ld\t%s\t",stu.num,stu.name);
for(j=0; j<COURSE_NUM; j++)
printf("%.0f\t",stu.score[j]);
printf("%.0f\t%.0f\t\n",stu.sum,stu.aver);
return ;
}
}
printf("\nNot found!\n");
}
void SortbyScore() {
struct Stu temp;
int i,j;
for(i=0;i<STU_NUM-1;i++) {
for(j=0;j<STU_NUM-1-i;j++) {
if(stu[j].sum<stu[j+1].sum) {
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
}
void PrintScore() {
int i,j;
for(i=0; i<STU_NUM; i++) {
printf("%ld\t%s\t",stu.num,stu.name);
for(j=0; j<COURSE_NUM; j++) {
printf("%.f\t",stu.score[j]);
}
printf("%.f\t%.f\n",stu.sum,stu.aver);
}
}
main() {
int ch;
int n=0,m=0;
printf("Input student number(n<=%d)\n",STU_MAX);
scanf("%d",&STU_NUM);
printf("Input course number(m<=%d)\n",COURSE_MAX);
scanf("%d",&COURSE_NUM);
while(1) {
ch=Menu();
switch(ch) {
case 1:
ReadScore();
break;
case 2:
AverSumofEveryCourse();
break;
case 3:
AverSumofEveryStudent();
break;
case 4:
SortbyScore();
printf("\nSort in descending order by score:\n");
PrintScore();
break;
case 5:
SearchbyNum();
break;
case 6:
PrintScore();
break;
case 0:
printf("End of program.");
exit(0);
default:
printf("Input error!");
break;
}
}
}