实验5-2学生成绩管理系统V5.0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
///
//in this total program, I replace the array with structure
//and create an array with struct pointer//
//last using -> to quote the id/score/names
///
struct STUDENT{
    float score[3];
    long id;
    char names[20];
};
typedef struct STUDENT student;//simplify the struct STUDENT
typedef struct STUDENT *Pstudent;

void print();
void append();
void course_total();
void student_total();
void score_sort(int (*compare)(float a,float b));
void number_sort();
void name_sort(Pstudent names_[30]);
void number_search();
void name_search();
void statistic(Pstudent scores_[30]);

void show(int i);

int ascend(float a, float b){
    if(a>b) return 1;
    else  return 0;
}
int descend(float a, float b){
    if(a<b)  return 1;
    else  return 0;
}
int n;//the number of students

//score [0] MT
//score [1] EN
//score [2] PH

int flg=1;//true print the result
student *stuArray[30];//the global variable can simplify the compute

int again=1;//whether to continue
int main(){
    //static Pstudent stuArray=(STUDENT *)malloc(sizeof(STUDENT)*30);
    int i;
    printf("Input student number(n<30):");
    scanf("%d",&n);
    int choice;
    while(again){
        print();
        scanf("%d",&choice);
        switch(choice){
            case 1:
                append();
                break;
            case 2:
                course_total();//use flag to define whether to print
                break;
            case 3:
                student_total();
                break;
            case 4:
                score_sort(descend);
                if(flg){
                    printf("Sort in descending order by total score of every student:\n");
                    printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
                    for(i=0;i<n;i++)
                        show(i);
                }
                break;
            case 5:
                score_sort(descend);
                if(flg){
                    printf("Sort in ascending order by total score of every student:\n");
                    printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
                    for(i=0;i<n;i++)
                        show(n-1-i);
                }
                break;
            case 6:
                number_sort();
                break;
            case 7:
                name_sort(stuArray);
                break;
            case 8:
                number_search();
                break;
            case 9:
                name_search();
                break;
            case 10:
                statistic(stuArray);
                break;
            case 0:
                again=0;
                printf("End of program!\n");
                break;
            default:
                printf("Input error!\n");
                break;
        }

    }
return 0;
}

