Writing a calculator using stacks in Python

I wrote a calculator in Python. Again. But this time, I didn't write the UI layer. Because principles! Here at Enthought, it's advised to have a clear seperation between the Model and the View and what you see below is the Model part of the calculator. It also makes life easier in two ways. First, it makes testing the underlying model and it's methods easier. Secondly, I can implement the UI however I want to, i.e using traitsUI, raw Qt, Jigna. Whatever!

If you don't understand what's going on in the code, let me explain things a little. There is a standard Python list called inputstack, and it uses as a stack, that stores all of the input characters, let them be numbers or an operator. And the eval_stack method will traverse the inputstack checking for numbers or operators.

Also, this Github repository contains all the calculator codes I've written so far! Again, as always, the code was highlighted and embedded into this blogpost as raw html using hilite.me! Try it! It's awesome!


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import unittest

class Application():

    inputstack = []

    def eval_stack(self):
        i = 0           # counter to keep track of decimal point
        temp = 0        # counter to keep track of current input
        temp2 = 0       # counter to store previous input
        op = ''         # used to store the math operator
        while self.inputstack != []:
           if not isinstance(self.inputstack[-1], basestring):
               temp += int(self.inputstack.pop())*10**i
               i += 1
               print temp
           else :
               if self.inputstack[-1] == '.':
                   self.inputstack.pop()
                   temp = temp/(10.**(i))
                   i = 0
                   print temp
               else :
                   op = self.inputstack.pop()
                   temp2 = temp
                   temp = 0
                   i = 0
                   print op, temp, temp2

        if op == '+':
            return temp2 + temp
        elif op == '-':
            return temp - temp2
        elif op == '*':
            return temp*temp2
        elif op == '/':
            return float(temp)/temp2

class testApplication(unittest.TestCase):

    def test_add(self):
        test = Application()
        test.inputstack = [1,2,'+',3,4]
        self.assertEqual(test.eval_stack(), 46)

    def test_add_float(self):
        test = Application()
        test.inputstack = [1,2,'.',4,'+',3,4,'.',5]
        self.assertEqual(test.eval_stack(), 46.9) 
    
    def test_sub(self):
        test = Application()
        test.inputstack = [1,2,'-',3,4]
        self.assertEqual(test.eval_stack(), -22)

    def test_sub_float(self):
        test = Application()
        test.inputstack = [1,2,'.',4,'-',3,4,'.',5]
        self.assertEqual(test.eval_stack(), -22.1)
    
    def test_mul(self):
        test = Application()
        test.inputstack = [1,2,'*',4]
        self.assertEqual(test.eval_stack(), 48)

    def test_mul_float(self):
        test = Application()
        test.inputstack = [1,'.',4,'*',3,'.',5]
        self.assertEqual(test.eval_stack(), 4.9)
    
    def test_div(self):
        test = Application()
        test.inputstack = [1,2,'/',4]
        self.assertEqual(test.eval_stack(), 3.0)

    def test_div_float(self):
        test = Application()
        test.inputstack = [1,'/',5]
        self.assertEqual(test.eval_stack(), 0.2)

Popular posts from this blog

Animation using GNUPlot

Arxiv author affiliations using Python

Thoughts on the National Deep Tech Startup Policy