Example #1
0
 /// <summary>
 /// Обработка входящего сигнала
 /// </summary>
 /// <param name="key">Сигнал</param>
 private void HandleInput(char key)
 {
     try
     {
         if (Char.IsDigit(key) || key == '.')
         {
             _current.Append(key);
         }
         else if (key == (char)Keys.Back)
         {
             _current.RemoveDigit();
         }
         else if (key == (char)0)
         {
             _current.SetValue(0);
         }
         else if (_operations.ContainsKey(key))
         {
             HandleOperation(key);
         }
         _window.UpdateDisplay(_buffer.ToString(), _op, _current.ToString());
     }
     catch (NumberOverflowException)
     {
         _buffer = new CalculatorNumber();
         _current.SetValue(0);
         _op = "";
         _window.UpdateDisplay("OVERFLOW", _op, _current.ToString());
     }
     catch (NotANumberException)
     {
         _buffer = new CalculatorNumber();
         _current.SetValue(0);
         _op = "";
         _window.UpdateDisplay("NaN", _op, _current.ToString());
     }
 }
Example #2
0
        public Calculator()
        {
            _buffer     = new CalculatorNumber();
            _op         = "";
            _current    = new CalculatorNumber(0);
            _operations = new Dictionary <char, Action>
            {
                { '+', () => _buffer += _current },
                { '-', () => _buffer -= _current },
                { '*', () => _buffer *= _current },
                { '/', () => _buffer /= _current },
                { '=', () => _buffer.SetValue(_current) },
                { (char)Keys.Enter, () => _buffer.SetValue(_current) }
            };

            _window = new CalculatorWindow();
            _window.InputHandler += (keyCode => HandleInput(keyCode));
            _window.UpdateDisplay(_buffer.ToString(), _op, _current.ToString());
        }