python3 postfix.pyo < postfix.test
This is an individual homework. While you may discuss the ideas and algorithms of this homework with others, at no time should you read the source code or possess the source files of any other person, including people outside this course. We will detect plagiarism using automated tools and will prosecute all violations to the fullest extent of the university regulations, including failing this course, academic probation, and expulsion from the university.
Create a simple postfix calculator.
A postfix calculator reads its input in
postfix notation. You may be familiar with infix notation, where the operator is placed between the operands. (e.g., 1 * 2 + 3
). Postfix notation places the operator after the operands (e.g., 1 2 * 3 +
). Postfix notation removes the need for parentheses or operator priorities, which is an advantage over infix notation. A postfix calculator reads operands and operators from left to right in a postfix expression and uses a stack to evaluate the expression:
Your program reads its input, a postfix expression, from stdin, evaluates the expression, and prints the result to stdout.
The input consists of a sequence of numbers and operators separated by one or more whitespace. Read the input until EOF, as the input may span multiple lines.
0
'...'9
', and can be immediately preceeded by a '-
' for negative integers. +
', subtract '-
', multiply '*
', and divide '/
'(the equivalent of the //
operator in Python).
', tab '\t
', newline '\n
' or carriage return '\r
').We will test your program with only valid postfix expressions, which contain valid numbers, operators, and whitespace defined above. Your prgram need not handle integer overflow or division by 0.
Output the decimal signed integer result of the calculation,
followed by a newline '\n
'. Your output should exactly match the output of the reference program. In other words, given the same input, the output of your program must be identical to that of the reference program. If you run the Linux command diff on the two outputs, diff must report nothing.
However, if you think that the reference program does not behave
as specified, please report the bug.
Your program must not exit with a non-zero status. This means that if your program calls exit()
, the argument must be zero.