Example #1
0
        public void Run(string[] delimiters, bool allowNegative = true, int?upperBound = null, OperatorTypes mathOperator = OperatorTypes.Add)
        {
            try
            {
                if (delimiters?.Length > 0)
                {
                    _parser.SetDelimiters(delimiters);
                }
                _parser.AllowNegative = allowNegative;
                _parser.UpperBound    = upperBound;

                do
                {
                    try
                    {
                        _parser.Reset();

                        Console.Write("Please enter numbers: ");

                        int input;
                        do
                        {
                            input = Console.Read();

                            char ch;
                            try
                            {
                                ch = Convert.ToChar(input);
                            }
                            catch (OverflowException)
                            {
                                ch = 'a'; // causes entry to be 0
                            }

                            _parser.Read(ch);
                        }while (input != 13);
                        Console.ReadLine(); // read the rest of the line - \n

                        List <int>         numbers = _parser.GetNumbers();
                        ICalculationResult result  = _calculator.Calculate(numbers, mathOperator);
                        PrintResult(result.Text);
                    }
                    catch (FormatException formatException)
                    {
                        PrintError("Failed to process input: " + formatException.Message);
                        Console.ReadLine();
                    }
                    catch (Exception exception)
                    {
                        PrintError("Failed to read input: " + exception.Message);
                        Console.ReadLine();
                    }
                }while (true);
            }
            catch (Exception exception)
            {
                PrintError("Failed to run calculator: " + exception.Message);
            }
        }