Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            MathCalcResult result = null;
            var            input  = string.Join(" ", args);

            var        errorMessages = new List <string>();
            Expression expression    = null;

            try
            {
                var expressionFields = input.Split(';').Select(o => o.Trim());
                var arguments        = ParseArguments(expressionFields);
                var expressionData   = expressionFields.FirstOrDefault(o => !IsVariableDeclaration(o));

                if (string.IsNullOrEmpty(expressionData))
                {
                    Console.WriteLine(JsonConvert.SerializeObject(new MathCalcResult()
                    {
                        ErrorMessage = "Nelze spočítat prázdný požadavek."
                    }));

                    return;
                }

                expression = new Expression(expressionData, arguments.ToArray());
                errorMessages.AddRange(ValidateAndGetErrors(expression));
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    errorMessages.Add("Cannot correctly format input data");
                }
            }

            if (expression == null)
            {
                errorMessages.Add("Occured error in expression parsing.");
            }

            if (errorMessages.Count > 0)
            {
                result = new MathCalcResult()
                {
                    ErrorMessage = string.Join(Environment.NewLine, errorMessages)
                };

                Console.WriteLine(JsonConvert.SerializeObject(result));
                return;
            }

            result = new MathCalcResult()
            {
                IsValid       = true,
                Result        = expression.calculate(),
                ComputingTime = expression.getComputingTime() * 1000.0,
            };

            Console.WriteLine(JsonConvert.SerializeObject(result));
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            MathCalcResult result;
            var            parser = new ExpressionParser(string.Join(" ", args));

            if (parser.Empty)
            {
                Console.WriteLine(JsonConvert.SerializeObject(new MathCalcResult()
                {
                    ErrorMessage = "Nelze spočítat prázdný požadavek."
                }));

                return;
            }

            if (!parser.IsValid)
            {
                result = new MathCalcResult()
                {
                    ErrorMessage = string.Join(Environment.NewLine, parser.Errors)
                };

                Console.WriteLine(JsonConvert.SerializeObject(result));
                return;
            }

            result = new MathCalcResult()
            {
                IsValid       = true,
                Result        = parser.Expression.calculate(),
                ComputingTime = parser.Expression.getComputingTime() * 1000.0,
            };

            Console.WriteLine(JsonConvert.SerializeObject(result));
        }