|
发表于 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]);
} |
|