c++,结构体成员中包含数组

2024-12-02 04:30:06
推荐回答(1个)
回答1:

#include
#include
#include
#include
using namespace std;
struct stu_info
{
int stu_id;
string stu_name;
bool stu_sex ; /*true 表示男 false表示女*/
int stu_score[3];/*按顺序分别为计算机,英语,数学成绩*/
};
bool comp_score_reverse(stu_info a, stu_info b)
{
if(a.stu_score[0] == b.stu_score[0] ) /*计算机成绩相同*/
{
if(a.stu_score[1] == b.stu_score[1] ) /*英语成绩相同*/
{
return a.stu_score[2] >= b.stu_score[2];
}
return a.stu_score[1] > b.stu_score[1];
}
return a.stu_score[0] > b.stu_score[0];
}
void print_stu(struct stu_info info)
{
cout <<"=================================="< cout <<"学号="< cout <<",姓名="<< info.stu_name;
cout <<",性别="<< (info.stu_sex ? "男":"女") << endl;
cout <<"计算机成绩="<}
int main(void)
{
int i;
stu_info s_info;
vector v_stu_infos;
int i_sex;

for( i=0;i<5;i++)
{
cout << "请输入第"< cin >> s_info.stu_id;
cout << "请输入第"< cin >> s_info.stu_name;
cout <<"请选择性别,1表示男,0表示女:"<< endl;
cin >>i_sex;
if( i_sex==1 )
s_info.stu_sex=true;
else
s_info.stu_sex=false;

cout << "请输入第"< cin >> s_info.stu_score[0];
cout << "请输入第"< cin >> s_info.stu_score[1];
cout << "请输入第"< cin >> s_info.stu_score[2];

v_stu_infos.push_back(s_info);
}
cout <<"============================================================"< cout <<"将学生信息按照计算机成绩从高到低递减排排序(计算机相同时,比较英语成绩,英语成绩也相同则比较数学成绩),进行输出"<< endl;

sort( v_stu_infos.begin(),v_stu_infos.end(),comp_score_reverse ); /*按要求进行排序*/

for_each(v_stu_infos.begin(),v_stu_infos.end(),print_stu );
}