void print(){
    printf("\nManagement for Students' scores\n");
    printf("1.Append record\n");
    printf("2.Calculate total and average score of every course\n");
    printf("3.Calculate total and average score of every student\n");
    printf("4.Sort in descending order by total score of every student\n");
    printf("5.Sort in ascending order by total score of every student\n");
    printf("6.Sort in ascending order by number\n");
    printf("7.Sort in dictionary order by name\n");
    printf("8.Search by number\n");
    printf("9.Search by name\n");
    printf("10.Statistic analysis\n");
    printf("0.Exit\n");
    printf("Please Input your choice:");
}
void append(){
    int i;
    printf("Input student's ID,name and score:\n");
    for(i=0;i<n;i++){////the most significant part malloc the memory when appending record
        stuArray[i] = (student *)malloc(sizeof(student));
        scanf("%ld%s",&stuArray[i]->id,stuArray[i]->names);
        scanf("%f",&stuArray[i]->score[0]);
        scanf("%f",&stuArray[i]->score[1]);
        scanf("%f",&stuArray[i]->score[2]);
    }
}
void course_total(){
    int i;
    float sum0=0.0,sum1=0.0,sum2=0.0;
    for(i=0;i<n;i++){
        sum0+=stuArray[i]->score[0];
        sum1+=stuArray[i]->score[1];
        sum2+=stuArray[i]->score[2];
    }
    if(flg){
        printf("course %d:sum=%.0f,aver=%.0f\n",1,sum0,sum0/n);
        printf("course %d:sum=%.0f,aver=%.0f\n",2,sum1,sum1/n);
        printf("course %d:sum=%.0f,aver=%.0f\n",3,sum2,sum2/n);
    }
}
void student_total(){
    float total[30]={0.0};
    int i;
    for(i=0;i<n;i++){
        total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
    }
    if(flg){
        for(i=0;i<n;i++)
            printf("student %d:sum=%.0f,aver=%.0f\n",i+1,total[i],total[i]/3);
    }
}
void score_sort(int (*compare)(float a,float b)){
    int i,j;
    float total[30]={0.0};
    for(i=0;i<n;i++){
        total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
    }
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++)
            //if((*compare)(stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2],stuArray[j]->score[0]+stuArray[j]->score[1]+stuArray[j]->score[2])==0){
            if((*compare)(total[i],total[j])==0){//just swap the pointer it simplify the program
                  student *tmp=(student *)malloc(sizeof(student));
                  memcpy(tmp,stuArray[i],sizeof(student));
                  memcpy(stuArray[i],stuArray[j],sizeof(student));
                  memcpy(stuArray[j],tmp,sizeof(student));
//                  float ttltmp=total[i];
//                  total[i]=total[j];
//                  total[j]=ttltmp;
//                long idtmp=stuArray[i]->id;stuArray[i]->id=stuArray[i]->id;stuArray[i]->id=idtmp;//swap them
//                float *scoretmp=(float *)malloc(sizeof(int)*3);
//                memcpy(scoretmp,stuArray[i]->score,sizeof(int)*3);
//                memcpy(stuArray[i]->score,stuArray[i]->score,sizeof(int)*3);
//                memcpy(stuArray[i]->score,scoretmp,sizeof(int)*3);
//                char namestmp[20];
//                memcpy(namestmp,stuArray[i]->names,20);memcpy(stuArray[i]->names,stuArray[j]->names,20);memcpy(stuArray[j]->names,namestmp,20);
        }//memcpy-> copy the hole the memory
    }

}
void number_sort(){//没必要传参
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<i;j++)
            if(stuArray[i]->id<stuArray[j]->id){
                  student *tmp=(student *)malloc(sizeof(student));
                  memcpy(tmp,stuArray[i],sizeof(student));
                  memcpy(stuArray[i],stuArray[j],sizeof(student));
                  memcpy(stuArray[j],tmp,sizeof(student));
//                long idtmp=stuArray[i]->id;stuArray[i]->id=stuArray[j]->id;stuArray[j]->id=idtmp;
//                float *scoretmp=(float *)malloc(sizeof(int)*3);
//                memcpy(scoretmp,stuArray[i]->score,sizeof(int)*3);
//                memcpy(stuArray[i]->score,stuArray[j]->score,sizeof(int)*3);
//                memcpy(stuArray[j]->score,scoretmp,sizeof(int)*3);
//                char namestmp[20];
//                memcpy(namestmp,stuArray[i]->names,20);memcpy(stuArray[i]->names,stuArray[j]->names,20);memcpy(stuArray[j]->names,namestmp,20);
            }
    }
    if(flg){
        printf("Sort in ascending order by number:\n");
        printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
        for(i=0;i<n;i++)
            show(i);
    }
}
void name_sort(Pstudent names_[30]){
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++)
            if(strcmp(names_[i]->names,names_[j]->names)<0){
                  student *tmp=(student *)malloc(sizeof(student));
                  memcpy(tmp,stuArray[i],sizeof(student));
                  memcpy(stuArray[i],stuArray[j],sizeof(student));
                  memcpy(stuArray[j],tmp,sizeof(student));
//                long idtmp=stuArray[i]->id;stuArray[i]->id=stuArray[j]->id;stuArray[j]->id=idtmp;
//                float *scoretmp=(float *)malloc(sizeof(float)*3);
//                memcpy(scoretmp,stuArray[i]->score,sizeof(float)*3);
//                memcpy(stuArray[i]->score,stuArray[j]->score,sizeof(float)*3);
//                memcpy(stuArray[j]->score,scoretmp,sizeof(float)*3);
//                char namestmp[20];
//                strcpy(namestmp,names_[i]->names);strcpy(names_[i]->names,names_[j]->names);strcpy(names_[j]->names,namestmp);
            }
    }
    if(flg){
        printf("Sort in dictionary order by name:\n");
        printf("NO\tName\tMT\tEN\tPH\tSUM\tAVER\n");
        for(i=0;i<n;i++)
            show(i);
    }
}
void number_search(){
    long query;
    printf("Input the number you want to search:");
    scanf(" %ld",&query);
    int i;
    score_sort(descend);//100 98 87
    for(i=0;i<n;i++){
        if(stuArray[i]->id==query)
            break;
    }
    if(i!=n){
        printf("%d\t",i+1);
        show(i);
    }
    else
        printf("Not found!\n");
}
void name_search(){
    char query[20];
    score_sort(descend);
    printf("Input the name you want to search:");
    scanf("%s",query);
    int i;
    for(i=0;i<n;i++){
        if(!strcmp(query,stuArray[i]->names)){
            break;
        }
    }
    if(i!=n){
        printf("%d\t",i+1);
        show(i);
    }
    else
        printf("Not found!\n");
}
void statistic(Pstudent scores_[30]){//a pointer array stands for scores
    float MT[30],EN[30],PH[30];
    int i;
    for(i=0;i<n;i++){
        MT[i]=scores_[i]->score[0];
        EN[i]=scores_[i]->score[1];
        PH[i]=scores_[i]->score[2];
    }
    int sta[6]={0};//means the statistic of every student (<60 or 60-70 ....)
    for(i=0;i<n;i++){
        if(MT[i]<60)
            sta[0]++;
        if(MT[i]==100)
            sta[5]++;
        if(MT[i]>=60&&MT[i]<=69)
            sta[1]++;
        if(MT[i]>=70&&MT[i]<=79)
            sta[2]++;
        if(MT[i]>=80&&MT[i]<=89)
            sta[3]++;
        if(MT[i]>=90&&MT[i]<=100)
            sta[4]++;
    }

    if(flg){
        printf("For course %d:\n",1);
        printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
        printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
        printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
        printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
        printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
    }
    memset(sta,0,6*sizeof(int));//initialize the sta array
    for(i=0;i<n;i++){
        if(EN[i]<60)
            sta[0]++;
        if(EN[i]==100)
            sta[5]++;
        if(EN[i]>=60&&EN[i]<=69)
            sta[1]++;
        if(EN[i]>=70&&EN[i]<=79)
            sta[2]++;
        if(EN[i]>=80&&EN[i]<=89)
            sta[3]++;
        if(EN[i]>=90&&EN[i]<=100)
            sta[4]++;
    }

    if(flg){
        printf("For course %d:\n",2);
        printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
        printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
        printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
        printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
        printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
    }
    memset(sta,0,6*sizeof(int));
    for(i=0;i<n;i++){
        if(PH[i]<60)
            sta[0]++;
        if(PH[i]==100)
            sta[5]++;
        if(PH[i]>=60&&PH[i]<=69)
            sta[1]++;
        if(PH[i]>=70&&PH[i]<=79)
            sta[2]++;
        if(PH[i]>=80&&PH[i]<=89)
            sta[3]++;
        if(PH[i]>=90&&PH[i]<=100)
            sta[4]++;
    }

    if(flg){
        printf("For course %d:\n",3);
        printf("<60\t%d\t%.2f%%\n",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69\t%d\t%.2f%%\n",sta[1],sta[1]/(float)n*100);
        printf("70-79\t%d\t%.2f%%\n",sta[2],sta[2]/(float)n*100);
        printf("80-89\t%d\t%.2f%%\n",sta[3],sta[3]/(float)n*100);
        printf("90-100\t%d\t%.2f%%\n",sta[4],sta[4]/(float)n*100);
        printf("100\t%d\t%.2f%%\n",sta[5],sta[5]/(float)n*100);
    }
}

void show(int i){

    printf("%ld\t%s\t",stuArray[i]->id,stuArray[i]->names);//order is the id after sort
    printf("%.0f\t%.0f\t%.0f\t",stuArray[i]->score[0],stuArray[i]->score[1],stuArray[i]->score[2]);
    float sum=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
    printf("%.0f\t%.0f\n",sum,sum/3);
}

 

转载于:https://www.cnblogs.com/xzenith/p/3681090.html

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值