【发布时间】:2018-09-11 20:57:29
【问题描述】:
我必须编写一个 Stack 类,该类使用后缀表示法来计算使用 Python 从文本文件输入的问题。我还想更新列表,并且任何时候都不允许超过 10 个项目。我相信我的 push 或 pop 方法似乎有问题。任何建议将不胜感激,谢谢。
更新的书面代码:
class Stack:
#initializing the data
def __init__(self):
self.__data = [None] * 10
self.__length = 0
#destroying the existing stack
def __destroy__(self):
self.__data = [None] * 10
self.__length = 0
return
#checking to see if the stack is empty
def is_stack_empty(self):
return self.__length == 0
#checking to see if the stack is full
def is_stack_full(self):
if self.__length == 10:
return True
else:
return False
#pushing an item to the stack
def push(self, item):
try:
self.__data.append(item)
self.__length = self.__length + 1
return
except IndexError:
print("ERROR: Cannot push more than 10 items to the stack.")
#popping an item from the stack
def pop(self):
try:
return self.__data.pop()
self.__length = self.__length - 1
return
except IndexError:
print("ERROR: Cannot pop from an empty list.")
#checking to see what the last item on the stack is
def top(self):
return self.__data[-1]
#checking the length of the stack
def __len__(self):
return self.__length
#overloading the string operator
def __str__(self):
return str(self.__data)
def main():
file_name = input("Enter the name of the file containing postfix expressions: ")
#expression = input("Enter a mathmatical expression: ")
#e_list = expression.split()
#validates file
found = False
while not found:
try:
in_file = open(file_name)
found = True
except Exception as ex:
print(file_name, " is not found.")
file_name = input("Please re-enter a valid text file name: ")
stack = Stack()
for each in in_file:
try:
value = int(each)
except Exception as ex:
second = stack.pop()
first = stack.pop()
if each == "+":
try:
answer = first + second
stack.push(answer)
except Exception as ex:
print("ERROR: "+ expression + " is an invalid postfix expression.")
elif each == "-":
try:
answer = first - second
stack.push(answer)
except Exception as ex:
print("ERROR: "+ expression + " is an invalid postfix expression.")
elif each == "*":
try:
answer = first * second
stack.push(answer)
except Exception as ex:
print("ERROR: "+ expression + " is an invalid postfix expression.")
elif each == "/":
try:
answer = first / second
stack.push(answer)
except Exception as ex:
print("ERROR: "+ expression + " is an invalid postfix expression.")
else:
stack.push(value)
print("Answer: ", stack.pop())
return
main()
文件内容:
5 10 * 6 +
20 5 /
3 8 6 + *
3 4 + 9 - 12 +
9 3 2 1 + + /
3 + 5
* 3 4 5 + *
4 9 1 3 + -
h 3 +
0 1 2 3 4 5 6 7 8 9 10 11 12 + / - * + + + + + * * /
结果输出:
Enter the name of the file containing postfix expressions: expressions.txt
Expression: 5 10 * 6 +
Answer: 56
Expression: 20 5 /
Answer: 4.0
Expression: 3 8 6 + *
Answer: 42
Expression: 3 4 + 9 - 12 +
Answer: 10
Expression: 9 3 2 1 + + /
Answer: 1.5
ERROR: 3 + 5
is an invalid postfix expression.
Expression: 3 + 5
Answer: 5
ERROR: * 3 4 5 + *
is an invalid postfix expression.
Expression: * 3 4 5 + *
Answer: 27
Expression: 4 9 1 3 + -
Answer: 5
ERROR: h 3 +
is an invalid postfix expression.
Expression: h 3 +
Answer: None
Expression: 0 1 2 3 4 5 6 7 8 9 10 11 12 + / - * + + + + + * * /
Answer: 0.0
预期输出:
Enter the name of the file containing postfix expressions: expressions.txt
Expression: 5 10 * 6 +
Answer: 56
Expression: 20 5 /
Answer: 4.0
Expression: 3 8 6 + *
Answer: 42
Expression: 3 4 + 9 - 12 +
Answer: 10
Expression: 9 3 2 1 + + /
Answer: 1.5
ERROR: 3 + 5 is an invalid postfix expression
ERROR: * 3 4 5 + * is an invalid postfix expression
ERROR: 4 9 1 3 + - is an invalid postfix expression
ERROR: h 3 + is an invalid postfix expression
ERROR: 0 1 2 3 4 5 6 7 8 9 10 11 12 + / - * + + + + + * * / is an invalid postfix expression
【问题讨论】:
-
在尝试从堆栈中弹出任何内容之前查看
each的值,以了解为什么int(each)失败。作为一般规则,不要捕获像Exception这样广泛的内容,除非您至少记录您捕获的实际异常,或计划重新提出。 -
虽然我们在处理它,但您的许多方法过于复杂。而不是
if something: return True else: return False,只是return something。如果你只想检查一个列表是否为非空,你可以只使用列表本身(非空序列为真;空序列为假),而不是检查它的长度。 -
最后一件事:如果
pop打印错误但对空堆栈不执行任何操作,push可能应该打印错误但对于完整堆栈不执行任何操作,而不是默默地将堆栈扩展它的最大尺寸。 (如果你决定使用固定大小的列表和单独的长度设计,推到一个完整的堆栈会引发一个你没有检查的异常;如果你使用不断增长的列表,它将使堆栈处于无效状态违反其不变量的状态。两者都不是您想要的。)
标签: python stack postfix-notation