152 lines
4.3 KiB
Python
Executable File
152 lines
4.3 KiB
Python
Executable File
import ptoken as token
|
|
import tokenizer
|
|
import expression
|
|
import calc_engine
|
|
import factors
|
|
import jsonpickle
|
|
import itertools
|
|
import copy
|
|
|
|
base_list = [1, 2, 4, 6, 12]
|
|
tolerance = .28
|
|
|
|
variables = {}
|
|
def print_vars(vars):
|
|
i = 0
|
|
for key in vars.keys():
|
|
var = vars[key]
|
|
if i != 0:
|
|
print(", %s" %(var.name), end = "")
|
|
else:
|
|
print("%s " %(var.name), end = "")
|
|
i += 1
|
|
print()
|
|
|
|
def modify_variable(var, vars):
|
|
print("%s is now selected." %(var.name))
|
|
print("The following properties are avaliable: Variable Type, Starting Point and Ending Point.")
|
|
print('Commands are "type", "start" and "end", respectly.')
|
|
|
|
while True:
|
|
response = input("Select a property to modify for %s or q to exit property modifying: " %(var.name))
|
|
|
|
if response == "type":
|
|
response = input("Enter the type of variable this is (currently %s): " %(var.type))
|
|
vars[var.name].type = response
|
|
print("Updated to %s." %(response))
|
|
|
|
elif response == "start":
|
|
response = input("Enter the variable's starting point (currently %f): " %(var.starting_point))
|
|
|
|
try:
|
|
vars[var.name].starting_point = float(response)
|
|
print("Updated to %f." %(float(response)))
|
|
except ValueError:
|
|
print("The starting point must be a number.")
|
|
|
|
elif response == "end":
|
|
response = input("Enter the variable's ending point (currently %f): " %(var.ending_point))
|
|
|
|
try:
|
|
vars[var.name].ending_point = float(response)
|
|
print("Updated to %f." %(float(response)))
|
|
except ValueError:
|
|
print("The ending point must be a number.")
|
|
|
|
pass
|
|
|
|
elif response == "q":
|
|
return
|
|
|
|
else:
|
|
print('Invalid property "%s".' %(response))
|
|
print('Commands are "type", "start" and "end".')
|
|
|
|
exp_str = input("Enter the expression the student will use: ")
|
|
wrong_response = input("Enter the wrong response for the expression: ")
|
|
try:
|
|
wrong_response = float(wrong_response)
|
|
except ValueError:
|
|
print("The anwser must be numeric! Will not be included in output file.")
|
|
tokens = tokenizer.get_tokens_from_expression_string(exp_str)
|
|
rev_tokens = copy.deepcopy(tokens)
|
|
#rev_tokens.reverse()
|
|
|
|
for t in rev_tokens:
|
|
if t.type == token.TokenType.variable:
|
|
variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value[0]))
|
|
|
|
selected_variable = None
|
|
|
|
while True:
|
|
print("Variables in expression:")
|
|
print_vars(variables)
|
|
response = input("Enter a variable name to modify its properties or continue (default): ")
|
|
|
|
if response == "continue" or response == "":
|
|
break
|
|
|
|
if response in variables:
|
|
selected_variable = variables[response]
|
|
|
|
if selected_variable != None:
|
|
modify_variable(selected_variable, variables)
|
|
|
|
else:
|
|
print("Invalid selection %s." %(response))
|
|
continue
|
|
|
|
exp = expression.Expression(exp_str, variables, wrong_response)
|
|
#exp.variables
|
|
#fileLocation = input('File Path to JSON: ')
|
|
#json = json.loads(open(fileLocation).read())
|
|
|
|
|
|
#userInput = input('Enter your formula: ')
|
|
#tokens = tokenizer.get_tokens_from_expression_string("5000(2(2(1.08^10)-1)/0.08)")
|
|
#with open("expression.json", "r") as f:
|
|
# #j = json.load(f)
|
|
# exp = jsonpickle.decode(f.read())
|
|
# exp.variables
|
|
#results = calc_engine.calculate_results(tokens.copy())
|
|
#print(results)
|
|
|
|
#for t in tokens:
|
|
# print("%s (%s) " %(t.value, token.TokenType.get_token_type_name(t.type)), end = "")
|
|
#print()
|
|
|
|
#tokens = tokenizer.get_tokens_from_expression_string(exp.expression)
|
|
tokenizer.print_token_list(tokens)
|
|
factors_list = []
|
|
ls = []
|
|
answers = {}
|
|
|
|
for v in exp.variables:
|
|
factor = factors.Factor(v, base_list)
|
|
|
|
if exp.variables[v].type == "Interest":
|
|
factor.calculate_factors(exp.variables[v].starting_point, True, True)
|
|
else:
|
|
factor.calculate_factors(exp.variables[v].starting_point)
|
|
|
|
factors_list.append(factor)
|
|
ls.append(factor.get_flat_factors_list())
|
|
|
|
res = list(itertools.product(*ls))
|
|
for r in res:
|
|
t = tokenizer.replace_variables(tokens, list(r))
|
|
#tokenizer.print_token_list(t)
|
|
result = calc_engine.calculate_results(t)
|
|
tol = int(exp.wrong_response * tolerance + exp.wrong_response)
|
|
if result <= tol and result >= int(exp.wrong_response - exp.wrong_response * tolerance):
|
|
if not result in answers:
|
|
answers[result] = t
|
|
#if result <= tol and result >= int(exp.wrong_response - exp.wrong_response * tolerance):
|
|
|
|
|
|
for k in answers:
|
|
print("Result: %s for expression: " %(format(k, ".8f")))
|
|
tokenizer.print_token_list(answers[k])
|
|
|
|
|