def evaluate_expression(self): exp_str = None # If invalid input, keep prompting for input while exp_str is None: exp_str = input( '\nPlease enter the expression you want to evaluate:\n') try: exp_str = exp_str.replace(' ', '') expression = Expression(exp_str) expression.parse_tree() except Exception as e: exp_str = None print('Invalid Expression, ' + str(e)) # checking for print order orderprint_selection = self.__print_order_selection() if orderprint_selection == "1": expression.print_preorder() elif orderprint_selection == "2": expression.print_inorder() elif orderprint_selection == "3": expression.print_postorder() # writes into a history file with open('./history.txt', 'a') as history_file: history_file.write("Expression {} evaluates to: {:.3f}\n".format( str(expression), expression.val)) with open('./input/input_history.txt', 'a') as history_file: history_file.write('\n' + str(expression)) # printing print("\nExpression evaluates to:\n{:.3f}".format(expression.val))
def __parsefile(self, input_file, sort_method, ascending_check, sort_by_value): # checks for sorting method and creates object accordingly if sort_method == "1": exp_list = SortedList(ascending_check) elif sort_method == "2": exp_list = [] for i in range(len(input_file)): try: input_file[i].replace(' ', '') expression = Expression(input_file[i]) expression.parse_tree() expression.set_sort_value(sort_by_value) except Exception as e: print("\nInvalid expression at line " + str(i + 1) + ". Skipping expression") print(e) continue if sort_method == "1": exp_list.insert(expression) elif sort_method == "2": exp_list.append(expression) if sort_method == "2": sort_expressions(exp_list, ascending_check) return exp_list