示例#1
0
文件: Core.cs 项目: durasel74/Liusse
        private void Result()
        {
            logger.Trace("Вычисление результата...");
            try
            {
                string expression = currentExpression;
                int    priorityIndex;
                string expressionFragment;
                string result;

                int i = 0;
                while (i < Int32.MaxValue)
                {
                    expression = Parse.Format(expression);
                    logger.Trace($"{expression} =>");

                    priorityIndex = Calculate.PriorityOfOperations(expression);
                    if (priorityIndex == -1)
                    {
                        expression = Parse.FormatResult(expression);
                        if (expression != CurrentExpression)
                        {
                            Example           = Parse.AddingMissingBrackets(currentExpression) + '=';
                            CurrentExpression = expression;
                            Journal.AddElement(expression, Example);
                            logger.Trace($"Ответ: {Example + expression}");
                        }
                        break;
                    }

                    expressionFragment = Calculate.Selector(expression, priorityIndex);

                    result = Calculate.Arithmetic(Calculate.Converter(expressionFragment));

                    expression = Calculate.Replacer(expression,
                                                    expressionFragment, result);

                    i++;
                }
                if (i == Int32.MaxValue)
                {
                    throw new Exception("Лимит попыток посчитать пример превышен.");
                }
                logger.Trace("Вычисление закончилось успешно...");
            }
            catch (NotCorrectException) { }
            catch (OverflowException) { CurrentExpression = "Слишком большое число"; }
            catch (DivideByZeroException)
            {
                Example           = Parse.AddingMissingBrackets(CurrentExpression) + '=';
                CurrentExpression = "Деление на ноль невозможно";
            }
            catch (Exception error)
            {
                logger.Error("ОШИБКА: |" + error.Message + "|\n " +
                             "\t\t\t\t\t Приложение было принудительно остановлено.");
                Environment.Exit(0);
            }
        }