2 Replies - 2092 Views - Last Post: 01 June 2011 - 02:32 AM Rate Topic: -----

#1 Mandla Msibi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-April 11

The hardest calculator-postfix notations

Posted 25 May 2011 - 08:13 AM

Guys, I'm trying to make a calculator that can evaluate mathematical expressions in
postfix notation. Operands will be any number, operators are +, −, ∗ (no division). Operators
and operands will always be separated by spaces. A special operand, "answer", is taken to mean the
final answer of the previous expression.
Input will come in the form of several mathematical expressions, each on its own line, terminated
by a blank line. These expressions may be related to each other through the answer operand, but are
otherwise treated as separate expressions. It is possible for an expression to be invalid. For valid
expressions, evaluate the expression and output the final answer. For invalid expressions, output
'Invalid’. If the ans operand refers to an invalid expression, then the expression containing it is
also invalid.
My problem is the "answer" operand and checking Validity of any expression.

the code is:

import operator
#import doctest
expression = raw_input()

class Stack :
	def initial(self):
		self.item = []

	def push(self, item):
		self.item.append(item)

	def pop(self):
	
		return self.item.pop()

	def empty(self):
		return (self.item ==[])


arithmetics = {"+":"add","-":"sub","*":"mul"}

def postfix (expression,stack=Stack(), operators = arithmetics):
	if not isinstance(expression, str):
		return
	for val in expression.split(" "):
		if operators.has_key(val):
			method = getattr(operator, operators.get(val))
			if len(stack.items) < 2:
				return
			first_out_one = stack.pop()
			fisrt_one_two = stack.pop()
			operand = method(fisrt_one_two,fisrt_one_one)
			stack.push(operand)
		else:
			try:
				operand = int(val)
				stack.push(operand)
			except ValueError:
				continue
	return stack.pop()

print postfix(expression,Stack(),arithmetics)]


This post has been edited by baavgai: 25 May 2011 - 08:18 AM
Reason for edit:: tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: The hardest calculator-postfix notations

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,813
  • Joined: 16-October 07

Re: The hardest calculator-postfix notations

Posted 25 May 2011 - 08:33 AM

# there's no conceivable reason to pass stack
# def postfix (expression,stack=Stack(), operators = arithmetics):
def postfix (expression, operators = arithmetics):
	stack = Stack()
	# when, exactly, did you initialize stack?
	# did you call initial?
	
	for val in expression.split():
		if operators.has_key(val):
			# don't need this yet
			# method = getattr(operator, operators.get(val))
			if len(stack.items) < 2:
				# you just return?
				# how do I know if anything went wrong?
				return
			first_out_one = stack.pop()
			fisrt_one_two = stack.pop()
			
			method = getattr(operator, operators.get(val))
			operand = method(fisrt_one_two,fisrt_one_one)
			stack.push(operand)
		else:
			try:
				operand = int(val)
				stack.push(operand)
			except ValueError:
				# you've just blocked the error
				# how do I know it happened
				continue
	return stack.pop()



Your home grown stack is confusing and somewhat wrong. Do you really need it? Do you really need to import the operators lib?

In any case, stop blocking your errors! Decide how to communicate them to the user.

This post has been edited by baavgai: 25 May 2011 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mandla Msibi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-April 11

Re: The hardest calculator-postfix notations

Posted 01 June 2011 - 02:32 AM

Well Ive tried another one for the same problem and it is giving me some errors I don't understand.

class Stack:
	def _init_(self):
		self.items = []

	def push(self,item):
		self.items.append(item)

	def pop(self):
		return self.items.pop()
	
	def isEmpty(self):
		return (self.items ==[])

def evalPostfix(expr):
	#expr = raw_input()
	tokenList = expr.split(" ")
	s= Stack()
	for token in tokenList:
		if token == '' or token ==' ':
			continue
		if token =='+':
			sum = s.pop() + s.pop()
			s.push(sum)
		elif token == '*':
			product = s.pop() * s.pop()
			s.push(product)
		else:
			s.push(int(token))
	return s.pop() 
expr = raw_input() 
evalPostfix(expr)


This post has been edited by baavgai: 01 June 2011 - 04:06 AM
Reason for edit:: tag fix: must be enclosed

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1