Пример #1
0
        /// <summary>
        /// Обработчик нажатия на клавишу-оператор
        /// </summary>
        /// <param name="sender">Объект-отправитель</param>
        /// <param name="e">Аргументы события</param>
        private void OnOperatorButtonClick(object sender, EventArgs e)
        {
            if (wasLastPressedGetAnswer)
            {
                wasLastPressedGetAnswer = false;
                previousOperator        = "=";
            }
            var    button          = sender as Button;
            string currentOperator = button.Text;

            try
            {
                var updatedValues = CalcFunctions.CalculateCurrentAnswer(currentAnswer, currentInput, previousOperator);
                currentAnswer    = updatedValues.Item2;
                currentValue     = 0;
                currentInput     = "";
                previousOperator = currentOperator;
                UpdateScreen();
            }
            catch (DivideByZeroException)
            {
                answerLabel.Text = "Дел ноль";
                currentValue     = 0;
                currentInput     = "";
            }
            catch (SyntaxErrorException)
            {
                answerLabel.Text = "ошибка ввода";
                currentValue     = 0;
                currentInput     = "";
            }
        }
Пример #2
0
        /// <summary>
        /// Обработчик события нажатия на равно
        /// </summary>
        /// <param name="sender">Объект-отправитель</param>
        /// <param name="e">Аргументы события</param>
        private void OnGetAnswerClick(object sender, EventArgs e)
        {
            wasLastPressedGetAnswer = true;
            operatorLabel.Text      = "=";
            try
            {
                var updatedValues = CalcFunctions.CalculateCurrentAnswer(currentAnswer, currentInput, previousOperator);
                currentValue  = updatedValues.Item1;
                currentAnswer = updatedValues.Item2;
            }
            catch (DivideByZeroException)
            {
                answerLabel.Text = "Дел ноль";
                currentValue     = 0;
                currentInput     = "";
                return;
            }
            catch (SyntaxErrorException)
            {
                answerLabel.Text = "ошибка ввода";
                currentValue     = 0;
                currentInput     = "";
                return;
            }
            string toPrint = currentAnswer.ToString();
            int    index   = toPrint.IndexOf(',');

            if (index > 0 && ((index + 2) < toPrint.Length))
            {
                toPrint = toPrint.Substring(0, index + 3);
            }
            if (toPrint.Length <= 8)
            {
                answerLabel.Text = toPrint;
            }
            else
            {
                answerLabel.Text = "Не влез";
            }
        }