How My CLI Calculator Works 12-15-2015, 03:14 PM
#1
Ok, so I recently posted the code for my CLI Calculator in a challenge. It was requested of me to explain the code, so here is an explanation of why it works. 
If you don't understand something, just ask.
This is the code:
There are four function in our code: lex, evalMulDiv, evalAddSub, calc.
The function lex just takes our input string and splits up the operands and operators into an array.
Our calc function is just for organization, it calls all the needed functions to calculate the value of our equation.
There are two separate eval functions because of operator precedence. Operator precedence is just the math rule that you must calculate the multiplication and division of a problem before the addition and subtraction.
You don't have to fully understand the eval functions, but here's how they work. First, in the evalMulDiv function, we loop through all the tokens in our equation... if we find either the * or / operator, we will do that specific math (* or /) on the two operands before and after us. The same is true for our evalAddSub function, except we find the + and - operators.
So, this is how it all works together:

If you don't understand something, just ask.
This is the code:
Code:
import re
def lex(equation):
# remove all the whitespaces from the equation string
equation = equation.replace(" ", "")
return re.findall("[0-9]+|[\+, \-, \*, \/]", equation)
def evalMulDiv(tokens):
i = 0
while i < len(tokens):
if tokens[i] == "*":
tokens = tokens[:i - 1] + [int(tokens[i - 1]) * int(tokens[i + 1])] + tokens[i + 2:]
i -= 2
elif tokens[i] == "/":
tokens = tokens[:i - 1] + [int(tokens[i - 1]) / int(tokens[i + 1])] + tokens[i + 2:]
i -= 2
i += 1
return tokens
def evalAddSub(tokens):
i = 0
while i < len(tokens):
if tokens[i] == "+":
tokens = tokens[:i - 1] + [int(tokens[i - 1]) + int(tokens[i + 1])] + tokens[i + 2:]
i -= 2
elif tokens[i] == "-":
tokens = tokens[:i - 1] + [int(tokens[i - 1]) - int(tokens[i + 1])] + tokens[i + 2:]
i -= 2
i += 1
return tokens
def calc(equation):
tokens = lex(equation)
tokens = evalMulDiv(tokens)
return int(evalAddSub(tokens)[0])
equation = raw_input("> ")
print(calc(equation))There are four function in our code: lex, evalMulDiv, evalAddSub, calc.
The function lex just takes our input string and splits up the operands and operators into an array.
Our calc function is just for organization, it calls all the needed functions to calculate the value of our equation.
There are two separate eval functions because of operator precedence. Operator precedence is just the math rule that you must calculate the multiplication and division of a problem before the addition and subtraction.
You don't have to fully understand the eval functions, but here's how they work. First, in the evalMulDiv function, we loop through all the tokens in our equation... if we find either the * or / operator, we will do that specific math (* or /) on the two operands before and after us. The same is true for our evalAddSub function, except we find the + and - operators.
So, this is how it all works together:
Code:
lex("2+ 3*50") -> ["2", "+", "3", "*", "50"]
evalMulDiv(["2", "+", "3", "*", "50"]) -> ["2", "+", "150"]
evalAddSub(["2", "+", "150"]) -> ["152"]






![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
















(really cool stuff)