我正在创建一个后缀计算器,它接受算术表达式并首先将运算符推送到stacl。你知道吗
./pythonfilename 3 4 1+-用作我的输入。但是,由于没有显示任何输出,我尝试调试我的程序,以了解为什么我的程序不接受我的参数。 不会导致打印任何输出。我按Ctrl+C显示回溯调用,它指出x=sys.stdin.readlines系统标准. 你知道吗
#!/usr/bin/python
import sys
import fileinput
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items(pop)
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def is_number(line):
try:
float(line)
except ValueError:
return False
def infixtoPostfix():
initStack=Stack()
x = sys.stdin.readlines() #read user input
for lines in x:#for lines in fileinput.input():
for line in lines.strip().split(" "):
if is_number(line):
initStack.push(line)
line = float(line)
elif line =='+':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum + secNum
initStack.push(result)
print initStack.peek()
elif line == '-':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum - secNum
initStack.push(result)
print initStack.peek()
elif line == '*':
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum * secNum
initStack.push(result)
print initStack.peek()
elif line == "/":
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum / secNum
initStack.push(result)
print initStack.peek()
elif line == "%":
firstNum = initStack.pop()
secNum = initStack.pop()
result = firstNum % secNum
initStack.push(result)
print initStack.peek()
infixtoPostfix()
读取管道(
cat ... | python myprog.py
)的标准方法是line
这里将包括最后的'\n'
如果您想在命令行(
python myprog.py 3 4 1 + -
)上获取参数,您可以使用sys.argv[1:]
(sys.argv[0]
包含myprog.py
)。你知道吗要获得输入的一致词法,首先需要检查
sys.argv
,然后拆分sys.stdin
:然后您只需要更改
infixPostfix()
函数来使用这个令牌数组(而不是在同一个函数中同时进行解析和求值)。你知道吗附言:一个更简洁的方式写的个别条款将是:
但这取决于你想完成什么。。你知道吗
更新:完整解决方案
更新2:带有调试语句以可视化堆栈(为了简洁起见,删除了堆栈类而使用常规列表)
测试:
(
cat rpn.txt | python rpn.py
将输出相同的东西,如果rpn.txt
包含3 4 1 + -
)。你知道吗如果您尝试带有语法错误的rpn程序,则该程序将引发异常,例如:
在一个真正的编译器中,这是不好的,因为你不想让最终用户看到你实现的细节。相反,您应该给他们一个诊断错误消息,以及您的程序找到它的确切位置。你知道吗
在这种情况下,这并不难。我省略了用于打印堆栈的调试语句:
需要对结果打印进行小的更改,打印列表中的最后一个元素(
STACK[-1]
),它是堆栈的顶部,而不是依赖列表/堆栈的末尾只有一个元素:如果我们给这个版本的程序提供语法错误:
我们得到了一个正确的错误消息,带有一个小的尖尖的“图形”指示错误被检测到的位置。你知道吗
我们可以更进一步,因为我们知道所有的操作都在堆栈上包含两个元素,并给出更详细的错误消息,例如:
我将把它的实现作为一个练习。你知道吗
如您所见,即使在这个简单的示例中,错误处理代码也比求值代码多,这在编写简单的编译器时并不奇怪。你知道吗
./pythonfilename 3 4 1+-
实际上,3 4 1+-是作为参数传递的,而不是作为输入传递的。你知道吗
如果要读取文件,请使用open('filename')
使用./pythonfilename'3 4 1+-'
x=[系统argv[1] ]
代替x=sys.stdin.readlines系统标准()
但是,您的代码只处理单个输入作为参数
相关问题 更多 >
编程相关推荐