#include<stdio.h> #include<Windows.h> #include<conio.h> #define StudentNumber 30 //Maximum number of students #define SubjectNumber 6 //Maximum number of subjects #define NameLength 20 //Maximum number of name_length typedef struct student{ long int num; char name[NameLength]; float score[SubjectNumber]; float sum; }STU; STU s[StudentNumber]; int itemSelected=-1; int n; //StudentNumber int m; //SubjectNumber void inputScore(); //Enter basic information void SortByGrade(); void printScore(); int Menu(){ system("cls"); printf("\n ==============Student achievement management system===============\n"); printf("\t 1.Enter your student id number, name and grade for each subject\n"); printf("\t 2.A league table is arranged according to each student's total score from highest to lowest\n"); printf("\t 3.Output information about each student\n"); printf("\t 0.Quit\n"); printf("======================================================================\n"); printf("Please select options:"); scanf( "%d",&itemSelected); system("cls"); return itemSelected; } void chose(int a){ switch(a){ case 1: inputScore(); break; case 2: SortByGrade();break; case 3: printScore();break; case 0: printf("About to exit program"); exit(0); default: printf("Input error!");break; } } int main() { char a; printf("Please enter the total number of students(n<%d):\n",StudentNumber); scanf("%d",&n); getchar(); printf("Please enter the total number of subjects(m<%d):\n",SubjectNumber); scanf("%d",&m); getchar(); system("cls"); while(1){ itemSelected=Menu(); chose(itemSelected); } } void inputScore(){ printf("\n===============================================\n"); printf("Please input student ID, name and subject information (enter 0 to stop information input) \n"); int i,j; for(i=0;i<n;i++){ printf("Student id (8):"); scanf("%ld",&s[i].num); if(s[i].num==0) break; getchar(); printf("Name:"); gets(s[i].name); printf("Please enter your scores for each subject:"); s[i].sum=0; for(j =0;j<m;j++){ scanf("%f",&s[i].score[j]); s[i].sum +=s[i].score[j]; } system("cls"); } } void SortByGrade(){ //Sorted from highest to lowest overall score STU temp; int i,j,k; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(s[j].sum<s[j+1].sum){ temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } } } printf("\n============Sorted from highest to lowest overall score===========\n"); for(j=0;j<n;j++){ printf("The overall score ranking %d is:",j+1); for(k=0;k<NameLength;k++){ printf("%c",s[j].name[k]); } printf("\nThe student's total score is:%.2f\n",s[j].sum); } getch(); } void printScore(){ printf("==============Below is the student information==============\n"); int j,k,c; for(j=0;j<n;j++){ printf("The student's student id is:%ld\n",s[j].num); printf("The student's name is:"); for(k=0;k<NameLength;k++){ printf("%c",s[j].name[k]); } printf("\nA student's scores for each subject is:\n"); for(c=0;c<m;c++){ printf("The %d subject score is:%.2f\n",c+1,s[j].score[c]); } printf("The student's total score is:%.2f",s[j].sum); printf("\n"); } getch(); }