Skip to content

Instantly share code, notes, and snippets.

@maxkibble
Created June 26, 2018 05:26
Show Gist options
  • Select an option

  • Save maxkibble/1f0b4de51576ae75356c6a61b7aa1544 to your computer and use it in GitHub Desktop.

Select an option

Save maxkibble/1f0b4de51576ae75356c6a61b7aa1544 to your computer and use it in GitHub Desktop.

Revisions

  1. maxkibble created this gist Jun 26, 2018.
    51 changes: 51 additions & 0 deletions calculator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    from operator import add, sub, mul, truediv


    class Calculator(object):
    op = {'+': add, '-': sub, '*': mul, '/': truediv}

    def to_suffix(self, s):
    st = []
    ret = ''
    tokens = s.split()
    for tok in tokens:
    if tok in ['*', '/']:
    while st and st[-1] in ['*', '/']:
    ret += st.pop() + ' '
    st.append(tok)
    elif tok in ['+', '-']:
    while st and st[-1] != '(':
    ret += st.pop() + ' '
    st.append(tok)
    elif tok == '(':
    st.append(tok)
    elif tok == ')':
    while st[-1] != '(':
    ret += st.pop() + ' '
    st.pop()
    else:
    ret += tok + ' '
    while st:
    ret += st.pop() + ' '
    return ret

    def eva(self, s):
    st = []
    tokens = s.split()
    for tok in tokens:
    if tok not in self.op:
    st.append(float(tok))
    else:
    n1 = st.pop()
    n2 = st.pop()
    st.append(self.op[tok](n2, n1))
    return st.pop()

    def evaluate(self, string):
    # print(self.to_suffix(string))
    return self.eva(self.to_suffix(string))


    calc = Calculator()
    # input should split each operator/number with space, can handle float, negative and brackets
    print(calc.evaluate('( 20.0 / 2 ) + ( -3 * ( 1 + 2 ) )'))