From ee9ba528c6319e1a4cee56dab9caf3b2faee6229 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 8 Apr 2020 15:05:34 -0500 Subject: [PATCH] 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. --- calc_engine.py | 36 +++++++++++++++++++++++------------- main.py | 14 +++++++++----- tokenizer.py | 6 +++++- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/calc_engine.py b/calc_engine.py index 06727e1..d234578 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -1,12 +1,11 @@ import token -def calculate_results(tokens): - - return 0 +def calculate_results(tokens): + return handle_add_and_subtract(tokens) def handle_parenthesis(tokens, call_has_priority = False): running_value = 0 - previous_token = CToken("", token.TokenType.unknown) + previous_token = token.CToken("", token.TokenType.unknown) while True: if len(tokens) == 0: @@ -38,7 +37,7 @@ def handle_parenthesis(tokens, call_has_priority = False): if not previous_token.type == token.TokenType.unknown: tokens.append(previous_token) 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) #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: tokens.append(previous_token) 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) #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() 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() two_ahead = tokens.pop() @@ -101,7 +100,7 @@ def handle_parenthesis(tokens, call_has_priority = False): def handle_multiply_and_division(tokens): running_value = 0 - previous_token = CToken("", token.TokenType.unknown) + previous_token = token.CToken("", token.TokenType.unknown) while True: if len(tokens) == 0: @@ -148,9 +147,10 @@ def handle_multiply_and_division(tokens): def handle_add_and_subtract(tokens): running_value = 0 - previous_token = CToken("", token.TokenType.unknown) + previous_token = token.CToken("", token.TokenType.unknown) while True: + if len(tokens) == 0: break @@ -177,7 +177,9 @@ def handle_add_and_subtract(tokens): running_value = operate(previous_token.value, tmp, current_token.type) else: running_value = operate(running_value, tmp, current_token.type) + previous_token.type = token.TokenType.unknown + continue if len(tokens) > 0: two_ahead = peek_list(tokens) @@ -206,7 +208,7 @@ def handle_add_and_subtract(tokens): if previous_token.type != token.TokenType.unknown: tokens.append(previous_token) 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) @@ -222,15 +224,23 @@ def peek_list(tokens): raise IndexError("The token list is empty.", tokens) def operate(n1, n2, tokenType): + n1 = float(n1) + n2 = float(n2) + 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: print("Subtracting %d and %d to get %d." %(n1, n2, n1 - n2)) + return n1 - n2 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: - 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: print("Raising %d to the power of %d to get %d" %(n1, n2, n1**n2)) + return n1 ** n2 else: raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType) diff --git a/main.py b/main.py index 519a70c..3b78c12 100755 --- a/main.py +++ b/main.py @@ -1,8 +1,12 @@ import token import tokenizer -#userInput = input('Enter your formula: ') -tokens = tokenizer.get_tokens_from_expression_string("i + 2 / i (3 * n) 5") +import calc_engine -for t in tokens: - print("%s (%s) " %(t.value, token.TokenType.get_operator_name(t.type)), end = "") -print() +userInput = input('Enter your formula: ') +tokens = tokenizer.get_tokens_from_expression_string(userInput) +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() diff --git a/tokenizer.py b/tokenizer.py index 88385b3..51e1aa6 100644 --- a/tokenizer.py +++ b/tokenizer.py @@ -69,6 +69,10 @@ def get_tokens_from_expression_string(expression_string): tokens.append(token.CToken(c + str(symbols_dic[c]), token.TokenType.variable)) parsing_number = False 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