import{_ as h,D as l,c as p,j as s,I as a,w as n,a as k,a4 as t,o as e}from"./chunks/framework.DtvhUNIn.js";const V=JSON.parse('{"title":"2.探索未知","description":"","frontmatter":{},"headers":[],"relativePath":"技术资源汇总(杭电支持版)/3.编程思维体系构建/3.4.6.2.探索未知.md","filePath":"技术资源汇总(杭电支持版)/3.编程思维体系构建/3.4.6.2.探索未知.md"}'),r={name:"技术资源汇总(杭电支持版)/3.编程思维体系构建/3.4.6.2.探索未知.md"},E=s("h1",{id:"_2-探索未知",tabindex:"-1"},[k("2.探索未知 "),s("a",{class:"header-anchor",href:"#_2-探索未知","aria-label":'Permalink to "2.探索未知"'},"")],-1),d={class:"tip custom-block"},g={class:"custom-block-title"},o=s("strong",null,"驾驭项目,而不是被项目驾驭",-1),F=t("
你和一个项目的关系会经历 4 个阶段:
如果你想要达成第二个阶段,你需要仔细学习不断探索更新的内容,达到第三个阶段的主要手段是独立完成实验内容和独立调试。至于要达到第四个阶段,就要靠你的主观能动性了:代码还有哪里做得不够好?怎么样才算是够好?应该怎么做才能达到这个目标?
你毕业后到了工业界或学术界,就会发现真实的项目也都是这样:
这说明了:如果你一遇到 bug 就找大神帮你调试,你失去的机会和能力会比你想象的多得多
",6),c=t(`文字冒险游戏的基本交互很简单
那么,当命令很多的时候,如果你将他写在一起,一个文件有五六千行,我相信这样的情况你是不愿意去看的,因此,我们引入了函数的概念。
🤔 自行了解函数的概念,同时去了解当我需要引用别的文件的函数时该怎么办?
了解一下什么是“驼峰原则”,我们为什么要依据它命名函数?
下面的代码示例包含三个函数,每个步骤一个函数:
#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 是什么意思?我为什么要用他?
extern bool parseAndExecute(char *input);#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’,这些不用担心,将会在下一章解决。
返回false将导致主循环结束。
`,17),y={class:"warning custom-block"},u={class:"custom-block-title"},C=s("strong",null,"RTFM&&STFW",-1),_=s("p",null,"搞懂 strtok 和 strcmp 的用法",-1),A=s("p",null,"考虑一下 NULL 是干什么的",-1),B=s("p",null,"测试样例",-1),D=s("p",null,"Welcome to Little Cave Adventure. It is very dark in here.",-1),m=s("p",null,"--> go north It's too dark to go anywhere.",-1),b=s("p",null,"--> look around It is very dark in here.",-1),v=s("p",null,"--> eat sandwich I don't know how to 'eat'.",-1),f=s("p",null,"--> quit",-1),q=s("p",null,"Bye",-1);function x(w,T,I,N,P,S){const i=l("font");return e(),p("div",null,[E,s("div",d,[s("p",g,[a(i,{size:"5"},{default:n(()=>[o]),_:1})]),F]),c,s("div",y,[s("p",u,[a(i,{size:"5"},{default:n(()=>[C]),_:1})]),_]),A,B,D,m,b,v,f,q])}const R=h(r,[["render",x]]);export{V as __pageData,R as default};