Ejemplo n.º 1
0
        private void Button_equal_Click(object sender, RoutedEventArgs e)
        {
            OperationsHistory.Text = Output.Text + "=";
            Output.Text            = CalculatorClass.Calculate(Output.Text).ToString();

            equalPressed = true;
        }
Ejemplo n.º 2
0
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                OperationsHistory.Text = Output.Text + "=";
                Output.Text            = CalculatorClass.Calculate(Output.Text).ToString();

                equalPressed = true;
            }

            if (e.Key == Key.Back)
            {
                if (Output.Text.Length > 0)
                {
                    Output.Text = Output.Text.Substring(0, Output.Text.Length - 1);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string content = (string)(sender is Button ? ((Button)sender).Content : sender);

            switch (content)
            {
            case "+": calculationTypeChosen = CalculationType.Add; break;

            case "-": calculationTypeChosen = CalculationType.Subtract; break;

            case "/": calculationTypeChosen = CalculationType.Divide; break;

            case "x": calculationTypeChosen = CalculationType.Multiply; break;

            case "c":
                inputs = new string[2];
                calculationTypeChosen = null;
                showingResult         = false;
                InputDisplay.Content  = "";
                break;

            case "=":
                if (calculationTypeChosen == null)
                {
                    return;
                }
                InputDisplay.Content = CalculatorClass.Calculate(Convert.ToDouble(inputs[0], cultureInfo), Convert.ToDouble(inputs[1], cultureInfo), calculationTypeChosen);
                showingResult        = true;
                break;

            default:
                if (showingResult)
                {
                    Button_Click("c", new RoutedEventArgs());
                }
                inputs[calculationTypeChosen != null ? 1 : 0] += content;
                InputDisplay.Content = inputs[calculationTypeChosen != null ? 1 : 0];
                break;
            }
        }