本文介绍了是否可以将此流程图写入没有变量和goto的代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在没有变量和 goto
的情况下将此流程图写入C ++?
Is it possible to write this flowchart into C++ without variables and goto
?
这就是我已经拥有的:
i1;
i2;
if(w1) {
i3;
while(w2) {
i2;
if(w1) { break; } // and it shouldn't do i4;
i3;
}
i4;
}
i5;
推荐答案
您可以通过一些简单的递归来实现。您必须注意您的递归具有适当的停止条件,以避免堆栈溢出。
You can do it with some simple recursion. You have to be careful that your recursion has an appropriate "stop" condition to avoid a stack overflow.
将每个框都命名为函数,基本上可以得到以下内容:
Naming each "box" as a function, we get basically the following:
#include <iostream>
using namespace std;
void start_flowing();
int main()
{
// This is the topmost "box"
start_flowing();
return 0;
}
void action_0(); // first "action" box
void check_0(); // first "decision" box
void action_1(); // second "action" box
void check_1(); // second "decision" box
void action_2(); // third "action" box
void complete(); // final "action" box
void start_flowing()
{
// first box goes to second box directly
action_0();
}
void action_0()
{
// first action box goes to first decision directly
check_0();
}
void check_0()
{
// whatever this means...
// this does the evaluation of the decision
bool result = do_the_check();
if(result)
{
// continue "down" to next action box
action_1();
}
else
{
// short circuit out to completion
complete();
}
}
void action_1()
{
check_1();
}
void check_1()
{
// whatever this means...
// this does the evaluation of the decision
bool result = do_the_check();
if(result)
{
// continue "down" to next action box
action_2();
}
else
{
// jump back "up" to first action box
action_0();
}
}
void action_2()
{
// completes the sequence
complete();
}
这篇关于是否可以将此流程图写入没有变量和goto的代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!