编程实现一个选项功能:可按任意成绩或平均成绩排序,并显示出来


#include <stdio.h>
 
#define NC 100
struct studentRed {  //定义学生记录,即对应表格的一行
    char studName[20]; //姓名对应第一列的Student Name
    char studID[20]; //学号对应第二列的Student ID 用20个字符编码,可用字母做学号
    float compProgram; // score for Computer programming 用浮点数可输入小数分数
    float physEducat;  //score for Computer programming
    float commResech;  //score for Communication and Research
    float averageScore;  //average score
}*p,students[NC]={
        {"John","zy001",84,86,78,0},
        {"Xiaoming","zy002",77,82,90,0},
        {"Xiaohua","zy003",79,82,85,0},
        {"Jianguo","zy004",84,86,78,0},
        {"Xiaodong","zy005",60,55,40,0},
};
 
void insertAvgScore(struct studentRed *stu,int length){
    for (int i = 0; i <length ;++i) {
        stu[i].averageScore=(stu[i].compProgram+stu[i].physEducat+stu[i].commResech)/3;
    }
}
void printStudentInfo(struct studentRed *stu,int length){
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    for(int i=0;i<length;i++){
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",stu[i].studName,stu[i].studID,stu[i].compProgram,stu[i].physEducat,stu[i].commResech,stu[i].averageScore);
    }
 
}
 
int main(){
//    printf("0\n");
    insertAvgScore(students,5);
    //printStudentInfo(students,5);
    
    FILE * fp = fopen("f:\\stu.txt","w+");
    if(fp==NULL){
        printf("打开文件失败或创建文件失败,程序退出!\n");
        return 0; 
    }
    
    int i;
    //存放文件 
    for(i=0;i<5;i++){
        fprintf(fp,"%s %s %f %f %f %f ",students[i].studName,students[i].studID,students[i].compProgram,students[i].physEducat,students[i].commResech,students[i].averageScore); 
    } 
     
    fclose(fp);
    
    fp = fopen("f:\\stu.txt","r");
    if(fp==NULL){
        printf("打开文件失败,程序退出!\n");
        return 0; 
    }
    
    struct studentRed t[NC] ;
//    printf("1\n"); 
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    //从文件读取并打印 
    for(i=0;i<5;i++){
//        fscanf(fp,"%s",t.studName);
//        fscanf(fp,"%s",t.studID);
//        fscanf(fp,"%f",t.compProgram);
//        fscanf(fp,"%f",t.physEducat);
//        fscanf(fp,"%f",t.commResech);
//        fscanf(fp,"%f",t.averageScore);
    //    printf("1.1\n");
        fscanf(fp,"%s%s%f%f%f%f ",t[i].studName,t[i].studID,&t[i].compProgram,&t[i].physEducat,&t[i].commResech,&t[i].averageScore); 
    //    printf("\n21\n");
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",t[i].studName,t[i].studID,t[i].compProgram,t[i].physEducat,t[i].commResech,t[i].averageScore);    
        //printf("\n3\n");
    } 
      
    fclose(fp); 
    return 0;
}

一个实现,如下:

 
 
#include <stdio.h>
#include <string.h> 
#include <stdlib.h> 
#define NC 100
struct studentRed {  //定义学生记录,即对应表格的一行
    char studName[20]; //姓名对应第一列的Student Name
    char studID[20]; //学号对应第二列的Student ID20个字符编码,可用字母做学号
    float compProgram; // score for Computer programming 用浮点数可输入小数分数
    float physEducat;  //score for Computer programming
    float commResech;  //score for Communication and Research
    float averageScore;  //average score
}*p,students[NC]={
        {"John","zy001",84,86,78,0},
        {"Xiaoming","zy002",77,82,90,0},
        {"Xiaohua","zy003",79,82,85,0},
        {"Jianguo","zy004",84,86,78,0},
        {"Xiaodong","zy005",60,55,40,0},
};
 
void insertAvgScore(struct studentRed *stu,int length){
    for (int i = 0; i <length ;++i) {
        stu[i].averageScore=(stu[i].compProgram+stu[i].physEducat+stu[i].commResech)/3;
    }
}
void printStudentInfo(struct studentRed *stu,int length){
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    for(int i=0;i<length;i++){
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",stu[i].studName,stu[i].studID,stu[i].compProgram,stu[i].physEducat,stu[i].commResech,stu[i].averageScore);
    }
 
}

