コード例 #1
0
        public static Equation ParseEquation(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new ArgumentException($"{nameof(input)} cannot be null, empty or white space.");
            }

            if (!input.Any(c => Types.Comparison.Contains(c)))
            {
                throw new ArgumentException("An Equation contains comparison symbols. You want an Expression.");
            }

            int index = input.IndexOfAny(Types.Comparison.ToArray());

            string leftExpression = input.Substring(0, index);

            string comparison = input.ElementAt(index).ToString();

            if (Types.Comparison.Contains(input.ElementAt(index + 1)))
            {
                comparison += input.ElementAt(index + 1).ToString();
                index      += 1;
            }

            string rightExpression = input.Substring(index + 1);

            ComparisonType compareType = ConvertTo.ComparisonTypeEnum(comparison);
            Expression     lhs         = MathParser.ParseExpression(leftExpression);
            Expression     rhs         = MathParser.ParseExpression(rightExpression);

            Equation result = new Equation(lhs, compareType, rhs);

            return(result);
        }
コード例 #2
0
        public Problem(string[] statements)
        {
            Statements = new List <ISentence>();

            if (statements == null || statements.Length < 1)
            {
                throw new ArgumentException("You must pass at least one statement.");
            }

            _statements = statements;
            Statements  = _statements.Select(s => MathParser.ParseSentence(s)).ToList();
        }
コード例 #3
0
        public static ISentence ParseSentence(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new ArgumentException();
            }

            if (input.Any(c => Types.Comparison.Contains(c)))
            {
                return((ISentence)MathParser.ParseEquation(input));
            }
            else
            {
                return((ISentence)MathParser.ParseExpression(input));
            }
        }