From 7507db75b3e4b40edcb7c86580aa1496926f7cd8 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 20 Apr 2020 00:49:32 -0500 Subject: [PATCH] Tested the Factor Engine, first round of testing passed. Fixed a bug in the JSON_Generator to stop expression variables from being added in reverse. --- calc_engine.py | 10 +++++----- json_generator/expression.py | 1 + json_generator/main.py | 5 ++++- main.py | 8 ++++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/calc_engine.py b/calc_engine.py index 771c2a8..54c4e06 100644 --- a/calc_engine.py +++ b/calc_engine.py @@ -244,19 +244,19 @@ def operate(n1, n2, tokenType): 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)) + #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)) + #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/json_generator/expression.py b/json_generator/expression.py index 13365a4..4188319 100644 --- a/json_generator/expression.py +++ b/json_generator/expression.py @@ -30,6 +30,7 @@ class Variable: @staticmethod def get_variable_type(variable_name): + print(variable_name[0]) if len(variable_name) == 0: return "Unknown" if not variable_name[0] in Variable.VARIABLE_TYPES: diff --git a/json_generator/main.py b/json_generator/main.py index 22a438f..3602d15 100644 --- a/json_generator/main.py +++ b/json_generator/main.py @@ -2,6 +2,7 @@ import tokenizer import ctoken import expression import jsonpickle +import copy variables = {} def print_vars(vars): @@ -62,8 +63,10 @@ try: except ValueError: print("The anwser must be numeric! Will not be included in output file.") tokens = tokenizer.get_tokens_from_expression_string(exp) +rev_tokens = copy.deepcopy(tokens) +rev_tokens.reverse() -for t in tokens: +for t in rev_tokens: if t.type == ctoken.TokenType.variable: variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value)) diff --git a/main.py b/main.py index c4c51ff..fa55aa4 100755 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ import factors import jsonpickle import itertools -base_list = [2, 4, 6, 12] +base_list = [1, 2, 4, 6, 12] #fileLocation = input('File Path to JSON: ') #json = json.loads(open(fileLocation).read()) @@ -45,4 +45,8 @@ res = list(itertools.product(*ls)) for r in res: t = tokenizer.replace_variables(tokens, list(r)) #tokenizer.print_token_list(t) - print("Results %f" %(calc_engine.calculate_results(t))) + result = calc_engine.calculate_results(t) + tol = (exp.wrong_response * .01 + exp.wrong_response) + if result <= tol and result >= (exp.wrong_response - exp.wrong_response * .01): + print("Result: %s for expression: " %(format(result, ".8f"))) + tokenizer.print_token_list(t)