0

我目前正在学习绘制控制流图,我不确定我是否在以下场景中正确绘制它:

对于以下代码片段,其中xy是输入变量,z是输出变量:

if x=0 then x:=10
if x<y-5 then y:=y-1 else x:=y+5
z:=y-x

这就是我所做的:

控制流图

这在任何方面都是正确的吗?提前致谢。

4

1 回答 1

0

不,您的控制流程图不正确。节点代表基本代码块,这意味着它只能包含赋值语句,不能包含条件语句或循环。边表示条件,例如 if then else 语句和 case 语句。

对于这个问题,

1. Start node is the first node with an incoming arrow. So, remove the solid node at the top and leave the arrow as it is.
2. Replace true in first left branch with x == 0
3. Replace false in the first right branch with x != 0 and connect it to the node containing x < y-5. Remove the solid node.
4. Replace true following node containing x < y-5 with x < y-5
5. Replace false following node containing x < y-5 with x >= y-5
6. Remove last two true on the edges
7. Make the node containing z:=y-x as last node and remove the arrow and solid node after this node.
于 2018-06-25T23:08:41.363 回答