定义结构体student,包括三个成员:学生姓名、出生日期(该成员也定义为结构体类型)和分数。

2024-12-16 08:16:57
推荐回答(1个)
回答1:

#include
#include

using namespace std;

struct date
{
int year;
int month;
int day;
};

struct student
{
char name[50];
date birthday;
int score;

bool operator < (const student& s2) const
{
return score > s2.score;
}
};

int main()
{
student s[5];
int i;
for(i=0; i<5; i++)
{
printf("Please enter the name, birthday and score:\n");
scanf("%s %d %d %d %d", s[i].name, &s[i].birthday.year, &s[i].birthday.month, &s[i].birthday.day, &s[i].score);
}
sort(s, s+5);//排序函数,algorithm库里面的。

for(i=0; i<5; i++)
{
printf("name:%s, birthday:%d-%d-%d, score:%d\n", s[i].name, s[i].birthday.year, s[i].birthday.month, s[i].birthday.day, s[i].score);
}
return 0;
}