RPN Calculator – Keeping Boredom Away

Alright, those who know me well are bound to ask, 19 credit hours and you are bored? Answer: Yes. It is still the first week and we don’t seem to have gotten beyond the “introduce your loser self to the other non-interested members of the class” and since I have only 1 programming intensive course this semester, I should have lots of time to kill.

Well, my first script this semester is an RPN calculator. Totally useless for the vast majority of people out there but they know what to expect from someone who likes theoretical computer science.

So, I first began with a stack – in this case an array. I then keep populating it with numbers and every time the state of the stack changes, it prints out the current state.

If an operation is entered (only +, -, * and /), it performs the operation, replaces the appropriate positions in the stack and then prints out the current state.

Here is the code, Its been a while since I wrote any OOP stuff in Python and the “self” especially feels weird after an entire semester of Java…

#!/usr/bin/env python
#Author: Shriphani Palakodety
#Mail: shriphani@shriphani.com
#RPN Calculator

class Machine:
        def __init__(self, stack=[]):
                self.stack = []

        def modifyStack(self, element):
                ‘add to stack if input is float else run operation or complain’

                if self.isOperator(element):
                        self.operate(element)
                        self.printStack()
                elif self.isFloat(element):
                        self.stack.append(element)
                        self.printStack()

                else:
                        print "Only numbers and operators"

        def isFloat(self, char):
                ‘Checks whether the last item entered in the stack is a float or not’
                try:
                        float(char)
                except ValueError:
                        return False
                return True

        def isOperator(self, char):
                ‘Checks whether the last item entered in the stack is an operator or not’
                if char == ‘+’ or char == ‘-’ or char == ‘*’ or char == ‘/’:
                        return True
                else:
                        return False

        def operate(self, operator):
                ‘Perform the stated operation on the last two elements in the stack’
               
                operand1 = float(self.stack[-1])
                operand2 = float(self.stack[-2])

                self.stack.pop(-1)
                self.stack.pop(-1)

                if operator==‘+’:
                        self.stack.append(operand1+operand2)

                elif operator==‘-’:
                        self.stack.append(operand2-operand1)

                elif operator==‘*’:
                        self.stack.append(operand1*operand2)

                elif operator==‘/’:
                        self.stack.append(float(operand2)/float(operand1))

                else:
                        print "God only knows what operation you have typed in"
                        self.stack += [operand2, operand1]
                        self.printStack()

        def printStack(self):
                print self.stack

       
def kickOff():
        calc = Machine()
        while True:
                a = raw_input(‘Input: ‘)
                if a != :
                        calc.modifyStack(a)
                else:
                        calc.printStack()
                        raise SystemExit

print"Press Enter to Quit\n"
kickOff()
 

The calculator in action:

pal-179-083:~ shriphani$ python rpn.py
Press Enter to Quit

Input: 1
['1']
Input: 2
['1', '2']
Input: 3
['1', '2', '3']
Input: 4
['1', '2', '3', '4']
Input: 5
['1', '2', '3', '4', '5']
Input: +
['1', '2', '3', 9.0]
Input: h
Only numbers and operators
Input: -
['1', '2', -6.0]
Input:
['1', '2', -6.0]

In other news, I am really pissed with Picasa since I can’t see how many people have viewed my pics (http://picasaweb.google.com/shriphanip in case you are interested), I have begun filling my playlists with songs played at apple keynotes and in apple ads. Friends say this is an obsession with everything with the Apple stamp on it. I basically don’t care.

There has always been this one question bugging me since last semester. In Java:

 class Test
{
        public static void main(String[] args)
        {
                int a = 012345;
                System.out.println(a);
        }
}
 

When I run this:

pal-179-083:~ shriphani$ java Test
5349
pal-179-083:~ shriphani$

What is happening here?

Anyway, looking forward to this semester. Hope my GPA stays at 4.0


About this entry