复古C语言游戏代码修复与解析——以79泊松分酒为例
本文基于《程序设计语言(B)》课程报告任务,选取GameCode79文件夹中的79号C程序进行修复与解析。该程序为古老的“泊松分酒”问题求解代码,因语法陈旧无法在现代编译环境中直接编译。本文详细记录了从编译错误分析、代码修正、功能理解、流程图绘制,到使用VSCode编译和Gitee进行版本管理的全过程。通过本次任务,不仅加深了对C语言语法演进的理解,也锻炼了借助AI和搜索引擎解决实际问题的能力。C
摘要
本文基于《程序设计语言(B)》课程报告任务,选取GameCode79文件夹中的79号C程序进行修复与解析。该程序为古老的“泊松分酒”问题求解代码,因语法陈旧无法在现代编译环境中直接编译。本文详细记录了从编译错误分析、代码修正、功能理解、流程图绘制,到使用VSCode编译和Gitee进行版本管理的全过程。通过本次任务,不仅加深了对C语言语法演进的理解,也锻炼了借助AI和搜索引擎解决实际问题的能力。
一、选题与准备
1.1 选题
在GameCode155文件夹中,我选择了编号为79的程序。该程序标题为“Poisson Beer Division Problem”,是一个经典的分酒逻辑问题,具有一定的算法代表性,适合用于理解流程控制与函数调用。
1.2 环境准备
int main(void) {
int a, y, z, i;
// ... 输入输出逻辑
getti(a, y, z, i);
getchar();
return 0;
}
修正后的主函数部分代码如下:
-
操作系统:Windows 11
-
编译器:Dev-C++ 5.11、VSCode + GCC (MinGW 8.1.0)
-
辅助工具:ChatGPT、CSDN、搜索引擎、Visio 2021
-
代码管理:Gitee、Git
二、编译调试过程
2.1 初始编译与错误分析
使用Dev-C++打开原文件
155.c,首次编译出现以下典型错误:[Error] 'clrscr' was not declared in this scope [Error] 'getch' was not declared in this scope [Warning] return type of 'main' is not 'int'
这些错误表明原程序使用了Turbo C等古老环境特有的函数和非标准语法。
2.2 错误修正
针对上述问题,逐步进行修正:
-
主函数类型:将
void main()改为int main(void),并添加return 0;。 -
清屏函数:删除
clrscr();,因其不具备跨平台性。 -
等待按键:将
getch();替换为标准C的getchar();。 -
全局变量移除:原程序使用全局变量存储目标容量,改为通过函数参数传递,提高模块化。
int main(void) {
int a, y, z, i;
// ... 输入输出逻辑
getti(a, y, z, i);
getchar();
return 0;
}
2.3 反复调试
经过多次编译测试,最终实现零错误、零警告
编译成功:0 errors, 0 warnings 生成 155.exe
三、代码理解与重命名
3.1 运行测试
运行修复后的程序,输入示例数据:
#include<stdio.h>
// 函数声明:添加目标容量i作为参数,替代全局变量
void getti(int a, int y, int z, int i);
// 标准主函数定义
int main(void)
{
int a, y, z, i; // 改为局部变量,消除全局变量风险
// 移除clrscr():现代编译器不支持,若需清屏可使用对应平台方法(如Windows的system("cls")、Linux的system("clear"))
puts("***************************************************************");
puts("* This program is to solve Problem of Poisson Beer Division. *");
puts("* The Problem is as follows: Someone has a bottle of 12 pints *");
puts("* beer. He wants to get 6 pints, but he does not has a bottle *");
puts("* of 6 pints. He only has a bottle of 8 pints and a bottle of *");
puts("* 5 pints. So how can he get 6 pints? *");
puts("***************************************************************");
// 优化提示信息,避免中文乱码
printf(" >> Input Full bottle a, Empty b, Empty c, and Target volume:\n");
printf(" >> ");
// 读取4个整数:a(满瓶)、y(空瓶1)、z(空瓶2)、i(目标)
scanf("%d%d%d%d", &a, &y, &z, &i);
// 调用函数,传入目标容量i
getti(a, y, z, i);
printf("\n Press any key to quit...");
// 替代getch(),标准C函数,等待用户输入后退出
getchar();
// 主函数正常返回值,标识程序运行成功
return 0;
}
// 函数定义:添加参数i(目标容量),注释改为英文避免乱码
// a: Capacity of full bottle, y: Capacity of empty bottle 1, z: Capacity of empty bottle 2
// i: Target volume to get
void getti(int a, int y, int z, int i)
{
int b=0, c=0, j=0; // b: Actual volume in bottle 1, c: Actual volume in bottle 2, j: Step counter
puts(" >> The division steps are as follows.\n");
printf(" Bottle: a<%d> b<%d> c<%d>\n", a, y, z);
printf("-----------------------------\n");
printf(" Step No.|\n");
printf(" <%d> | %4d %4d %4d\n", j++, a, b, c);
// 修复循环条件:当三个瓶子都未达到目标容量时,继续循环
while(a != i && b != i && c != i)
{
if(!b) // If bottle 1 is empty, pour from a to b (fill b)
{ a -= y; b = y;}
else if(c == z) // If bottle 2 is full, pour from c to a (empty c)
{ a += z; c = 0;}
else if(b > z - c) // If bottle 1 has more than the remaining space of bottle 2
{ b -= (z - c); c = z;} // Pour to fill bottle 2, leave the rest in bottle 1
else{ c += b; b = 0;} // Pour all from bottle 1 to bottle 2
printf(" <%d> | %4d %4d %4d\n", j++, a, b, c);
}
printf("-----------------------------\n");
}
程序输出分步倒酒过程,最终在某个瓶子中得到6品脱酒。运行界面清晰展示了每一步三个瓶子的酒量变化。
3.2 流程图
程序通过循环和条件判断模拟倒酒过程,直到某一瓶满足目标容量。
开始 → 输入参数 → 初始化变量 → 判断是否达到目标
↓
选择倒酒规则
↓
更新瓶子状态
↓
输出当前步骤
↓
是否达到目标? → 是 → 结束
↓否
继续循环
3.3 重命名
根据程序功能,将原文件重命名为:155泊松分酒.c
四、使用VSCode管理项目
4.1 VSCode环境配置
-
安装VSCode,并添加C/C++扩展
-
配置MinGW环境变量"command": "gcc",
-
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"]
4.2 在VSCode中编译运行
通过快捷键Ctrl+Shift+B编译,F5调试运行,成功在VSCode中编译并执行程序。
五、代码版本管理
5.1 Gitee仓库创建
在Gitee创建公开仓库C-Course-Report-155,用于存放项目文件。
5.2 VSCode中集成Git
本任务不仅是一次代码修复练习,更是一次完整的项目开发与文档撰写训练,为后续课程和实际开发打下了良好基础。
-
在VSCode中初始化仓库git add .
-
git commit -m "修复并提交155泊松分酒程序" git push origin main
六、总结
通过本次课程报告任务,我深刻体会到:
-
C语言标准的演进:古老的语法在现代环境中需适配,理解标准变迁对编程兼容性至关重要。
-
调试与检索能力:利用AI和社区资源逐条解决编译错误,提升了问题定位与解决能力。
-
工具链的掌握:从Dev-C++到VSCode再到Git,掌握了现代C语言开发与版本管理的基本流程。
-
批判性思维的培养:在修复代码时不断思考“为什么这样写”“是否有更好写法”,逐步形成代码优化的意识。
更多推荐



所有评论(0)