如何为以下图表编写伪代码?

4

我没有成功使用do-while或while循环来完成以下图表:

enter image description here

这里,A、B和C都是函数。如何为上述图表编写伪代码?

编辑:这是我在练习C++编程时遇到的问题。如果没有“B循环”(或“A循环”),我可以将其编写为以下形式:

Start
Input x;
while(x!=2)
{
A(); Input x;
}
C();
End

然而,当"B循环"出现时,我不知道如何将其包含在内。

闻起来像是作业。如果你懂C#,只需删除类型信息并去掉分号即可。 - leppie
你有尝试过什么吗?看看http://en.wikipedia.org/wiki/Pseudocode。 - home
我本应该将 A, B, C 写成 A(), B(), C(),因为它们都是函数。 - user758077
@home:是的,我做了。让我困惑的是这两个循环。 - user758077
3个回答

4
Start;

Input x;

while(x!=2){ 

    if (x!=1){
        A();
    } else{ 
        B()
    }
    Input x;
}

C();

End

但是要注意你使用的语言,我建议在数据采集之间添加休眠模式(就在输入x之前)。


2
@BaptisteGousset的回答很好,但在某些情况下,有一种替代方法可能更好。可以使用do...while循环而不是while来简化代码以删除重复的Input操作,但这会使比较逻辑稍微复杂一些。
Start;
Do
{
    Input x;
    if (x equals 1)
    {
        B();
    }
    elseif(x not equal to 2)
    {
        A();
    }

} While (x not equal to 2);
C();
End;

您也可以使用goto语句来实现这一点,但是goto通常被认为是有害的。
通常情况下,对于任何流程图,没有一个单一的规范“伪代码”答案-它取决于您愿意使用哪些结构以及这些结构与您实际任务的优雅程度。

2
程序的作用是什么? 用英语解释,然后将其写下来。 然后你就有了伪代码。
If any input
 if input is not 1 and not 2
 return a  and do more input (? dont get the diagram here ;p)
 if input is 1
 return b and more input (??)
 else if not above
 return c and end program

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接