5.6作业 吴哲

struct Student{
    char name[16];
    int age;
    double math_score;
    double chinese_score;
    double english_score;
    double physics_score;
    double chemistry_score;
    double bio_score;
};
int main(int argc, const char *argv[])
{
        struct Student s[5]={
        {"迪迦",1686,88,85,87,78,98,54},
        {"熊二",15,8,5,7,8,9,4},
        {"熊大",16,89,86,98,91,92,94},
        {"野原新之助",6,99,97,93,95,98,92},
        {"光头强",31,7,5,8,1,4,3}
         };
    FILE* fp = fopen("./text.txt","w");
    if(fp == NULL)
    {
        printf("文件创建失败\n");
        return 1;
    }
    for(int i=0;i<5;i++)
    {
    fprintf(fp,"%s\t",s[i].name);
    fprintf(fp,"%d\t",s[i].age);
    fprintf(fp,"%g\t",s[i].math_score);
    fprintf(fp,"%g\t",s[i].chinese_score);
    fprintf(fp,"%g\t",s[i].english_score);
    fprintf(fp,"%g\t",s[i].physics_score);              
    fprintf(fp,"%g\t",s[i].chemistry_score);
    fprintf(fp,"%g\t",s[i].bio_score);
    fprintf(fp,"\n");
    }

    fclose(fp);
    return 0;
}
struct Student{
    char name[16];
    int age;
    double math_score;
    double chinese_score;
    double english_score;
    double physics_score;
    double chemistry_score;
    double bio_score;
};
int main(int argc, const char *argv[])
{
    struct Student s[5];
    FILE* fp = fopen("./text.txt","r");
    if(fp == NULL)
    {
        printf("文件打开失败");
        return 1;
    }
    int i=0;
    while(1)
    {
    int renval = fscanf(fp,"%s %d %lf %lf %lf %lf %lf %lf",
               &s[i].name,
               &s[i].age,
               &s[i].math_score,
               &s[i].chinese_score,
               &s[i].english_score,
               &s[i].physics_score,
               &s[i].chemistry_score,
               &s[i].bio_score);
    if(renval == EOF)
    {
        break;                                                      
    }
    printf("%s\t %d\t %g\t %g\t %g\t %g\t %g\t %g\t",
             s[i].name,
             s[i].age,
             s[i].math_score,
             s[i].chinese_score,
             s[i].english_score,
             s[i].physics_score,
             s[i].chemistry_score,
             s[i].bio_score);
    i++;
    }
    fclose(fp);
    return 0;


 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我知道了。以下是Python代码实现: ```python # 定义学生类 class Student: def __init__(self, id, name, score1, score2, score3): self.id = id self.name = name self.score1 = score1 self.score2 = score2 self.score3 = score3 self.average = 0 # 计算平均分 def calc_average(self): self.average = (self.score1 + self.score2 + self.score3) / 3 # 定义主函数 def main(): # 初始化学生列表 student_list = [ Student(1001, "赵娟", 78, 82, 84), Student(1002, "王平", 80, 86, 77), Student(1003, "周颖", 88, 83, 76), Student(1004, "吴哲新", 76, 92, 87), Student(1005, "杨静", 81, 86, 85), ] # 计算每个学生的平均分 for student in student_list: student.calc_average() # 按平均分降序排列 student_list.sort(key=lambda x: x.average, reverse=True) # 输出原始记录和按平均成绩降序排后的记录信息 print("原始记录:") for student in student_list: print(f"{student.id}\t{student.name}\t{student.score1}\t{student.score2}\t{student.score3}\t{student.average:.2f}") print("\n按平均成绩降序排列后的记录信息:") for student in student_list: print(f"{student.id}\t{student.name}\t{student.score1}\t{student.score2}\t{student.score3}\t{student.average:.2f}") if __name__ == '__main__': main() ``` 输出结果如下: ``` 原始记录: 1003 周颖 88 83 76 82.33 1005 杨静 81 86 85 84.00 1002 王平 80 86 77 81.00 1001 赵娟 78 82 84 81.33 1004 吴哲新 76 92 87 85.00 按平均成绩降序排列后的记录信息: 1004 吴哲新 76 92 87 85.00 1005 杨静 81 86 85 84.00 1002 王平 80 86 77 81.00 1001 赵娟 78 82 84 81.33 1003 周颖 88 83 76 82.33 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值