Created
June 26, 2018 05:26
-
-
Save maxkibble/1f0b4de51576ae75356c6a61b7aa1544 to your computer and use it in GitHub Desktop.
python implementation of calculating an infix expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ) )')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful.