⏱ 70 分钟 | 前置:第 1~11 课全部 | 缓冲课:进度慢可压缩串讲,保住后面面向对象课时
| 时间 | 环节 |
|---|---|
| 0–5 分钟 | 复习第 11 课 + 作业讲评(重点讲递归展开图和汉诺塔步数) |
| 5–25 分钟 | 知识大串讲:知识地图 + 十大高频坑(学生抢答) |
| 25–30 分钟 | 项目需求分析与模块划分(板书画结构图) |
| 30–55 分钟 | 通讯录项目:边讲边敲 + 学生跟做 |
| 55–65 分钟 | 课堂练习:补全删除功能 + 快的人做修改功能 |
| 65–70 分钟 | 总结 + 布置作业(含阶段测验) |
| 课次 | 一句话核心 | 高频坑 |
|---|---|---|
| 1 环境 | 编译→运行;报错信息是线索不是天书 | 中文分号/引号;忘保存就编译 |
| 2 变量 | 贴标签的盒子;先定义初始化再使用 | 未初始化垃圾值;int 装小数被截断 |
| 3 运算符 | / 拿整份 % 拿零头;== 比较 = 赋值 | 7/2=3;链式比较 60<=x<=100 |
| 4 分支 | if 链命中即退出;switch 配 break | if 后分号;悬空 else;= 当 == |
| 5 循环 | 三要素:起点条件变化;累加0累乘1计数0 | 差一错误;死循环(Ctrl+C);容器放循环里 |
| 6 嵌套 | 外层一步内层一圈;穷举+递推 | 换行位置;isPrime 每轮要重置 |
| 7 一维数组 | 下标 0~n-1;遍历骨架 i<n;擂台=a[0] | 越界不报错;maxVal 初值设 0 |
| 8 二维/字符 | 表格 a[i][j];字符串以 '\0' 结尾 | 行列都越界;char[5] 装 "hello" |
| 9 string | +拼接 length() substr find | cin>>后 getline 要 ignore;find 不判 npos |
| 10 函数 | 打包工具;return 出货;值传递复印件 | 忘原型声明;指望形参改实参 |
| 11 进阶 | 局部变量各层独立;递归=终止+靠近 | 无终止栈溢出;默认参数不在最右 |
前 11 课的程序都挤在 main 里。真正的软件像餐厅:main 是前台(接单、调度),函数是后厨各岗位(一个岗位一道工序),数组是仓库(存原料)。
今天的项目分工(板书画结构图):
printMenu() —— 贴菜单addContact(...) —— 入库新联系人listContacts(...) —— 盘点仓库findContact(...) —— 按名字找货(返回下标或 -1)deleteContact(...) —— 出库(后面的人往前挪)数据用平行数组:names[i] 和 phones[i] 是同一个人的两样信息,下标对齐。第 16 课学结构体后会升级成"一个变量装一个人"。
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;
void printMenu();
int addContact(string names[], string phones[], int n);
void listContacts(string names[], string phones[], int n);
int findContact(string names[], int n, string target);
int deleteContact(string names[], string phones[], int n, int pos);
int main() {
string names[MAX], phones[MAX]; // 平行数组:下标对齐 = 同一个人
int n = 0; // 实际人数
int choice;
do {
printMenu();
cout << "请选择:";
cin >> choice;
if (choice == 1) {
n = addContact(names, phones, n); // 接住新人数!
} else if (choice == 2) {
listContacts(names, phones, n);
} else if (choice == 3) {
string target;
cout << "输入要查的姓名:";
cin >> target;
int pos = findContact(names, n, target);
if (pos == -1) {
cout << "查无此人" << endl;
} else {
cout << names[pos] << " : " << phones[pos] << endl;
}
} else if (choice == 4) {
string target;
cout << "输入要删的姓名:";
cin >> target;
int pos = findContact(names, n, target);
if (pos == -1) {
cout << "查无此人" << endl;
} else {
n = deleteContact(names, phones, n, pos);
cout << "已删除" << endl;
}
}
} while (choice != 0);
cout << "再见!" << endl;
return 0;
}
void printMenu() {
cout << "===== 通讯录 =====" << endl;
cout << "1 添加 2 列表 3 查找 4 删除 0 退出" << endl;
}
int addContact(string names[], string phones[], int n) {
if (n >= MAX) {
cout << "通讯录已满!" << endl;
return n; // 满了人数不变
}
cout << "姓名:";
cin >> names[n]; // 新人放在下标 n(第 n+1 个位置)
cout << "电话:";
cin >> phones[n];
cout << "已保存:" << names[n] << endl;
return n + 1; // 人数 +1 交回给 main
}
void listContacts(string names[], string phones[], int n) {
if (n == 0) {
cout << "(通讯录是空的)" << endl;
return;
}
for (int i = 0; i < n; i++) {
cout << i + 1 << ". " << names[i] << " " << phones[i] << endl;
}
}
int findContact(string names[], int n, string target) {
for (int i = 0; i < n; i++) {
if (names[i] == target) {
return i; // 找到:返回下标
}
}
return -1; // 第 7 课的 -1 暗号
}
int deleteContact(string names[], string phones[], int n, int pos) {
for (int i = pos; i < n - 1; i++) { // 从被删位置起,后面每人往前挪一格
names[i] = names[i + 1];
phones[i] = phones[i + 1];
}
return n - 1; // 人数 -1(最后一个旧数据不用管,n 管着边界)
}names[i] 外面真的变。机制第 14 课指针揭秘,现在先记规则:数组参数改元素有效,普通参数改形参无效。return n + 1 让 main 接住:n = addContact(...)。忘了接 = 添加了个寂寞。i < n - 1 保证 i + 1 不越界(差一错误防线)。i < n - 1 吗?③ 空列表有提示吗?
printMenu,让标题显示当前人数,如 ===== 通讯录(3 人)=====。void printMenu(int n); // 声明也要同步改! void printMenu(int n) { cout << "===== 通讯录(" << n << " 人)=====" << endl; cout << "1 添加 2 列表 3 查找 4 删除 0 退出" << endl; } // main 里:printMenu(n);
cout << "姓名:";
cin >> names[n];
if (findContact(names, n, names[n]) != -1) { // 注意:只在 0~n-1 里找,新输入的还没入库
cout << "已有同名联系人,取消添加" << endl;
return n;
}
cout << "电话:";
cin >> phones[n];5 修改:输入姓名,找到后输入新电话并更新;找不到提示"查无此人"。void editContact(string names[], string phones[], int n, int pos),main 里复用 findContact。
void editContact(string names[], string phones[], int n, int pos) {
cout << "当前电话:" << phones[pos] << endl;
cout << "新电话:";
cin >> phones[pos]; // 数组是原件,直接改就生效
cout << "已更新" << endl;
}
// main 的 do-while 里加一个分支:
} else if (choice == 5) {
string target;
cout << "输入要改的姓名:";
cin >> target;
int pos = findContact(names, n, target);
if (pos == -1) {
cout << "查无此人" << endl;
} else {
editContact(names, phones, n, pos);
}
}7/2、7%2、7/2.0 各等于多少?cout << (5 >= 3 && 3 > 4); 输出什么?if (score = 100) 有什么问题?for (int i = 0; i < 5; i++) 循环体执行几次?最后一轮 i 是多少?int a[5]; 合法下标范围?a[5] 会怎样?char s[] = "hello"; 占几字节?s[5] 是什么?cin >> s 和 getline(cin, s) 的区别?混用时要注意什么?guess3.cpp —— 把第 5 课的猜数字升级成函数版:难度菜单(1 简单 1~50 猜 10 次 / 2 普通 1~100 猜 7 次 / 3 困难 1~200 猜 5 次),写 bool playGame(int range, int maxTries) 封装一局(返回是否猜中),main 里支持"再来一局"循环。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
bool playGame(int range, int maxTries) {
int secret = rand() % range + 1;
int guess = 0;
cout << "=== 1~" << range << ",最多 " << maxTries << " 次 ===" << endl;
for (int t = 1; t <= maxTries; t++) {
cout << "第 " << t << " 次,你猜:";
cin >> guess;
if (guess == secret) {
cout << "猜中了!共 " << t << " 次" << endl;
return true;
}
cout << (guess > secret ? "大了!" : "小了!") << endl;
}
cout << "次数用完,答案是 " << secret << endl;
return false;
}
int main() {
srand(time(0));
char again;
do {
int level;
cout << "难度:1 简单 2 普通 3 困难:";
cin >> level;
if (level == 1) playGame(50, 10);
else if (level == 2) playGame(100, 7);
else playGame(200, 5);
cout << "再来一局?(y/n):";
cin >> again;
} while (again == 'y');
cout << "拜拜!" << endl;
return 0;
}i < n - 1;查找失败 = -1 暗号