Due: by midnight Monday, October 8, 2012.
For this program, you can work with a single partner. Be sure to specify in your listing who worked on it. If you've been having a lot of trouble, try to find someone to work with who has not been having trouble. But make sure that you understand what your group is doing.
Your program listing should have the following information.
# Files: Stack.py, Calculator.py # # Description: # # Student's Name(s): # # Student's UT EID: # # Course Name: CS 313E # # Date Created: # # Date Last Modified:
The mathematical notation that we use is called infix, because the operator comes between the arguments. It is actually much less convenient for machine processing than several other possible notations because you need precedence rules and parenthesis to make it unambiguous. Nevertheless, it's what we're stuck with.
Your assignment is to write a simple calculator that prompts the user for input, inputs an expression, evaluates and prints the value of the expression (or an error message if the expression is ill-formed). Your calculator should take input either in infix notation or in postfix notation. The default should be infix, but allow the user to input strings "infix" or "postfix" to toggle between the two. Exit the program when the user types "stop".
Note that this is not directly related to the Calc class we defined earlier, though you may adopt ideas from that if you like. On the other hand, that may just confuse you, so feel free to ignore that in doing this assignment.
Input may contain positive integer constants and the following operators: + (plus), - (minus), * (times), and parentheses (for infix). Use spaces to separate tokens in your input. That is, you should input "( 12 + 7 ) * 3" rather than "(12+7)*3". This allows your numeric constants to be more than a single character long.
Coding this will involve the following steps:
felix:~/cs313e/python> python Calculator.py This is a simple calculator. Enter expressions in infix (default) or postfix notation. Enter 'infix' or 'postfix' to toggle the mode. Enter 'stop' to exit. Tokens in your expression must be separated by spaces. Expressions can involve integers and any of +, *, -. Infix expressions can use parentheses to disambiguate. Enter an infix expression: 3 + 2 * 4 The answer is: 11 Enter an infix expression: ( 3 + 2 ) * 4 The answer is: 20 Enter an infix expression: postfix Enter a postfix expression: 3 2 4 * + The answer is: 11 Enter a postfix expression: 3 2 + 4 * The answer is: 20 Enter a postfix expression: 3 + Ill-formed expression Enter a postfix expression: infix Enter an infix expression: 3 + Ill-formed expression Enter an infix expression: a + b Ill-formed expression bad token a Enter an infix expression: ( 3 + 4 ) The answer is: 7 Enter an infix expression: stop felix:~/cs313e/python>