diff --git a/.gitignore b/.gitignore index bee8a64..8691330 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +*.json diff --git a/expression.py b/expression.py new file mode 100644 index 0000000..1c7a3f6 --- /dev/null +++ b/expression.py @@ -0,0 +1,27 @@ +class Expression: + expression = "" + variables = [] + + def __init__(self, expression_string, variables): + self.expression = expression_string + self.variables = variables + +class Variable: + VARIABLE_TYPES = { + "i" : "Interest", + "I" : "Interest", + "n" : "Time", + "N" : "Time" + } + + def __init__(self, name, type, starting_point = 0, ending_point = 0): + self.name = name + self.type = type + self.starting_point = starting_point + self.ending_point = ending_point + + @staticmethod + def get_variable_type(variable_name): + if not variable_name in Variable.VARIABLE_TYPES: + return "Unknown" + return Variable.VARIABLE_TYPES[variable_name] diff --git a/json_generator/data.json b/json_generator/data.json deleted file mode 100644 index 1166f00..0000000 --- a/json_generator/data.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "expression": "i + 2 + n", - "variables": { - "n1": { - "name": "n1", - "type": "Unknown", - "starting_point": 0, - "ending_point": 0 - }, - "i1": { - "name": "i1", - "type": "Unknown", - "starting_point": 0, - "ending_point": 0 - } - } -} \ No newline at end of file diff --git a/json_generator/expression.py b/json_generator/expression.py index 1c7a3f6..a79ce9f 100644 --- a/json_generator/expression.py +++ b/json_generator/expression.py @@ -1,10 +1,12 @@ class Expression: expression = "" + correct_answer = 0 variables = [] - def __init__(self, expression_string, variables): + def __init__(self, expression_string, variables, correct): self.expression = expression_string self.variables = variables + self.correct_answer = correct class Variable: VARIABLE_TYPES = { diff --git a/json_generator/main.py b/json_generator/main.py index 4419eb0..d8b60ec 100644 --- a/json_generator/main.py +++ b/json_generator/main.py @@ -66,6 +66,11 @@ def transform(object): raise TypeError("Only Books and Index will be JSON serialized!") exp = input("Enter the expression the student will use: ") +correct_answer = input("Enter the correct anwser for the expression: ") +try: + correct_answer = float(correct_answer) +except ValueError: + print("The anwser must be numeric! Will not be included in output file.") tokens = tokenizer.get_tokens_from_expression_string(exp) for t in tokens: @@ -92,5 +97,5 @@ while True: print("Invalid selection %s." %(response)) continue -with open("data.json", "w", encoding="utf-8") as f: - json.dump(expression.Expression(exp, variables), f, default=transform, indent = 4) +with open("expression.json", "w", encoding="utf-8") as f: + json.dump(expression.Expression(exp, variables, correct_answer), f, default=transform, indent = 4) diff --git a/main.py b/main.py index f1b4482..c45cbde 100755 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import token import tokenizer +import expression import calc_engine import json @@ -9,6 +10,10 @@ fileLocation = input('File Path to JSON: ') #userInput = input('Enter your formula: ') tokens = tokenizer.get_tokens_from_expression_string("2 + 6 / 2") +with open("expression.json", "r") as f: + j = json.load(f) + exp = expression.Expression(json.loads(j)) + print(exp) #results = calc_engine.calculate_results(tokens.copy()) #print(results)