private void OperationButton_Click(object sender, RoutedEventArgs e) { var clickedButtonContent = ((Button)sender).Content.ToString(); switch (clickedButtonContent) { case "AC": ResetCalculator(); break; case "%": if (double.TryParse(ResultTextBlock.Text, out _lastNumber)) { _lastNumber = _lastNumber / 100; ResultTextBlock.Text = _lastNumber.ToString(); } break; case "+/-": if (double.TryParse(ResultTextBlock.Text, out _lastNumber)) { _lastNumber = _lastNumber * -1; ResultTextBlock.Text = _lastNumber.ToString(); } break; case ".": PlaceDecimalPoint(); break; case "+": UpdateLastNumber(); _activeOperand = ActiveOperand.Add; break; case "-": UpdateLastNumber(); _activeOperand = ActiveOperand.Subtract; break; case "/": UpdateLastNumber(); _activeOperand = ActiveOperand.Divide; break; case "*": UpdateLastNumber(); _activeOperand = ActiveOperand.Multiply; break; case "=": PerformCalculation(); ResultTextBlock.Text = _result.ToString(); _lastNumber = _result; break; } }
private void ResetCalculator() { _activeOperand = ActiveOperand.None; ResultTextBlock.Text = "0"; }
private void Window_KeyDown(object sender, KeyEventArgs e) { var keyPressed = e.Key; switch (keyPressed) { case Key.D0: UpdateResultTextBlock(0); break; case Key.D1: UpdateResultTextBlock(1); break; case Key.D2: UpdateResultTextBlock(2); break; case Key.D3: UpdateResultTextBlock(3); break; case Key.D4: UpdateResultTextBlock(4); break; case Key.D5: UpdateResultTextBlock(5); break; case Key.D6: UpdateResultTextBlock(6); break; case Key.D7: UpdateResultTextBlock(7); break; case Key.D8: UpdateResultTextBlock(8); break; case Key.D9: UpdateResultTextBlock(9); break; case Key.OemPeriod: PlaceDecimalPoint(); break; case Key.OemPlus: UpdateLastNumber(); _activeOperand = ActiveOperand.Add; break; case Key.OemMinus: UpdateLastNumber(); _activeOperand = ActiveOperand.Subtract; break; case Key.Multiply: UpdateLastNumber(); _activeOperand = ActiveOperand.Multiply; break; case Key.Divide: UpdateLastNumber(); _activeOperand = ActiveOperand.Divide; break; case Key.Back: ResultTextBlock.Text = ResultTextBlock.Text.Substring(0, ResultTextBlock.Text.Length - 1); break; case Key.Delete: ResetCalculator(); break; case Key.Escape: Close(); break; case Key.Enter: PerformCalculation(); ResultTextBlock.Text = _result.ToString(); _lastNumber = _result; break; default: break; } }