feat: 好几章
This commit is contained in:
147
3.编程思维体系构建/3.4.6.2.探索未知.md
Normal file
147
3.编程思维体系构建/3.4.6.2.探索未知.md
Normal file
@@ -0,0 +1,147 @@
|
||||
# 2.探索未知
|
||||
|
||||
##### <strong> 驾驭项目, 而不是被项目驾驭</strong>
|
||||
|
||||
你和一个项目的关系会经历 4 个阶段:
|
||||
|
||||
1. 被驾驭: 你对它一无所知
|
||||
2. 一知半解: 你对其中的主要模块和功能有了基本的了解
|
||||
3. 驾轻就熟: 你对整个项目的细节都了如指掌
|
||||
4. 为你所用: 你可以随心所欲地在项目中添加你认为有用的功能
|
||||
|
||||
如果你想要达成第二个阶段,你需要仔细学习不断探索更新的内容, 达到第三个阶段的主要手段是独立完成实验内容和独立调试. 至于要达到第四个阶段, 就要靠你的主观能动性了: 代码还有哪里做得不够好? 怎么样才算是够好? 应该怎么做才能达到这个目标?
|
||||
|
||||
你毕业后到了工业界或学术界, 就会发现真实的项目也都是这样:
|
||||
|
||||
1. 刚接触一个新项目, 不知道如何下手
|
||||
2. RTFM, RTFSC, 大致明白项目组织结构和基本的工作流程
|
||||
3. 运行项目的时候发现有非预期行为(可能是配置错误或环境错误, 可能是和已有项目对接出错, 也可能是项目自身的 bug), 然后调试. 在调试过程中, 对这些模块的理解会逐渐变得清晰.
|
||||
4. 哪天需要你在项目中添加一个新功能, 你会发现自己其实可以胜任.
|
||||
|
||||
这说明了: 如果你一遇到 bug 就找大神帮你调试, 你失去的机会和能力会比你想象的多得多
|
||||
|
||||
文字冒险游戏的基本交互很简单
|
||||
|
||||
1. 玩家输入命令。
|
||||
2. 程序 [解析](http://en.wikipedia.org/wiki/Parsing) 并执行命令。
|
||||
3. 重复步骤 1 和 2,直到玩家决定退出
|
||||
|
||||
那么,当命令很多的时候,如果你将他写在一起,一个文件有五六千行,我相信这样的情况你是不愿意去看的,因此,我们引入了函数的概念。
|
||||
|
||||
自行了解函数的概念,同时去了解当我需要引用别的文件的函数时该怎么办?
|
||||
|
||||
了解一下什么是“驼峰原则”,我们为什么要依据它命名函数?
|
||||
|
||||
下面的代码示例包含三个函数,每个步骤一个函数:
|
||||
|
||||
1. 函数<em>getInput</em>。
|
||||
2. 函数<em>parseAndExecute</em>。
|
||||
3. 函数<em>main</em>,负责重复调用其他两个函数。
|
||||
|
||||
# main.c
|
||||
|
||||
```c
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "parsexec.h"
|
||||
//当我需要引用别的文件
|
||||
static char input[100] = "look around";
|
||||
//定义全局变量
|
||||
static bool getInput(void)
|
||||
{
|
||||
printf("\n--> ");
|
||||
//你可以将他改成你喜欢的内容
|
||||
return fgets(input, sizeof input, stdin) != NULL;
|
||||
//fgets用于收集键盘的输入
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Welcome to Little Cave Adventure.\n");
|
||||
while (parseAndExecute(input) && getInput());
|
||||
printf("\nBye!\n");
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
注意:某些老版本的 C 语言不支持 bool 选项,你将他改为 int 是一样的。
|
||||
|
||||
思考题:static 是什么意思?我为什么要用他?
|
||||
|
||||
# <strong>parsexec.h</strong>
|
||||
|
||||
```c
|
||||
extern bool parseAndExecute(char *input);
|
||||
```
|
||||
|
||||
思考题:extern 是干什么的?.h 文件又在干嘛?
|
||||
|
||||
哇,我用了一个指针!input 前面是个指针!!!
|
||||
|
||||
指针是啥?[C 指针详解](https://www.runoob.com/w3cnote/c-pointer-detail.html) STFW(虽然都给你了)
|
||||
|
||||
在这里用指针是为了传参的时候可以传字符串哦
|
||||
|
||||
# <strong>parsexec.c</strong>
|
||||
|
||||
```c
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
bool parseAndExecute(char *input)
|
||||
{
|
||||
char *verb = strtok(input, " \n");
|
||||
char *noun = strtok(NULL, " \n");
|
||||
//strtok是string库下的一个函数
|
||||
if (verb != NULL)
|
||||
{
|
||||
if (strcmp(verb, "quit") == 0)
|
||||
//strcmp也是
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (strcmp(verb, "look") == 0)
|
||||
{
|
||||
printf("It is very dark in here.\n");
|
||||
}
|
||||
else if (strcmp(verb, "go") == 0)
|
||||
{
|
||||
printf("It's too dark to go anywhere.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("I don't know how to '%s'.\n", verb);
|
||||
//%s是verb附加参数的占位符
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
你的编译器可能会给出警告 the unused variable ‘noun’,这些不用担心,将会在下一章解决。
|
||||
|
||||
返回<em>false</em>将导致主循环结束。
|
||||
|
||||
RTFM&&STFW 搞懂 strtok 和 strcmp 的用法
|
||||
|
||||
考虑一下 NULL 是干什么的
|
||||
|
||||
测试样例
|
||||
|
||||
Welcome to Little Cave Adventure.
|
||||
It is very dark in here.
|
||||
|
||||
--> go north
|
||||
It's too dark to go anywhere.
|
||||
|
||||
--> look around
|
||||
It is very dark in here.
|
||||
|
||||
--> eat sandwich
|
||||
I don't know how to 'eat'.
|
||||
|
||||
--> quit
|
||||
|
||||
Bye
|
||||
Reference in New Issue
Block a user