根据这段代码,设计一个等价的结构化程序LOOP: Set I to (START+ FINISH)/2 If TABLE(I)= ITEM goto FOUND If TABLE(I)<ITEM Set START to (I+1) If TABLE(I)> ITEM Set FINISH to (I-1) If (FINISH- START)> 1 goto LOOP If TABLE(START)= ITEM goto FOUND If TABLE (FINISH)= ITEM goto FOUNDSet FLAG to 0 Goto DONE FOUND: Set FLAG to 1 DONE: Exit
时间: 2024-02-25 07:59:33 浏览: 13
下面是等价的结构化程序,使用了while循环和if-else语句:
```
Set I to (START+ FINISH)/2
FLAG = 0
while (START <= FINISH and FLAG == 0):
if TABLE(I) == ITEM:
FLAG = 1
elif TABLE(I) < ITEM:
START = I + 1
else:
FINISH = I - 1
if FINISH - START > 1:
I = (START + FINISH) // 2
else:
if TABLE(START) == ITEM:
FLAG = 1
elif TABLE(FINISH) == ITEM:
FLAG = 1
if FLAG == 1:
# found
else:
# not found
```
该结构化程序使用了while循环来代替goto语句,判断是否找到了目标元素使用了一个标志位FLAG,并在循环结束后进行判断。其他部分基本上按照伪代码的逻辑进行转化。
相关问题
根据这段代码,设计一个等价的结构化程序LOOP: Set I to (START+ FINISH)/2 If TABLE(I)= ITEM goto FOUND If TABLE(I)<ITEM Set START to (I+1) If TABLE(I)> ITEM Set FINISH to (I-1) If (FINISH- START)> 1 goto LOOP If TABLE(START)= ITEM goto FOUND If TABLE (FINISH)= ITEM goto FOUNDSet FLAG to 0 Goto DONE FOUND: Set FLAG to 1 DONE: Exit
下面是等价的结构化程序,使用了do-while循环和if-else语句:
```
Set I to (START+ FINISH)/2
FLAG = 0
do:
if TABLE(I) == ITEM:
FLAG = 1
elif TABLE(I) < ITEM:
START = I + 1
else:
FINISH = I - 1
if FINISH - START > 1:
I = (START + FINISH) // 2
else:
if TABLE(START) == ITEM:
FLAG = 1
elif TABLE(FINISH) == ITEM:
FLAG = 1
while FINISH - START > 1 and FLAG == 0
if FLAG == 1:
# found
else:
# not found
```
该结构化程序使用了do-while循环来代替goto语句,判断是否找到了目标元素使用了一个标志位FLAG,并在循环结束后进行判断。其他部分基本上按照伪代码的逻辑进行转化。
while True: gus=eval(input()) if gus==0x12//2: break这段代码中为什么9可以作为输入结束程序运行
这段代码中的 `0x12//2` 实际上是等价于十进制的 `9`,因为 `0x12` 是十六进制的18,除以2得到9。所以当输入的数值为9时,判断条件 `gus==0x12//2` 就会成立,程序会执行 `break` 语句,跳出 `while` 循环,从而结束程序运行。
值得注意的是,这段代码中的 `eval()` 函数可以执行任意的 Python 代码,包括一些危险的代码,因此在实际应用中应该避免使用 `eval()` 函数,或者对用户输入进行严格的检查和过滤,以确保程序的安全性。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)