修改后的代码如下:

#include #include

using namespace std;

struct student { int stnumber; string name; float scores; };

void Sort(int n, student* st) { int i, j; student temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (st[j].scores > st[j + 1].scores) { temp = st[j]; st[j] = st[j + 1]; st[j + 1] = temp; } } } }

void Set(int n, student* st) { int i; cout << "依次输入学生信息:(学号,姓名,成绩)" << endl; for (i = 0; i < n; i++) { cout << "第" << i+1 << "个学生:"; cin >> st[i].stnumber >> st[i].name >> st[i].scores; } }

void Show(int n, student* st) { int i; cout << "排序结果如下:" << endl; for (i = 0; i < n; i++) { cout << "第" << i+1 << "名:"; cout << "学号:" << st[i].stnumber << " 姓名:" << st[i].name << " 成绩:" << st[i].scores << endl; } }

int main() { int n; cout << "输入学生个数:"; cin >> n; student st[100]; Set(n, &st[0]); // 传入数组首地址 Sort(n, &st[0]); // 传入数组首地址 Show(n, &st[0]); // 传入数组首地址 return 0; }

在调用函数时,使用数组的首地址作为参数,即 &st[0],这样就可以使用引用来修改数组元素的值,避免了数组复制带来的开销。

你好您好!有什么可以帮到您呢?#include iostream #include string using namespace std; struct student int stnumber; string name; float scores; ; void Sortint n student st int i j; student temp; for i = 0; i n - 1;

原文地址: https://www.cveoy.top/t/topic/b4m5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录