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.
This commit is contained in:
+5
-5
@@ -244,19 +244,19 @@ def operate(n1, n2, tokenType):
|
|||||||
n2 = float(n2)
|
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
|
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
|
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
|
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
|
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
|
return n1 ** n2
|
||||||
else:
|
else:
|
||||||
raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType)
|
raise TypeError("Invalid operator value " + str(tokenType) + ".", tokenType)
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class Variable:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_variable_type(variable_name):
|
def get_variable_type(variable_name):
|
||||||
|
print(variable_name[0])
|
||||||
if len(variable_name) == 0:
|
if len(variable_name) == 0:
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
if not variable_name[0] in Variable.VARIABLE_TYPES:
|
if not variable_name[0] in Variable.VARIABLE_TYPES:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import tokenizer
|
|||||||
import ctoken
|
import ctoken
|
||||||
import expression
|
import expression
|
||||||
import jsonpickle
|
import jsonpickle
|
||||||
|
import copy
|
||||||
|
|
||||||
variables = {}
|
variables = {}
|
||||||
def print_vars(vars):
|
def print_vars(vars):
|
||||||
@@ -62,8 +63,10 @@ try:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
print("The anwser must be numeric! Will not be included in output file.")
|
print("The anwser must be numeric! Will not be included in output file.")
|
||||||
tokens = tokenizer.get_tokens_from_expression_string(exp)
|
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:
|
if t.type == ctoken.TokenType.variable:
|
||||||
variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value))
|
variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value))
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import factors
|
|||||||
import jsonpickle
|
import jsonpickle
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
base_list = [2, 4, 6, 12]
|
base_list = [1, 2, 4, 6, 12]
|
||||||
|
|
||||||
#fileLocation = input('File Path to JSON: ')
|
#fileLocation = input('File Path to JSON: ')
|
||||||
#json = json.loads(open(fileLocation).read())
|
#json = json.loads(open(fileLocation).read())
|
||||||
@@ -45,4 +45,8 @@ res = list(itertools.product(*ls))
|
|||||||
for r in res:
|
for r in res:
|
||||||
t = tokenizer.replace_variables(tokens, list(r))
|
t = tokenizer.replace_variables(tokens, list(r))
|
||||||
#tokenizer.print_token_list(t)
|
#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)
|
||||||
|
|||||||
Reference in New Issue
Block a user