少年kiku 2022-12-12 10:19 采纳率: 100%
浏览 62
已结题

关于#c++#的问题

求解答
有3名学生的成绩需要处理,学生包含的信息有学号、姓名、数学、物理、化学三门课成绩及平均成绩和总分。
要求:
(1)定义反映学生信息的结构体数据类型。
(2)定义一个average函数处理平均成绩,
(3)定义一个sort函数根据学生总分进行降序排序。
(4)把学生信息:学号、姓名、三门课成绩、平均分数、总分写到score.txt 文件中。
现在已经完成了前两步,求解答后面的过程,最好是弄在一个程序里

#include<iostream.h>
struct Student
{  
    int id; 
    char name[20];   
    float score; 
}student1,student2,student3;
int main()
{
    double a[10];
    int i;
    double s=0;
    double average=0;
    cout<<"输入三科成绩"<<endl;
    for(i=0;i<3;i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    average=s/3;
    cout<<"平均成绩为"<<average<<endl;
    return 0;
}



  • 写回答

1条回答 默认 最新

  • 婉瑄|Vivian_ 2022-12-15 09:52
    关注

    你之前写的代码有问题,没有按照题目要求来。
    所以我重新写了一份,你看看是你需要的吧。(如果有不懂的尽管问 :))
    如果对你有帮助,点个采纳可否 QAQ~

    这是代码:

    #include <iostream>
    #include <fstream>
    
    struct Student
    {
        int id;
        char name[20];
        double math, phy, chem;
        double ave, total;
    };
    
    void average(Student* const stu, uint32_t size)
    {
        for(int i = 0; i < size; i++)
        {
            stu[i].total = stu[i].math + stu[i].phy + stu[i].chem;
            stu[i].ave = stu[i].total / 3;
        }
    }
    
    void sort(Student* const stu, uint32_t size)
    {
        // lambda function for swapping
        auto swap = [](Student* lhs, Student* rhs) -> void {
            Student temp = *rhs;
            *rhs = *lhs;
            *lhs = temp;
        };
        // simple bubble sort
        for(int i = size - 1; i > 0; i--)
        {
            for(int j = 0; j < i; j++)
            {
                if(stu[j].total < stu[j + 1].total)
                    swap(&stu[j], &stu[j + 1]);
            }
        }
    }
    
    void write2file(const std::string& path, const Student* const stu, uint32_t size)
    {
        std::ofstream fout;
        fout.open(path, std::ios::out);
        // if path file does not exist, one will be created
        for(int i = 0; i < size; i++)
        {
            fout << stu[i].id << ' ' << stu[i].name << ' '
                 << stu[i].math << ' ' << stu[i].phy << ' ' << stu[i].chem << ' '
                 << stu[i].ave << ' ' << stu[i].total << '\n';
        }
    }
    
    #define TEST
    #ifdef TEST
    int main()
    {
        // array contain three students
        Student arr[3] = {
            {1, "tom", 90, 78, 67}, 
            {2, "lily", 100, 87, 98}, 
            {3, "abby", 97, 60, 79}
        };
        average(arr, 3);
        sort(arr, 3);
        write2file("test.txt", arr, 3);
    }
    #endif
    

    这是输出文档:

    2 lily 100 87 98 95 285
    3 abby 97 60 79 78.6667 236
    1 tom 90 78 67 78.3333 235
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月24日
  • 已采纳回答 12月16日
  • 修改了问题 12月13日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见