Skip to main content

19  Eight Real C and C++ Assignments (C 与 C++ 真实作业八题)

NotePurpose (本章目标)

六个真实 C++ 作业按同一纪律集成:原始测试原样通过、边界补测、真实缺陷如实记录。每题的”踩坑”都来自本仓库开发的第一手记录。

English: Eight real C and C++ assignments under one discipline: original tests pass as-is, boundaries probed, defects logged first-hand.

19.1 Amirkabir 023 (矩阵代数库:测试先行的合同) · profile

17 函数的矩阵库,官方 24 用例 gtest 把每处边界语义钉死:random(min>max) 抛 std::logic_error、空矩阵相乘返回 {}、determinant({}) = 1.0(空积约定)、奇异矩阵 inverse 抛 logic_error。我们的第一版栽了六处——全部由”把测试当合同读”逐条修复。最微妙的是 upper_triangular 禁止选主元:bonus 用例的首主元恰为 0,必须用行加法绕开而不是换行。

English: The 24-case suite is the contract; all six of our first-draft semantic errors were caught by reading it that way — including the no-pivoting rule for upper_triangular.

19.2 Fordham 024 (动态数组表:扩容参数的隐性缩容) · profile

五个 TODO 里最容易埋雷的是 resize 调用:starter 用 count * RFACTOR 而不是 maxCnt * RFACTOR——元素数小于容量时”扩容”反而缩容,满表插入直接越界。另一个教学点是深拷贝三步走(抄形状 → 开新内存 → 搬内容),漏第二步就是析构期 double free。

English: The starter’s own resize call shrinks capacity when count < maxCnt — a full-table insert then writes out of bounds.

19.3 UCI 025 (现代容器三件套:optional 语义) · profile

ECS36C 用 std::optional<T> 表达空栈 pop:返回 nullopt 而不是崩溃或哨兵值。移动构造”偷三根指针、源头置空”一行都不能省。测试编排上,官方用例以注释态发放——我们生成激活副本而不动上游原文件。

English: ECS36C encodes empty-pop as std::optional::nullopt; official tests ship commented-out, activated via copies, never by editing upstream.

19.4 UCI 026 (BST 与 AVL:把退化演给读者看) · profile

同批数据下两个数字最有说服力:顺序插入 1024 键,BST 高度 1024、AVL 高度 11;官方 demo 的 sorted 数据下 BST 高度 26 vs AVL 高度 5。开发中两个真实坑值得记:Node 是 bst.h 的全局 struct(不能写 BSTree::Node);static 工具函数碰不了非静态 count——存在性与计数全部收进公共接口。

English: 26 vs 5 says more about AVL than any paragraph; the global-Node and static-boundary traps are the compile-time curriculum.

19.5 CCNY 027 (回溯三连:骨架相同、剪枝各异) · profile

N-Queens、Sudoku、Sujiko 三个 solve 全空——一套 choose/recurse/unchoose 骨架打三个问题。Sujiko 的宫目标和剪枝有明确的正确性边界:部分和已达目标可砍(必然失败),部分和未达不可砍(可能补上)。测试用 cout.rdbuf 重定向捕获 display_board() 输出,不侵入私有棋盘完成验证。

English: One skeleton, three prunes; redirect the stream to verify the board without touching privates.

19.6 Clemson 028 (修复对照版:13 样例全过仍不正确) · profile

upstream 学生实现通过全部 13 个官方样例,但五处缺陷全部藏在样例覆盖之外:size() 缺 return(UB)、空栈 pop 与空队列 dequeue 解引用空指针、空表读访问崩溃(接口注释明文要求返回 T())、空表 remove 崩溃。修复版只动边界路径,正常路径零改动——官方样例在两个版本上都过,边界测试才把两者分开。

官方样例: 13 passed, 0 failed
边界测试: empty-list/stack/queue edges + LIFO/FIFO + List ops 全过

English: All five defects live outside sample coverage — “13/13 pass” was never proof; boundary tests are what separate the two versions.

19.7 Princeton 033 (去注释:六十行状态机与七十四个边界) · profile

COS 217 的去注释题只要求一个过滤器,却把词法边界一次讲透:注释是 token 分隔符(int/**/x; 必须留一个空格),注释跨行要补回换行以保持行号,/* 在字符串与字符字面量里没有特殊含义,反斜杠会让下一个引号失去定界作用。八个状态的 DFA 覆盖全部情形,斜杠单独成状态以实现一格前瞻。

测试分两层:题面表格里的例子逐字节抄成 38 条金标准;starter 自带的 74 个输入文件没有公布答案,用独立 Python oracle 交叉验证。oracle 自己先错了一次——空格写在换行之后,15 个用例同时红。而交叉验证抓到的真 bug 是文件以孤立 / 结尾时该字符被吞掉:状态机在 EOF 时仍停在”待定斜杠”,还欠一个输出。

golden cases: 38/38 passed
oracle cases: 74/74 passed

English: A 60-line DFA, 38 spec-exact cases and 74 starter inputs cross-checked against an independently written oracle — which caught a trailing slash silently dropped at EOF.

19.8 Berkeley 034 (贪吃蛇:棋盘即状态) · profile

CS61C 的 proj1 用裸 C 写一个完整游戏引擎。设计上最值得记的一点是棋盘自己就是状态:每个蛇格是一个字符,同时编码”哪条蛇的哪个部位”、“朝向”和”下一步去哪”。因此不需要速度数组,update_tail 只要沿着尾字符的方向跟一格,O(1) 完成。

二十多个函数、21 组官方输入/参考输出、1708 行官方单元测试。单测纠正了一处规格误读:create_default_game 必须自带那条蛇(num_snakes == 1),而不是留给 initialize_snakes——跟着改 main,否则重复初始化会泄漏。另一个真实缺陷是 read_line 不检查空文件指针,在 fgets(NULL) 上段错误。

integration tests: 21 passed, 0 failed
unit tests: PASS
ASan (21 boards): clean, no leaks

English: The board is the state — one character carries part, heading and next cell. 21 reference boards plus the official unit suite; the tests corrected the spec reading, not the other way round.