def calculate(self, first_number, second_number): """ This function calculate -second_number :param first_number: The number to the left of the operator(automatically zero) :param second_number:The number to the right of the operator :return:CalculationResult(object) that hold whether or not the calculation was successful(if it was unsuccessful it will hold the error code) and the calculation result """ # create the CalculationResult calculation_data = CalculationResult() try: # try do the calculation calculation_data.result = -second_number except OverflowError: # if get to here the number is too big meaning # the equation is invalid, save the corresponding error # and changes the calculation_data.is_successful to false # (flag that indicate if the calculation was successful # or not) calculation_data.is_successful = False calculation_data.invalid_equation_code = \ InvalidEquationCode.NUMBER_TOO_BIG # general check on the result (check if number is inf ,-inf or # complex) is_valid_result(calculation_data) return calculation_data
def calculate(self, first_number, second_number): # create the CalculationResult calculation_data = CalculationResult() # try do the calculation calculation_data.result = \ first_number if first_number > second_number else second_number return calculation_data
def solve_equation(self, metadata): """ This function solve the equation :param metadata:Hold information about the equation :return:CalculationResult(object) that hold whether or not the calculation was successful(if it was unsuccessful it will hold the error code) and the calculation result """ # convert the equation from infix expression to postfix postfix_list = self._prepare_list_before_solve(metadata) # check if the equation is only one number # if true create CalculationResult is the number as the result # and return the CalculationResult if len(postfix_list) == 1: calculation_result = CalculationResult() calculation_result.result = postfix_list[0] return calculation_result # stack that hold the operands operands_stack = [] # scanning all the equation for current_char in postfix_list: # if current char is number push to the stack if isinstance(current_char, float): operands_stack.append(current_char) # else current_char is operator else: # pop the last two numbers from the operands stack second_number = operands_stack.pop() first_number = operands_stack.pop() # calculate the expression by calling the calculate # function of the operator instance from the # OPERATORS_DICTIONARY calculation_result = metadata. \ OPERATORS_DICTIONARY[current_char] \ .calculate(first_number, second_number) # check if the calculation was successful # if true push the calculation result to operands stack # else return the calculation_result that hold the # calculation error if calculation_result.is_successful: operands_stack.append(calculation_result.result) else: return calculation_result return calculation_result
def calculate(self, first_number, second_number): # create the CalculationResult calculation_data = CalculationResult() try: # try do the calculation calculation_data.result = (first_number + second_number) / 2 except OverflowError: # if get to here the number is too big meaning # the equation is invalid, save the corresponding error # and changes the calculation_data.is_successful to false # (flag that indicate if the calculation was successful # or not) calculation_data.is_successful = False calculation_data.invalid_equation_code = \ InvalidEquationCode.NUMBER_TOO_BIG # general check on the result (check if number is inf ,-inf or # complex) is_valid_result(calculation_data) return calculation_data