Fixed a bug in the tokenizer to make sure it reverses the list before returning. Fixed up the calc engine, however addition/subtraction bugs still exist.
This commit is contained in:
+23
-13
@@ -1,12 +1,11 @@
|
|||||||
import token
|
import token
|
||||||
|
|
||||||
def calculate_results(tokens):
|
def calculate_results(tokens):
|
||||||
|
return handle_add_and_subtract(tokens)
|
||||||
return 0
|
|
||||||
|
|
||||||
def handle_parenthesis(tokens, call_has_priority = False):
|
def handle_parenthesis(tokens, call_has_priority = False):
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = CToken("", token.TokenType.unknown)
|
previous_token = token.CToken("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
@@ -38,7 +37,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
if not previous_token.type == token.TokenType.unknown:
|
if not previous_token.type == token.TokenType.unknown:
|
||||||
tokens.append(previous_token)
|
tokens.append(previous_token)
|
||||||
else:
|
else:
|
||||||
tokens.append(CToken(running_value, token.TokenType.constant))
|
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
||||||
|
|
||||||
running_value = handle_add_and_subtract(tokens)
|
running_value = handle_add_and_subtract(tokens)
|
||||||
#Set the previous_token to an invalid state so its not used by mistake.
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
@@ -74,7 +73,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
if not previous_token.type == token.TokenType.unknown:
|
if not previous_token.type == token.TokenType.unknown:
|
||||||
tokens.append(previous_token)
|
tokens.append(previous_token)
|
||||||
else:
|
else:
|
||||||
tokens.append(CToken(running_value, token.TokenType.constant))
|
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
||||||
|
|
||||||
running_value = handle_multiply_and_division(tokens)
|
running_value = handle_multiply_and_division(tokens)
|
||||||
#Set the previous_token to an invalid state so its not used by mistake.
|
#Set the previous_token to an invalid state so its not used by mistake.
|
||||||
@@ -86,7 +85,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
tokens.pop()
|
tokens.pop()
|
||||||
running_value = operate(running_value, tokens.pop().value, token.TokenType.power)
|
running_value = operate(running_value, tokens.pop().value, token.TokenType.power)
|
||||||
|
|
||||||
if !caller_has_priority and len(tokens) > 0 and (peek_list(tokens).type == token.TokenType.multiply or peek_list(tokens).type == token.TokenType.divide):
|
if not call_has_priority and len(tokens) > 0 and (peek_list(tokens).type == token.TokenType.multiply or peek_list(tokens).type == token.TokenType.divide):
|
||||||
operator = tokens.pop()
|
operator = tokens.pop()
|
||||||
two_ahead = tokens.pop()
|
two_ahead = tokens.pop()
|
||||||
|
|
||||||
@@ -101,7 +100,7 @@ def handle_parenthesis(tokens, call_has_priority = False):
|
|||||||
|
|
||||||
def handle_multiply_and_division(tokens):
|
def handle_multiply_and_division(tokens):
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = CToken("", token.TokenType.unknown)
|
previous_token = token.CToken("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
@@ -148,9 +147,10 @@ def handle_multiply_and_division(tokens):
|
|||||||
|
|
||||||
def handle_add_and_subtract(tokens):
|
def handle_add_and_subtract(tokens):
|
||||||
running_value = 0
|
running_value = 0
|
||||||
previous_token = CToken("", token.TokenType.unknown)
|
previous_token = token.CToken("", token.TokenType.unknown)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -177,7 +177,9 @@ def handle_add_and_subtract(tokens):
|
|||||||
running_value = operate(previous_token.value, tmp, current_token.type)
|
running_value = operate(previous_token.value, tmp, current_token.type)
|
||||||
else:
|
else:
|
||||||
running_value = operate(running_value, tmp, current_token.type)
|
running_value = operate(running_value, tmp, current_token.type)
|
||||||
|
|
||||||
previous_token.type = token.TokenType.unknown
|
previous_token.type = token.TokenType.unknown
|
||||||
|
continue
|
||||||
|
|
||||||
if len(tokens) > 0:
|
if len(tokens) > 0:
|
||||||
two_ahead = peek_list(tokens)
|
two_ahead = peek_list(tokens)
|
||||||
@@ -206,7 +208,7 @@ def handle_add_and_subtract(tokens):
|
|||||||
if previous_token.type != token.TokenType.unknown:
|
if previous_token.type != token.TokenType.unknown:
|
||||||
tokens.append(previous_token)
|
tokens.append(previous_token)
|
||||||
else:
|
else:
|
||||||
tokens.append(CToken(running_value, token.TokenType.constant))
|
tokens.append(token.CToken(running_value, token.TokenType.constant))
|
||||||
|
|
||||||
running_value = handle_multiply_and_division(tokens)
|
running_value = handle_multiply_and_division(tokens)
|
||||||
|
|
||||||
@@ -222,15 +224,23 @@ def peek_list(tokens):
|
|||||||
raise IndexError("The token list is empty.", tokens)
|
raise IndexError("The token list is empty.", tokens)
|
||||||
|
|
||||||
def operate(n1, n2, tokenType):
|
def operate(n1, n2, tokenType):
|
||||||
|
n1 = float(n1)
|
||||||
|
n2 = float(n2)
|
||||||
|
|
||||||
if tokenType == token.TokenType.add:
|
if tokenType == token.TokenType.add:
|
||||||
print("Adding %d and %d to get %d." %(n1, n2, n1 + n2))
|
print("Adding %d and %d to get %d." %(n1, n2, n1 + n2))
|
||||||
|
return n1 + n2
|
||||||
elif tokenType == token.TokenType.subtract:
|
elif tokenType == token.TokenType.subtract:
|
||||||
print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2))
|
print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2))
|
||||||
|
return n1 - n2
|
||||||
elif tokenType == token.TokenType.multiply:
|
elif tokenType == token.TokenType.multiply:
|
||||||
print("Multiplying %d and %d to get %d" %(n1, n2, n1 * n2))
|
print("Multiplying %d and %d to get %d" %(n1, n2, n1 * n2))
|
||||||
|
return n1 * n2
|
||||||
elif tokenType == token.TokenType.divide:
|
elif tokenType == token.TokenType.divide:
|
||||||
print("Dividing %d and %d to get %d" %(n1, n2, n1 / n2))
|
print("Dividing %d and %d to get %d" %(n1, n2, n1 / n2))
|
||||||
|
return n1 / n2
|
||||||
elif tokenType == token.TokenType.power:
|
elif tokenType == token.TokenType.power:
|
||||||
print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2))
|
print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2))
|
||||||
|
return n1 ** n2
|
||||||
else:
|
else:
|
||||||
raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType)
|
raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType)
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import token
|
import token
|
||||||
import tokenizer
|
import tokenizer
|
||||||
#userInput = input('Enter your formula: ')
|
import calc_engine
|
||||||
tokens = tokenizer.get_tokens_from_expression_string("i + 2 / i (3 * n) 5")
|
|
||||||
|
|
||||||
for t in tokens:
|
userInput = input('Enter your formula: ')
|
||||||
print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "")
|
tokens = tokenizer.get_tokens_from_expression_string(userInput)
|
||||||
print()
|
results = calc_engine.calculate_results(tokens)
|
||||||
|
print(results)
|
||||||
|
|
||||||
|
#for t in tokens:
|
||||||
|
# print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "")
|
||||||
|
#print()
|
||||||
|
|||||||
+5
-1
@@ -69,6 +69,10 @@ def get_tokens_from_expression_string(expression_string):
|
|||||||
tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable))
|
tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable))
|
||||||
parsing_number = False
|
parsing_number = False
|
||||||
tmp = ""
|
tmp = ""
|
||||||
|
#When building this list we've effectively ordered the tokens in reverse order from how we want to
|
||||||
|
#process them. So, reverse the order of the list before returning them to the caller, we don't want
|
||||||
|
#the caller to have to worry about such a minor detail.
|
||||||
|
tokens.reverse()
|
||||||
|
|
||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user