堆叠,推送和弹出。火星mips,例如

时间:2015-11-04 20:08:47

标签: mips

字母表示插入,星号表示弹出最初为空的堆栈。假设 执行以下操作顺序。绘制每个步骤的堆栈内容, 然后弹出一系列值。  V E R * Y E * * A S * * Y * * *

现在我的同学们得到了他们的最终答案:

REYSAYEV和我尝试了,我得到了不同的东西

2 个答案:

答案 0 :(得分:2)

为什么机器能做到这一点呢?

https://repl.it/languages/python

尝试此Python代码
instructions = "V E R * Y E * * A S * * Y * * *"

stack = []

for command in instructions.split():
    if command == '*':
        print "Output:", stack.pop()
    else:
        stack.append(command)

    print "Current stack: ", stack

你的同学似乎是正确的;示例输出:

Current stack:  ['V']
Current stack:  ['V', 'E']
Current stack:  ['V', 'E', 'R']
Output: R
Current stack:  ['V', 'E']
Current stack:  ['V', 'E', 'Y']
Current stack:  ['V', 'E', 'Y', 'E']
Output: E
Current stack:  ['V', 'E', 'Y']
Output: Y
Current stack:  ['V', 'E']
Current stack:  ['V', 'E', 'A']
Current stack:  ['V', 'E', 'A', 'S']
Output: S
Current stack:  ['V', 'E', 'A']
Output: A
Current stack:  ['V', 'E']
Current stack:  ['V', 'E', 'Y']
Output: Y
Current stack:  ['V', 'E']
Output: E
Current stack:  ['V']
Output: V
Current stack:  []
=> None

答案 1 :(得分:1)

你得到了什么?以下是:

Stack: VER
Operation: POP
Resultant Stack: VE
Pops: R

Stack: VE
Operation: PUSH Y
Resultant Stack: VEY
Pops: R

Stack: VEY
Operation: PUSH E
Resultant Stack: VEYE
Pops: R

Stack: VEYE
Operation: POP
Resultant Stack: VEY
Pops: RE

Stack: VEY
Operation: POP
Resultant Stack: VE
Pops: REY

等等。