第 17 课 / 共 22 课
结构体综合应用
⏱ 70 分钟 | 前置:第 16 课(struct)、第 15 课(引用)| 工具:VSCode + g++
🏁 第三阶段收官课:指针、内存、结构体三件套合体,做出第一个"像样的数据管理程序"——学生成绩表。下一站:面向对象!
🎯 本课目标
- 会用结构体数组管理一批学生:一个下标 = 一个完整档案袋
- 掌握结构体传参三种姿势:值传递(复制)、
Student&(改原件)、const Student&(只读不复制)
- 认识枚举 enum:给一组整数常量起名字,告别魔法数字
- 完成学生成绩表项目:录入 / 成绩表 / 平均分 / 最高分 / 冒泡排序,并看懂排序原理
⏱ 时间分配
| 时间 | 环节 |
| 0–5 分钟 | 复习第 16 课 + 作业讲评(重点讲 -> 改错与整体赋值) |
| 5–15 分钟 | 知识点1:结构体数组 |
| 15–27 分钟 | 知识点2:结构体作函数参数 |
| 27–35 分钟 | 知识点3:枚举 enum |
| 35–50 分钟 | 知识点4:成绩表项目 + 冒泡排序(本课重头戏) |
| 50–65 分钟 | 课堂练习 |
| 65–70 分钟 | 总结 + 布置作业 |
知识点 1:结构体数组——一排档案袋(10 分钟)
clsarr.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
double score;
};
int main() {
Student cls[3] = {{"张三", 92}, {"李四", 78}, {"王五", 85}};
for (int i = 0; i < 3; i++) {
cout << i + 1 << ". " << cls[i].name
<< " " << cls[i].score << endl;
}
// 1. 张三 92
// 2. 李四 78
// 3. 王五 85
cls[1].score += 5; // 李四加分:一个下标锁定一个人,再 . 到成员
cout << cls[1].name << " 现在 " << cls[1].score << endl; // 李四 现在 83
return 0;
}
🧭 对比第 12 课通讯录:平行数组要 names[i]、phones[i] 两处对齐;结构体数组 cls[i] 一步拿到整个档案袋,cls[i].score 再取成员。删除、排序时整个档案袋一起搬(第 16 课整体赋值),再也不会张冠李戴。
录入也一样自然:cin >> cls[i].name >> cls[i].score; ——成员就是普通变量,第 2 课的 cin 直接往里灌。
知识点 2:结构体作函数参数(12 分钟)
stuparam.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
double score;
};
void printStudent(const Student& s) { // 只读引用:不复制、不许改
cout << s.name << " " << s.score << endl;
}
void addBonus(Student& s) { // 引用:改的就是原件
s.score += 5;
}
int main() {
Student st{"张三", 92};
printStudent(st); // 张三 92
addBonus(st);
printStudent(st); // 张三 97
return 0;
}
| 姿势 | 签名 | 效果 | 适用 |
| 值传递 | f(Student s) | 整袋复印,改不动原件 | 小结构体且确实要副本时 |
| 引用 | f(Student& s) | 就是原件,随便改 | 要修改(加分、改名) |
| const 引用 | f(const Student& s) | 不复制 + 只读保护 | 只看不改(打印、统计)首选 |
| 指针 | f(Student* p) | 顺门牌改原件,可能为空 | 需要"可能没有"的语义时 |
⚠️ 结构体可能很大(几十个成员),值传递每次调用都整袋复印,又慢又费内存。经验法则:只看不改用 const 引用,要改用引用——第 15 课 const 引用的伏笔正式兑现。数组传参仍是首地址:f(Student cls[], int n) 改元素改的是原件。
知识点 3:枚举 enum——给常量发工牌(8 分钟)
grade.cpp
#include <iostream>
using namespace std;
enum Grade { FAIL, PASS, GOOD, EXCELLENT }; // 默认依次 = 0, 1, 2, 3
Grade toGrade(double s) {
if (s >= 90) return EXCELLENT;
if (s >= 80) return GOOD;
if (s >= 60) return PASS;
return FAIL;
}
int main() {
cout << toGrade(92) << endl; // 3(EXCELLENT)
cout << toGrade(78) << endl; // 1(PASS)
Grade g = GOOD; // 用名字,不用记数字
if (g == GOOD) cout << "良好" << endl;
return 0;
}
- 枚举 = 一组有名字的整数常量。代码里写
EXCELLENT 而不是 3,读代码的人一眼懂——告别"魔法数字"
- 枚举是独立类型:
Grade g = 3; 编译报错(int 不能偷偷变 Grade),必须写 Grade g = EXCELLENT;——编译器帮你把关
- 配合 switch 把枚举翻译成中文(项目里的 gradeName 函数),第 4 课 switch 的老舞台
知识点 4:综合项目——学生成绩表(15 分钟)
项目结构(板书):main 管菜单与调度;inputStudents 录入(return 带回新 n,第 12 课老规矩);printTable 打印(const 数组只读);average 求平均;findTop 找最高分(返回下标);sortByScore 冒泡排序(整体交换档案袋);toGrade + gradeName 枚举翻译。
gradebook.cpp(完整项目,约 120 行)
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;
struct Student {
string name;
double score;
};
enum Grade { FAIL, PASS, GOOD, EXCELLENT };
Grade toGrade(double s) {
if (s >= 90) return EXCELLENT;
if (s >= 80) return GOOD;
if (s >= 60) return PASS;
return FAIL;
}
string gradeName(Grade g) {
switch (g) {
case EXCELLENT: return "优秀";
case GOOD: return "良好";
case PASS: return "及格";
default: return "不及格";
}
}
void printMenu();
int inputStudents(Student cls[], int n);
void printTable(const Student cls[], int n);
double average(const Student cls[], int n);
int findTop(const Student cls[], int n);
void sortByScore(Student cls[], int n);
int main() {
Student cls[MAX];
int n = 0;
int choice;
do {
printMenu();
cout << "请选择:";
cin >> choice;
if (choice == 1) {
n = inputStudents(cls, n);
} else if (choice == 2) {
printTable(cls, n);
} else if (choice == 3) {
if (n == 0) cout << "(还没有成绩)" << endl;
else cout << "平均分:" << average(cls, n) << endl;
} else if (choice == 4) {
if (n == 0) cout << "(还没有成绩)" << endl;
else {
int t = findTop(cls, n);
cout << "最高分:" << cls[t].name << " " << cls[t].score << endl;
}
} else if (choice == 5) {
sortByScore(cls, n);
cout << "已按分数从高到低排序" << endl;
}
} while (choice != 0);
cout << "再见!" << endl;
return 0;
}
void printMenu() {
cout << "===== 学生成绩表 =====" << endl;
cout << "1 录入 2 成绩表 3 平均分 4 最高分 5 排序 0 退出" << endl;
}
int inputStudents(Student cls[], int n) {
int cnt;
cout << "要录入几名学生?";
cin >> cnt;
if (n + cnt > MAX) {
cout << "超出容量上限!" << endl;
return n;
}
for (int i = 0; i < cnt; i++) {
cout << "第 " << i + 1 << " 名(姓名 分数):";
cin >> cls[n + i].name >> cls[n + i].score;
}
cout << "已录入 " << cnt << " 人" << endl;
return n + cnt; // n 是值传递,必须 return 带回(第 12 课老规矩)
}
void printTable(const Student cls[], int n) {
if (n == 0) {
cout << "(还没有成绩)" << endl;
return;
}
cout << "序号 姓名 分数 等级" << endl;
for (int i = 0; i < n; i++) {
cout << i + 1 << ". " << cls[i].name << " " << cls[i].score
<< " " << gradeName(toGrade(cls[i].score)) << endl;
}
}
double average(const Student cls[], int n) {
double sum = 0;
for (int i = 0; i < n; i++) sum += cls[i].score;
return sum / n;
}
int findTop(const Student cls[], int n) {
int top = 0;
for (int i = 1; i < n; i++) {
if (cls[i].score > cls[top].score) top = i;
}
return top; // 返回下标,姓名分数一起拿到
}
void sortByScore(Student cls[], int n) {
for (int i = 0; i < n - 1; i++) { // n-1 轮
for (int j = 0; j < n - 1 - i; j++) { // 每轮少比一个(尾部已就位)
if (cls[j].score < cls[j + 1].score) {
Student t = cls[j]; // 结构体整体赋值,交换超省心
cls[j] = cls[j + 1];
cls[j + 1] = t;
}
}
}
}
4.1 冒泡排序初步(板书画 3 个格子)
思想:相邻两个比一比,顺序不对就交换。一轮下来,最小的"沉底";n-1 轮后全部就位(降序版:每轮把当前最小沉到尾部)。
| 轮次 | 过程(78, 92, 85) | 结果 |
| 第 1 轮 | 78<92 交换 → 92,78,85;78<85 交换 → 92,85,78 | 78 沉底 |
| 第 2 轮 | 92>85 不动(只比前两个) | 92,85,78 完成 |
交换的是
整个 Student(姓名跟着分数走)——第 16 课整体赋值的威力。平行数组时代这里要搬两条数组,还容易漏。
4.2 验收剧本(已实测)
输入剧本
1 3 张三 92 李四 78 王五 85 ← 录入 3 人
2 ← 成绩表
3 ← 平均分
4 ← 最高分
5 ← 排序
2 ← 再看成绩表
0 ← 退出
关键输出(节选)
1. 张三 92 优秀
2. 李四 78 及格
3. 王五 85 良好
平均分:85
最高分:张三 92
已按分数从高到低排序
1. 张三 92 优秀
2. 王五 85 良好
3. 李四 78 及格
再见!
✏️ 课堂练习(约 15 分钟)
📄
习题纯文本版:c17.txt(可直接发给学生,不含答案;所有参考答案都在本页绿色折叠框里)
练习 1(必做):录入与平均分
clsavg.cpp —— 定义 Student{name, score},录入 3 名学生(用 92、78、85 测试),写
double average(const Student cls[], int n) 输出平均分 85。要求 average 用 const 数组参数。
参考代码(先自己做再看)
clsavg.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student { string name; double score; };
double average(const Student cls[], int n) {
double sum = 0;
for (int i = 0; i < n; i++) sum += cls[i].score;
return sum / n;
}
int main() {
Student cls[3];
for (int i = 0; i < 3; i++) {
cout << "第 " << i + 1 << " 名(姓名 分数):";
cin >> cls[i].name >> cls[i].score;
}
cout << "平均分 = " << average(cls, 3) << endl; // 85
return 0;
}
练习 2(必做):谁是最高分
topstu.cpp —— 写
int findTop(const Student cls[], int n) 返回最高分下标,输出"张三 92"。想想:为什么返回下标比返回分数更好?
参考代码
topstu.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student { string name; double score; };
int findTop(const Student cls[], int n) {
int top = 0;
for (int i = 1; i < n; i++) {
if (cls[i].score > cls[top].score) top = i;
}
return top;
}
int main() {
Student cls[3] = {{"张三", 92}, {"李四", 78}, {"王五", 85}};
int t = findTop(cls, 3);
cout << cls[t].name << " " << cls[t].score << endl; // 张三 92
return 0;
} 返回下标 = 拿到整个档案袋的钥匙,姓名、分数以及以后加的任何成员都能顺手取到;只返回分数就丢了"是谁"。
练习 3(选做):等级翻译官
gradename.cpp —— 用 enum Grade 与 toGrade、gradeName(switch 版),对 92、78、85、59 四个分数输出:优秀、及格、良好、不及格。
参考代码(先自己做再看)
gradename.cpp
#include <iostream>
#include <string>
using namespace std;
enum Grade { FAIL, PASS, GOOD, EXCELLENT };
Grade toGrade(double s) {
if (s >= 90) return EXCELLENT;
if (s >= 80) return GOOD;
if (s >= 60) return PASS;
return FAIL;
}
string gradeName(Grade g) {
switch (g) {
case EXCELLENT: return "优秀";
case GOOD: return "良好";
case PASS: return "及格";
default: return "不及格";
}
}
int main() {
double tests[4] = {92, 78, 85, 59};
for (int i = 0; i < 4; i++) {
cout << tests[i] << " → " << gradeName(toGrade(tests[i])) << endl;
}
return 0; // 优秀 / 及格 / 良好 / 不及格
}
📝 课后作业(下次课前交 .cpp 文件 + 运行截图)
作业 1:冒泡排序降序版
bubble.cpp —— 把项目里的 sortByScore 独立出来,对 {78, 92, 85} 排序后输出 92 85 78(姓名跟着走)。再换 5 个乱序数据验证。要求:先纸上画出第 1 轮的每次比较与交换。
参考代码
bubble.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student { string name; double score; };
void sortByScore(Student cls[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (cls[j].score < cls[j + 1].score) {
Student t = cls[j];
cls[j] = cls[j + 1];
cls[j + 1] = t;
}
}
}
}
int main() {
Student cls[3] = {{"李四", 78}, {"张三", 92}, {"王五", 85}};
sortByScore(cls, 3);
for (int i = 0; i < 3; i++) {
cout << cls[i].name << " " << cls[i].score << endl;
}
return 0; // 张三 92 / 王五 85 / 李四 78
}
作业 2:全班加分
bonus.cpp —— 写
void addBonus(Student& s, double b)(引用改原件),对 {张三 92, 李四 78, 王五 85} 每人加 5 分,输出新分数(97、83、90)与新平均分(90)。对比:如果参数写成 Student s 会怎样?动手试一次。
参考代码(先自己做再看)
bonus.cpp
#include <iostream>
#include <string>
using namespace std;
struct Student { string name; double score; };
void addBonus(Student& s, double b) { s.score += b; }
int main() {
Student cls[3] = {{"张三", 92}, {"李四", 78}, {"王五", 85}};
for (int i = 0; i < 3; i++) addBonus(cls[i], 5);
double sum = 0;
for (int i = 0; i < 3; i++) {
cout << cls[i].name << " " << cls[i].score << endl;
sum += cls[i].score;
}
cout << "新平均分 = " << sum / 3 << endl; // 97 83 90,平均 90
return 0;
} 值传递版加的是复印件的分,main 里纹丝不动——第 10 课悬案的结构体翻版。
作业 3:给成绩表加"修改"功能
edit.cpp —— 在项目基础上加菜单 6:输入姓名,找到后修改分数(复用 findTop 的思路写 findByName,找不到提示"查无此人")。这是第 12 课 editContact 作业的结构体升级版。
参考代码
关键新增代码
int findByName(const Student cls[], int n, string target) {
for (int i = 0; i < n; i++) {
if (cls[i].name == target) return i;
}
return -1; // 查无此人的暗号(第 12 课老配方)
}
// main 的 do-while 里加一个分支:
} else if (choice == 6) {
string target;
cout << "输入要改的姓名:";
cin >> target;
int pos = findByName(cls, n, target);
if (pos == -1) {
cout << "查无此人" << endl;
} else {
cout << "新分数:";
cin >> cls[pos].score; // 数组传参是地址,直接改原件
cout << "已修改" << endl;
}
} 菜单 printMenu 里记得补上"6 修改"。
作业 4(改错题)
下面的代码有
4 处问题,找出并改正:
有错误的代码
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
double score;
};
enum Grade { FAIL, PASS, GOOD, EXCELLENT };
void addBonus(Student s) {
s.score += 5;
}
double average(Student cls[], int n) {
double sum = 0;
for (int i = 0; i <= n; i++) {
sum += cls[i].score;
}
return sum / n;
}
int main() {
Student cls[3] = {{"张三", 92}, {"李四", 78}, {"王五", 85}};
addBonus(cls[0]);
cout << cls[0].score << endl; // 期望 97
cout << average(cls, 3) << endl;
cls[3] = {"赵六", 60};
Grade g = 3; // 想要 EXCELLENT
return 0;
}
参考答案
① addBonus(Student s) 值传递整袋复印,加分加在复印件上——改 void addBonus(Student& s);② average 的 i <= n 越界读 cls[3]——改 i < n;③ cls[3] = {...} 越界写:数组只有 3 格(下标 0~2),想加第 4 人要扩大容量或用第 15 课动态数组;④ Grade g = 3; int 不能隐式转枚举,编译报错——写 Grade g = EXCELLENT;,用名字不用数字。
📌 本课小结(板书用)
- 结构体数组:一个下标 = 一个完整档案袋;
cls[i].score 两级访问,录入输出都自然
- 传参口诀:只看不改 const 引用,要改用引用,值传递整袋复印慎用;数组传参仍是地址
- 枚举 = 有名字的整数常量(默认从 0 递增);int 不能偷偷变枚举,编译器把关
- 冒泡排序:相邻比较交换,n-1 轮,每轮少比一个;交换整个结构体,姓名跟着分数走
- findTop / findByName 返回下标(-1 = 查无此人),拿到下标就拿到整个档案袋
- n 值传递要 return 接住;const 数组参数是"只读承诺"——第 12 课通讯录的全部经验无缝升级
🔭 下节课预告:第 18 课《类与对象、封装》——结构体成员谁都能改太危险?class 给数据上锁:public/private、成员函数、getter/setter。从"档案袋"到"智能对象",面向对象的大门正式打开!