示例#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);
            }
        }
示例#2
0
        public void Arithmetic_CorrectExpression_Result(object[] expression,
                                                        string result)
        {
            expression[1] = Convert.ToDecimal(expression[1]);
            expression[2] = Convert.ToDecimal(expression[2]);
            string expected = result;

            string actual = Calculate.Arithmetic(expression);

            Assert.AreEqual(expected, actual);
        }
示例#3
0
 public void Arithmetic_NotCorrectExpression_Exception(object[] expression)
 {
     Assert.Throws <Exception>(() => Calculate.Arithmetic(expression));
 }