用循环方法实现解释器模式两位数的简单加减法运算
时间: 2024-05-01 10:21:45 浏览: 19
好的,我可以为您编写一个简单的解释器,用于解析和计算两位数的加减法表达式。下面是实现的伪代码:
```
while (expression 中还有下一个 token) {
if (下一个 token 是数字) {
将数字压入数字栈中;
} else if (下一个 token 是加号或减号) {
将运算符压入运算符栈中;
} else if (下一个 token 是右括号) {
将运算符栈中的栈顶运算符弹出,并弹出数字栈中的顶部两个数字;
计算这两个数字的运算结果,并将结果压入数字栈中;
}
}
最终数字栈中唯一剩余的数字就是表达式的计算结果。
```
下面是完整的 Python 代码实现,用于计算两位数的加减法表达式:
```python
class Interpreter:
def __init__(self, expression):
self.expression = expression.replace(" ", "")
self.pos = 0
self.current_token = None
def parse(self):
self.current_token = self.get_next_token()
result = self.expr()
if self.current_token is not None:
raise Exception("Invalid syntax")
return result
def expr(self):
result = self.term()
while self.current_token is not None and self.current_token in "+-":
op = self.current_token
self.eat(op)
if op == "+":
result = result + self.term()
elif op == "-":
result = result - self.term()
return result
def term(self):
result = self.factor()
while self.current_token is not None and self.current_token in "0123456789":
result = result * 10 + int(self.current_token)
self.pos += 1
if self.pos >= len(self.expression):
self.current_token = None
else:
self.current_token = self.expression[self.pos]
return result
def factor(self):
if self.current_token == "(":
self.eat("(")
result = self.expr()
self.eat(")")
return result
elif self.current_token in "0123456789":
return self.term()
else:
raise Exception("Invalid syntax")
def get_next_token(self):
if self.pos >= len(self.expression):
return None
token = self.expression[self.pos]
self.pos += 1
return token
def eat(self, token):
if self.current_token != token:
raise Exception("Invalid syntax")
self.current_token = self.get_next_token()
if __name__ == "__main__":
while True:
try:
text = input("calc> ")
except EOFError:
break
if not text:
continue
interpreter = Interpreter(text)
result = interpreter.parse()
print(result)
```
您可以尝试输入表达式如:`2 + 3 - 4`,程序将会输出 `-1`。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)