2012-04-23 39 views
2

我没有使用DO-同时或while循环用于下图:如何为以下图表编写伪代码?

enter image description here

在此,A,B,和C是功能。 如何为上面的图表编写伪代码?

编辑:这是从我的C++编程实践。如果没有“B环”(或“A环”),我可以把它写成如下:

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

然而,当“B环”进来,我不知道如何将它。

+0

闻起来像功课。如果你知道说C#,只需删除类型信息并丢失分号。 – leppie 2012-04-23 07:08:52

+0

你有什么尝试吗?看看http://en.wikipedia.org/wiki/Pseudocode – home 2012-04-23 07:09:38

+0

我应该把'A','B','C'写成'A()','B()','C()',因为它们都是功能。 – Jack 2012-04-23 07:26:07

回答

4
Start; 

Input x; 

while(x!=2){ 

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

C(); 

End 

但要注意你使用我建议你添加数据新添的书籍(只需输入X之前)之间的睡眠模式langage的

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 
+0

感谢您的回答。如果我需要'C++'代码呢? – Jack 2012-04-23 07:17:26

+1

那么你需要改变你的问题.. – 2012-04-23 08:51:10

2

@BaptisteGousset的回答是不错的,但有在某些情况下可能更好的替代方法。可以使用do ... while循环代替一段时间来简化代码以删除加倍的输入操作,但这会使比较逻辑更复杂一些。

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语句,但后藤往往是considered harmful

通常,任何流程图都不存在任何规范的“伪代码”答案 - 它取决于您愿意使用哪种结构以及这些结构如何优雅地适合您的实际任务。