VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1321|回复: 2

C语言数组练习题

[复制链接]
71_avatar_middle
最佳答案
0 
在线会员 发表于 2021-5-4 18:37:34 | 显示全部楼层 |阅读模式
期末老师需要对某班学生成绩进行处理,生成班级成绩单。假设班级人数为N(为了方便调试程序,N取5),学生的学号、姓名均不超过10个字符,输入每个学生的学号、姓名、4科成绩(例如:英语、数学、体育、计算机),计算每个学生的总分、平均分,然后按照总分由高到低排名,输出原始的成绩单和排名后的成绩单。请编程实现
     求助SOS!!!




上一篇:获取文件或文件夹图标
下一篇:在Windows上使用_vscwprintf处理UTF-8编码的字符串时失败,该如何解决?
01_avatar_middle
最佳答案
1 
donate_vip 发表于 2021-5-6 14:57:00 | 显示全部楼层
#include<iostream>
#include<string>
using namespace std;
const int int_NumOfStudent = 3;

//定义学生信息结构体
struct struct_StudentMsg
{
        //学生学号、姓名
        int int_Id;
        string string_Name;

        //学生的四个学科成绩
        float float_English;
        float float_Math;
        float float_Computer;
        float float_Sport;

        //总分、平均分
        float float_Average;
        float float_Total;

};

void fun_GetMsg(struct_StudentMsg* pStu);
void fun_PrintMsg(struct_StudentMsg* pStu);
void fun_Calc_TotalAndAverage(struct_StudentMsg* pStu);
void fun_SortTotal(struct_StudentMsg struct_Stu[]);

/*
        作用:录入学生信息
        参数:struct_StudentMsg* 结构体指针
        返回值:无
*/
void fun_GetMsg(struct_StudentMsg* pStu)
{
        for (int i = 0; i < int_NumOfStudent; i++)
        {
                cout << "录入第" << i + 1 << "位学生信息,分别是学生学号、姓名、英语成绩、数学成绩、计算机成绩、体育成绩" << endl;
                cin >> pStu[i].int_Id;
                cin >> pStu[i].string_Name;
                cin >> pStu[i].float_English;
                cin >> pStu[i].float_Math;
                cin >> pStu[i].float_Computer;
                cin >> pStu[i].float_Sport;
        }

        cout << "学生信息已录入完毕" << endl;
}

/*
        作用:输出学生信息
        参数:struct_StudentMsg* 结构体指针
        返回值:无
*/
void fun_PrintMsg(struct_StudentMsg* pStu)
{
        cout << "当前学习信息如下所示:" << endl;

        for (int i = 0; i < int_NumOfStudent; i++)
        {
               
                cout <<"学号:"<< pStu->int_Id;
                cout <<" 姓名:"<< pStu->string_Name;
                cout <<" 英语成绩:"<< pStu->float_English;
                cout <<" 数学成绩:"<< pStu->float_Math;
                cout <<" 计算机成绩:"<< pStu->float_Computer;
                cout <<" 体育成绩:"<< pStu->float_Sport ;
                cout << " 总成绩:" << pStu->float_Total;
                cout << " 平均成绩:" << pStu->float_Average << endl;
                pStu++;
        }
}

/*
        作用:计算每个学生信息总成绩和平均分
        参数:struct_StudentMsg* 结构体指针
        返回值:无
*/
void fun_Calc_TotalAndAverage(struct_StudentMsg* pStu)
{
        for (int i = 0; i < int_NumOfStudent; i++)
        {

                pStu->float_Total = pStu->float_English + pStu->float_Math + pStu->float_Computer + pStu->float_Sport;
                pStu->float_Average = pStu->float_Total/ int_NumOfStudent;
                pStu++;
        }
}

/*
        作用:利用学生总分进行排序 冒泡排序
        参数:结构体数组(值传递)
        返回值:无
*/
void fun_SortTotal(struct_StudentMsg struct_Stu_2[])
{
        struct_StudentMsg struct_tmp;
        int i = 0, j = 0;

        for ( i = 0; i < int_NumOfStudent; i++)
        {
                for ( j = 0; j < int_NumOfStudent-i-1; j++)
                {
                        if (struct_Stu_2[j].float_Total < struct_Stu_2[j+1].float_Total)
                        {
                                struct_tmp = struct_Stu_2[j+1];
                                struct_Stu_2[j + 1] = struct_Stu_2[j];
                                struct_Stu_2[j] = struct_tmp;
                        }
                }
        }
        cout << "按照总成绩排序后排名为如下所示:" << endl;
        for (int i = 0; i < int_NumOfStudent; i++)
        {

                cout << "学号:" << struct_Stu_2[i].int_Id;
                cout << " 姓名:" << struct_Stu_2[i].string_Name;
                cout << " 英语成绩:" << struct_Stu_2[i].float_English;
                cout << " 数学成绩:" << struct_Stu_2[i].float_Math;
                cout << " 计算机成绩:" << struct_Stu_2[i].float_Computer;
                cout << " 体育成绩:" << struct_Stu_2[i].float_Sport;
                cout << " 总成绩:" << struct_Stu_2[i].float_Total;
                cout << " 平均成绩:" << struct_Stu_2[i].float_Average << endl;
        }
}

int main()
{
        //信息录入
        struct_StudentMsg struct_Stu[int_NumOfStudent] = { 0 };
        cout << "录入 "<< int_NumOfStudent <<"位学生成绩" << endl;

        fun_GetMsg(&struct_Stu[0]);
        fun_PrintMsg(&struct_Stu[0]);

        //初始化结构体,方便调试、
        //struct_StudentMsg struct_Stu[int_NumOfStudent] = { {1, "Zhangsan", 60, 65, 70, 90},{2, "Lisi", 80, 90,65,100}, {3, "Wangwu", 87, 770,66,33} };

        fun_PrintMsg(&struct_Stu[0]);

        fun_Calc_TotalAndAverage(&struct_Stu[0]);

        fun_SortTotal(struct_Stu);

        fun_PrintMsg(&struct_Stu[0]);
}
25_avatar_middle
最佳答案
0 
在线会员 发表于 2021-5-10 12:59:49 | 显示全部楼层
用STL就很棒
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-1 21:30

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表