private void Equals_Handle(object sender, RoutedEventArgs e) { if (_selectedOperator == SelectedOperator.Addition) { result = MathService.Addition(firstNumber, lastNumber); DisplayResult.Text = result.ToString(); } else if (_selectedOperator == SelectedOperator.Subtraction) { result = MathService.Subtract(lastNumber, firstNumber); DisplayResult.Text = result.ToString(); } else if (_selectedOperator == SelectedOperator.Division) { result = MathService.Divide(lastNumber, firstNumber); DisplayResult.Text = result.ToString(); if (firstNumber == 0) { MessageBox.Show("Cannot divide by 0", "Error", MessageBoxButton.OK, MessageBoxImage.Error); DisplayResult.Text = "0"; firstNumber = 0; lastNumber = 0; _selectedOperator = null; } } else if (_selectedOperator == SelectedOperator.Multiplication) { result = MathService.Multiply(lastNumber, firstNumber); DisplayResult.Text = result.ToString(); } lastNumber = result; _selectedOperator = null; }
private double?DoOperation(SelectedOperator?so, double o, double o1) { switch (so) { case SelectedOperator.Addition: return(MathService.Add(o, o1)); case SelectedOperator.Subtraction: return(MathService.Subtract(o, o1)); case SelectedOperator.Multiplication: return(MathService.Multiply(o, o1)); case SelectedOperator.Division: return(MathService.Divide(o, o1)); default: return(null); } }
private void Equal_Click(object sender, RoutedEventArgs e) { double number; if (double.TryParse(Result.Content.ToString(), out number)) { switch (selectedOperator) { case OperatorOptions.Division: try { if (Result.Content.ToString() == "0") { throw new DivideByZeroException("Can't divide by 0"); } result = MathService.Divide(lastNumber, number); } catch (DivideByZeroException ex) { MessageBox.Show("Connot divide by 0", "Error!"); lastNumber = 0; } break; case OperatorOptions.Multiplication: result = MathService.Multiply(lastNumber, number); break; case OperatorOptions.Addition: result = MathService.Add(lastNumber, number); break; case OperatorOptions.Subtraction: result = MathService.Subtract(lastNumber, number); break; } Result.Content = result; } }