| | |
Need Help on Reverse Polish Notation program code
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
I am having problems with my reverse polish calculator code. The code is as follows:
The code trips up when it hits an operator for some reason. The line is causing me trouble is the "results = OPERATOR[item] (x, y)"
I would appreaciate any help!!! Please!!!
Thanks
Python Syntax (Toggle Plain Text)
import operator OPERATORS = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } class Stack(object): "Creates a stack to enter numbers or operators into" def __init__(self): self.storage = [0,0,0,0] self.sptn = -1 def push(self, item): self.sptn = self.sptn + 1 self.storage[self.sptn] = item def pop(self): self.sptn = self.sptn -1 def top(self): return self.storage[self.sptn] def __rep__(self): return self.storage[:self.sptn + 1] def get_input(): equation = raw_input("Enter the equation in reverse polish notation: ") return equation def evaluate_rpn(equation): s = Stack() try: for item in equation.split(): try: result = float(item) s.push(result) except ValueError: x = s.pop() y = s.pop() result = OPERATORS[item](x, y) s.push(result) result = s.top() except IndexError: print "Please input at least two numbers before entering an operator." def main(): equation = get_input() answer = evaluate_rpn(raw_input("Enter the equation in reverse polish notation: ")) print answer
The code trips up when it hits an operator for some reason. The line is causing me trouble is the "results = OPERATOR[item] (x, y)"
I would appreaciate any help!!! Please!!!
Thanks
0
#2 23 Days Ago
It's always good to use a few test prints:
The problem is in your class Stack. If you use a Python Queue as a stack it works fine:
Python Syntax (Toggle Plain Text)
import operator OPERATORS = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } class Stack(object): "Creates a stack to enter numbers or operators into" def __init__(self): self.storage = [0,0,0,0] self.sptn = -1 def push(self, item): self.sptn = self.sptn + 1 self.storage[self.sptn] = item def pop(self): self.sptn = self.sptn -1 def top(self): return self.storage[self.sptn] def __rep__(self): return self.storage[:self.sptn + 1] def get_input(): equation = raw_input("Enter the equation in reverse polish notation: ") return equation def evaluate_rpn(equation): s = Stack() try: for item in equation.split(): try: result = float(item) s.push(result) except ValueError: x = s.pop() y = s.pop() print x, y # test --> None None problem is here!!! result = OPERATORS[item](x, y) s.push(result) result = s.top() print result # test return result except IndexError: print "Please input at least two numbers before entering an operator." def main(): # use for testing equation = '2 3 +' #equation = get_input() answer = evaluate_rpn(equation) print answer main()
Python Syntax (Toggle Plain Text)
import operator import Queue OPERATORS = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } def get_input(): equation = raw_input("Enter the equation in reverse polish notation: ") return equation def evaluate_rpn(equation): # use a Python queue for a stack s = Queue.Queue(-1) try: for item in equation.split(): try: result = float(item) s.put(result) except ValueError: x = s.get() y = s.get() #print x, y # test result = OPERATORS[item](x, y) s.put(result) result = s.get() #print result # test return result except IndexError: print "Please input at least two numbers before entering an operator." def main(): # use for testing equation = '2 3 +' # otherwise use ... #equation = get_input() answer = evaluate_rpn(equation) print answer # '2 3 +' --> 5.0 main()
Last edited by sneekula; 23 Days Ago at 2:38 pm. Reason: code
No one died when Clinton lied.
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 23 Days Ago
Ok, thank you for the corrections. The problem is that I have to use a class for this part of the course. I thought I started the positions by s.sptn (start point) = -1 in the __init__(self) function. Any more help with trying to figure out why the class Stack is incorrect would be appreciated!
Thanks,
Linda
Thanks,
Linda
![]() |
Similar Threads
- Reverse Polish Notation (C)
- drawing with c++ (C++)
- Container; Please help me am dying , some can help me to make this program; (C++)
- arranging a statement (C)
- Urgent!!!!!!URGENTTTTT! (Python)
- a question for pascal (Pascal and Delphi)
- leaves and nodes mathematical expression (C++)
- calculator program in C -help needed (C)
- static variable (C)
Other Threads in the Python Forum
- Previous Thread: Dictionaries become empty
- Next Thread: Drawing Circles?
| Thread Tools | Search this Thread |
abrupt accessdenied advanced advice ajax anti arax array backend bash beginner bluetooth book c++ calculator calling code console csv curved cx-freeze dictionary digital dynamic embed examples file function google gui halp homework ideas iframe infosec input itunes java keyboard keycontrol leftmouse library line linux list loan loop microphone mysql mysqldb obexftp output prime programming projects push py2exe pygame pygtk pyopengl pyqt python random read recursive remote ruby script shebang skinning slicenotation smtp software source sprite ssh string sudokusolver syntax table tennis terminal text threading time tkinter tlapse tooltip tutorial ubuntu unicode url urllib urllib2 variable voip whileloop wikipedia write wxpython







