Exemple #1
0
 private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
 {
     try
     {
         if (e.KeyChar == (char)Keys.Escape)
         {
             e.Handled = true;
             stack.Clear();
             RefreshCalc();
         }
         if (stack.Operate(txtInput.Text + e.KeyChar.ToString()))
         {
             e.Handled = true;
             RefreshCalc();
         }
         else if (e.KeyChar == (char)Keys.Enter && !stack.AddOperand(txtInput.Text))
         {
             MessageBox.Show($"Invalid input: {txtInput.Text}!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else if (e.KeyChar == (char)Keys.Enter)
         {
             RefreshCalc();
         }
     }
     catch (Exception ex)
     {
         e.Handled = true;
         txtInput.Clear();
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #2
0
        public void ReadValue()
        {
            var valueOrSymbol = Console.ReadLine();

            try
            {
                if (!operationStack.AddOperand(valueOrSymbol) && !operationStack.Operate(valueOrSymbol))
                {
                    DisplayErrorMessage($"Invalid symbol: {valueOrSymbol}");
                }
            }
            catch (Exception e)
            {
                DisplayErrorMessage(e.Message);
            }
            Refresh();
        }