void printScore(struct studentRed * tstu,int length,int choice){
    
    float tcompProgram;
    float tphysEducat;
    float tcommResech;
    float taverageScore;
    char studName[20]; //姓名对应第一列的Student Name
    char studID[20];
    
    studentRed * stu = (studentRed*)malloc(sizeof(studentRed)*length); 
    
    int i,j;
    for(i=0;i<length;i++){  //赋值学生信息结构数组 
        strcpy(stu[i].studName,tstu[i].studName);
        strcpy(stu[i].studID,tstu[i].studID);
        stu[i].compProgram=tstu[i].compProgram;
        stu[i].physEducat=tstu[i].physEducat;
        stu[i].commResech=tstu[i].commResech;
        stu[i].averageScore=tstu[i].averageScore; 
    }
    

    for(i=0;i<length-1;i++){
        
        for(j=i;j<length;j++){ //按学科或平均成绩选项排序 
            if(choice==1&&stu[i].compProgram<stu[j].compProgram){
                tcompProgram=stu[i].compProgram;    
                stu[i].compProgram =stu[j].compProgram;
                stu[j].compProgram=tcompProgram;
                
                strcpy(studName,stu[i].studName);
                strcpy(stu[i].studName,stu[j].studName);
                strcpy(stu[j].studName,studName);
                
                strcpy(studID,stu[i].studID);
                strcpy(stu[i].studID,stu[j].studID);
                strcpy(stu[j].studID,studID);
                
            }else if(choice==2&&stu[i].physEducat<stu[j].physEducat){
                tphysEducat=stu[i].physEducat;
                stu[i].physEducat=stu[j].physEducat;
                stu[j].physEducat=tphysEducat;
                
                
                strcpy(studName,stu[i].studName);
                strcpy(stu[i].studName,stu[j].studName);
                strcpy(stu[j].studName,studName);
                
                strcpy(studID,stu[i].studID);
                strcpy(stu[i].studID,stu[j].studID);
                strcpy(stu[j].studID,studID);
            }else if(choice==3&&stu[i].commResech<stu[j].commResech){
                tcommResech=stu[i].commResech;
                stu[i].commResech=stu[j].commResech;
                stu[j].commResech=tcommResech;
                
                
                strcpy(studName,stu[i].studName);
                strcpy(stu[i].studName,stu[j].studName);
                strcpy(stu[j].studName,studName);
                
                strcpy(studID,stu[i].studID);
                strcpy(stu[i].studID,stu[j].studID);
                strcpy(stu[j].studID,studID);
            }else if(choice==4&&stu[i].averageScore<stu[j].averageScore){
                taverageScore=stu[i].averageScore;
                stu[i].averageScore=stu[j].averageScore;
                stu[j].averageScore=taverageScore;
                
                
                strcpy(studName,stu[i].studName);
                strcpy(stu[i].studName,stu[j].studName);
                strcpy(stu[j].studName,studName);
                
                strcpy(studID,stu[i].studID);
                strcpy(stu[i].studID,stu[j].studID);
                strcpy(stu[j].studID,studID);
            }
        }
    } 
    

    for(i=0;i<length;i++){  //打印结果 
    //    printf("1,choice=%d\n",choice);
   
        if(choice==1){
        //    printf("1-1\n");
            printf("%-12s%s\t%f\n",stu[i].studName,stu[i].studID,stu[i].compProgram);
        }else if(choice==2){
            printf("%-12s%s\t%f\n",stu[i].studName,stu[i].studID,stu[i].physEducat);
        }else if(choice==3){
            printf("%-12s%s\t%f\n",stu[i].studName,stu[i].studID,stu[i].commResech);
        }else if(choice==4){
            printf("%-12s%s\t%f\n",stu[i].studName,stu[i].studID,stu[i].averageScore);
        }
    
    } 
    
    
}
 
int main(){
//    printf("0\n");
    insertAvgScore(students,5);
    //printStudentInfo(students,5);
    
    FILE * fp = fopen("f:\\stu.txt","w+");
    if(fp==NULL){
        printf("打开文件失败或创建文件失败,程序退出!\n");
        return 0; 
    }
    
    int i;
    //存放文件 
    for(i=0;i<5;i++){
        fprintf(fp,"%s %s %f %f %f %f ",students[i].studName,students[i].studID,students[i].compProgram,students[i].physEducat,students[i].commResech,students[i].averageScore); 
    } 
     
    fclose(fp);
    
    fp = fopen("f:\\stu.txt","r");
    if(fp==NULL){
        printf("打开文件失败,程序退出!\n");
        return 0; 
    }
    
    struct studentRed t[NC] ;
//    printf("1\n"); 
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    //从文件读取并打印 
    for(i=0;i<5;i++){
//        fscanf(fp,"%s",t.studName);
//        fscanf(fp,"%s",t.studID);
//        fscanf(fp,"%f",t.compProgram);
//        fscanf(fp,"%f",t.physEducat);
//        fscanf(fp,"%f",t.commResech);
//        fscanf(fp,"%f",t.averageScore);
    //    printf("1.1\n");
        fscanf(fp,"%s%s%f%f%f%f ",t[i].studName,t[i].studID,&t[i].compProgram,&t[i].physEducat,&t[i].commResech,&t[i].averageScore); 
    //    printf("\n21\n");
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",t[i].studName,t[i].studID,t[i].compProgram,t[i].physEducat,t[i].commResech,t[i].averageScore);    
        //printf("\n3\n");
    } 
    
    int choice ; 
    printf("请输入数字选择操作:\n");
    printf("1>按compProgram成绩排序\n");
    printf("2>按physEducat成绩排序\n");
    printf("3>按commResech成绩排序\n");
    printf("4>按平均成绩排序\n");
    printf("5>退出程序\n");
    scanf("%d",&choice);
    //printf("choice=%d\n",choice);
    while(choice!=5){
        printScore(t,5,choice);
        printf("请输入数字选择操作:\n");
        printf("1>按compProgram成绩排序\n");
        printf("2>按physEducat成绩排序\n");
        printf("3>按commResech成绩排序\n");
        printf("4>按平均成绩排序\n");
        printf("5>退出程序\n");
        scanf("%d",&choice);
    //    printf("choice=%d\n",choice);
    } 
     
     
      
    fclose(fp); 
    return 0;
}