永发信息网

向计算机输入某班n(n《=100)个同学学号,姓名,性别的m(m《=10)门的课程考试成绩

答案:1  悬赏:30  手机版
解决时间 2021-03-19 06:00
向计算机输入某班n(n《=100)个同学学号,姓名,性别的m(m《=10)门的课程考试成绩
最佳答案
//读写文件功能可使用freopen函数,还是大一写的程序XD,恍恍惚惚过了两年了
#include 
#include 
#include 
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    else  return 0;
}
int n;//the number of students
     
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(){
    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:
");
                    printf("NO Name MT EN PH SUM AVER
");
                    for(i=0;i                        show(i);
                }
                break;
            case 5:
                score_sort(descend);
                if(flg){
                    printf("Sort in ascending order by total score of every student:
");
                    printf("NO Name MT EN PH SUM AVER
");
                    for(i=0;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!
");
                break;
            default:
                printf("Input error!
");
                break;
        }
     
    }
return 0;
}
     
void print(){
    printf("1.Append record
");
    printf("2.Calculate total and average score of every course
");
    printf("3.Calculate total and average score of every student
");
    printf("4.Sort in descending order by total score of every student
");
    printf("5.Sort in ascending order by total score of every student
");
    printf("6.Sort in ascending order by number
");
    printf("7.Sort in dictionary order by name
");
    printf("8.Search by number
");
    printf("9.Search by name
");
    printf("10.Statistic analysis
");
    printf("Please Input your choice:");
}
void append(){
    int i;
    printf("Input student's ID,name and score:
");
    for(i=0;i        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        sum0+=stuArray[i]->score[0];
        sum1+=stuArray[i]->score[1];
        sum2+=stuArray[i]->score[2];
    }
    if(flg){
        printf("course %d:sum=%.0f,aver=%.0f
",1,sum0,sum0/n);
        printf("course %d:sum=%.0f,aver=%.0f
",2,sum1,sum1/n);
        printf("course %d:sum=%.0f,aver=%.0f
",3,sum2,sum2/n);
    }
}
void student_total(){
    float total[30]={0.0};
    int i;
    for(i=0;i        total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
    }
    if(flg){
        for(i=0;i            printf("student %d:sum=%.0f,aver=%.0f
",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        total[i]=stuArray[i]->score[0]+stuArray[i]->score[1]+stuArray[i]->score[2];
    }
    for(i=0;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));
        }//memcpy-> copy the hole the memory
    }
     
}
void number_sort(){//没必要传参
    int i,j;
    for(i=0;i        for(j=0;j            if(stuArray[i]->idid){
                  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));
            }
    }
    if(flg){
        printf("Sort in ascending order by number:
");
        printf("NO Name MT EN PH SUM AVER
");
        for(i=0;i            show(i);
    }
}
void name_sort(Pstudent names_[30]){
    int i,j;
    for(i=0;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));
            }
    }
    if(flg){
        printf("Sort in dictionary order by name:
");
        printf("NO Name MT EN PH SUM AVER
");
        for(i=0;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        if(stuArray[i]->id==query)
            break;
    }
    if(i!=n){
        printf("%d ",i+1);
        show(i);
    }
    else
        printf("Not found!
");
}
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        if(!strcmp(query,stuArray[i]->names)){
            break;
        }
    }
    if(i!=n){
        printf("%d ",i+1);
        show(i);
    }
    else
        printf("Not found!
");
}
void statistic(Pstudent scores_[30]){//a pointer array stands for scores
    float MT[30],EN[30],PH[30];
    int i;
    for(i=0;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        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:
",1);
        printf("<60 %d %.2f%%
",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69 %d %.2f%%
",sta[1],sta[1]/(float)n*100);
        printf("70-79 %d %.2f%%
",sta[2],sta[2]/(float)n*100);
        printf("80-89 %d %.2f%%
",sta[3],sta[3]/(float)n*100);
        printf("90-100 %d %.2f%%
",sta[4],sta[4]/(float)n*100);
        printf("100 %d %.2f%%
",sta[5],sta[5]/(float)n*100);
    }
    memset(sta,0,6*sizeof(int));//initialize the sta array
    for(i=0;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:
",2);
        printf("<60 %d %.2f%%
",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69 %d %.2f%%
",sta[1],sta[1]/(float)n*100);
        printf("70-79 %d %.2f%%
",sta[2],sta[2]/(float)n*100);
        printf("80-89 %d %.2f%%
",sta[3],sta[3]/(float)n*100);
        printf("90-100 %d %.2f%%
",sta[4],sta[4]/(float)n*100);
        printf("100 %d %.2f%%
",sta[5],sta[5]/(float)n*100);
    }
    memset(sta,0,6*sizeof(int));
    for(i=0;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:
",3);
        printf("<60 %d %.2f%%
",sta[0],sta[0]/(float)n*100);//change n to float
        printf("60-69 %d %.2f%%
",sta[1],sta[1]/(float)n*100);
        printf("70-79 %d %.2f%%
",sta[2],sta[2]/(float)n*100);
        printf("80-89 %d %.2f%%
",sta[3],sta[3]/(float)n*100);
        printf("90-100 %d %.2f%%
",sta[4],sta[4]/(float)n*100);
        printf("100 %d %.2f%%
",sta[5],sta[5]/(float)n*100);
    }
}
     
void show(int i){
     
    printf("%ld %s ",stuArray[i]->id,stuArray[i]->names);//order is the id after sort
    printf("%.0f %.0f %.0f ",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 %.0f
",sum,sum/3);
}来自:求助得到的回答
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
暴风城怎么去藏宝海湾
儿子初二,让给他买《十宗罪》,能看吗?
麻鸭面的做法大全
二婚我没儿没女可以把我一半房产给我侄子吗
11/6×9.6,和17.6谁大?
松树的特点和外型特征有哪些
平鑫涛与林婉珍和琼瑶,哪个组合才是婚姻最好
100级女裙子 防御加248 另外单加26伤害 体质
IP地址为180.100.100.100属于
农村房屋拆迁,拆迁方必须公开哪些文件才算合
男友说分手,我找到他还能挽回吗
花千骨的老公是谁?
解方程x÷1.6=5.5
刚移栽的果苗,能浇水吗?
交警查车掉头逆行跑了被记下车牌会怎样处理
推荐资讯
上学一共要学多少年????
22题 理由,
城市勘测是国家核心期刊吗
今天买了一盆风信子,刚移栽,要怎么养?
汽车12段音质怎么调
高要农机长达农机经营部在什么地方啊,我要过
张家港吾悦康美健身有没有健身卡转让,或者认
我今年16岁。想出来打工不知道有什么工作适合
胡杨这个地址在什么地方,我要处理点事
别人拨打我的电话里有音乐因该怎么设?
欧式家具小缝隙里有灰尘怎样才能弄干净?
怎么样才可以联系徐工集团董事长
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?