コード例 #1
0
        // Обработка нажатия клавиши
        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                // Числовые клавиши
                #region Figures
            case Keys.D1:
            case Keys.NumPad1:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('1');
                }
                break;

            case Keys.D2:
            case Keys.NumPad2:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('2');
                }
                break;

            case Keys.D3:
            case Keys.NumPad3:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('3');
                }
                break;

            case Keys.D4:
            case Keys.NumPad4:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('4');
                }
                break;

            case Keys.D5:
            case Keys.NumPad5:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('5');
                }
                break;

            case Keys.D6:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        InputExpression.AddBinaryOperation("^");
                    }
                    else
                    {
                        InputExpression.AddFigure('6');
                    }
                }
                break;

            case Keys.NumPad6:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('6');
                }
                break;

            case Keys.D7:
            case Keys.NumPad7:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('7');
                }
                break;

            case Keys.D8:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        InputExpression.AddBinaryOperation("*");
                    }
                    else
                    {
                        InputExpression.AddFigure('8');
                    }
                }
                break;

            case Keys.NumPad8:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('8');
                }
                break;

            case Keys.D9:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        InputExpression.AddOpeningParenthesis();
                    }
                    else
                    {
                        InputExpression.AddFigure('9');
                    }
                }
                break;

            case Keys.NumPad9:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('9');
                }
                break;

            case Keys.D0:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        InputExpression.AddClosingParenthesis();
                    }
                    else
                    {
                        InputExpression.AddFigure('0');
                    }
                }
                break;

            case Keys.NumPad0:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddFigure('0');
                }
                break;
                #endregion Figures

            // Клавиши с десятичным разделителем и знаками операций
            case Keys.Divide:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddBinaryOperation("/");
                }
                break;

            case Keys.Multiply:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddBinaryOperation("*");
                }
                break;

            case Keys.Subtract:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddBinaryOperation("-");
                }
                break;

            case Keys.Decimal:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddPoint();
                }
                break;

            case Keys.Add:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddBinaryOperation("+");
                }
                break;

            case Keys.OemMinus:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    InputExpression.AddBinaryOperation("-");
                }
                break;

            case Keys.Oemplus:
                if (tb_Input.Text.Length < tb_Input.MaxLength)
                {
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        InputExpression.AddBinaryOperation("+");
                    }
                    else
                    {
                        InputExpression.FinishInput();
                        int i = 0;
                        while (i < OperationSigns.Length && !tb_Input.Text.Contains(OperationSigns[i]))
                        {
                            i++;
                        }
                        // Если выражение содержит знаки операций
                        if (i < OperationSigns.Length)
                        {
                            tb_RPNExpression.Text = Translation.TranslateExpression(tb_Input.Text);
                            tb_Output.Text        = Calculation.CalculateExpression(tb_RPNExpression.Text);
                        }
                        // Если выражение не содержит знаки операций
                        else
                        {
                            tb_RPNExpression.Text = tb_Output.Text = tb_Input.Text;
                        }
                    }
                }
                break;

            case Keys.Back:
                InputExpression.DeleteSymbol();
                break;

            case Keys.Escape:
                InputExpression.Clear();
                tb_RPNExpression.Text = tb_Output.Text = "0";
                break;
            }
        }
コード例 #2
0
 // Очистить выражение
 private void bt_ClearAll_Click(object sender, EventArgs e)
 {
     InputExpression.Clear();
     tb_RPNExpression.Text = tb_Output.Text = "0";
 }