c++从键盘输入10个学生的信息(包括学号,姓名,成绩)要求按照成绩高低顺序输出各学升的信息

2025-04-01 03:32:15
推荐回答(1个)
回答1:

//定义结构体存储学生信息,对成绩使用冒泡排序
#include 
using namespace std;
struct Stu{
    string id;
    string name;
    int score;
};
int main(){
    Stu stut[10];
    Stu tmp;
    for(int i = 0; i < 10; i++){
        cin >> stut[i].id >> stut[i].name >> stut[i].score;}
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 9 - i; j++){
            if(stut[j].score < stut[j + 1],score){
                tmp = stut[j];
                stut[j] = stut[j + 1];
                stut[j + 1] = tmp;}
    for(int i = 0; i < 10; i++){
        cout << stut[i].id << stut[i].name << stut[i].score << endl;}
    return 0;}