m0_68362664 2022-04-24 22:18 采纳率: 68.2%
浏览 40
已结题

编程实现功能:可将整个students结构体数组存到硬盘上,并从存好的硬盘上读到内存的students结构体数组中


#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(){
    insertAvgScore(students,5);
    printStudentInfo(students,5);
    return 0;
}

  • 写回答

1条回答 默认 最新

  • a5156520 2022-04-25 11:01
    关注

    修改了一下,如下:

    #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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月4日
  • 已采纳回答 4月26日
  • 修改了问题 4月24日
  • 创建了问题 4月24日

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突