/// <summary>
        /// Consists of a collection of expressions in brackets (if there are in the string) and a list of summands (contained between brackets, if any).
        /// If there are no brackets, then the monomial consists of one summand)
        /// </summary>
        /// <param name="monomialString"></param>
        /// <returns></returns>
        public static Monomial Parse(string monomialString)
        {
            if (monomialString == null)
            {
                throw new ArgumentNullException(nameof(monomialString));
            }
            if (monomialString.IsNullOrWhiteSpace())
            {
                throw new ArgumentException(nameof(monomialString));
            }

            var summandItems = ParseHelper.GetSummandsForMonomial(monomialString);

            //default value:
            //for brackets - 1 '(x-1) -> 1(x-1)'
            //otherwise - 0 'x^0 + a -> 1+a'
            var summand = summandItems.summandItems.Select(SummandParser.Parse).Multiply()
                          ?? new Summand(summandItems.bracketsParts.Count > 0 ? 1 : 0);


            IEnumerable <Summand> resultSummands = summand != null ? new [] { summand } : new Summand[] { };

            IEnumerable <Summand> result = resultSummands;

            foreach (var part in summandItems.bracketsParts)
            {
                var rightItems = PolynomialParser.Parse(part).Normalize().Summands.Sum();
                result = EquationOperation.Multiply(result, rightItems);
            }
            resultSummands = result.Sum();

            return(new Monomial(resultSummands ?? new Summand[] {}));
        }
Beispiel #2
0
        public static Equation Parse(string equationString)
        {
            if (equationString.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(equationString), "String shouldn't be empty");
            }
            var transformedEquationString = equationString.RemoveWhiteSpaces();

            var equationExpressionsStringParts = transformedEquationString.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

            if (equationExpressionsStringParts.Length == 0)
            {
                throw new NotValidEquationArgumentException("Symbol '=' not found in source string");
            }
            if (equationExpressionsStringParts.Length == 1)
            {
                throw new NotValidEquationArgumentException("The equation must be filled on either side of the symbol '='");
            }
            if (equationExpressionsStringParts.Length > 2)
            {
                throw new NotValidEquationArgumentException("There must be only one  symbol '=' in the equation");
            }

            var leftPart  = equationExpressionsStringParts[0];
            var rightPart = equationExpressionsStringParts[1];

            var left  = PolynomialParser.Parse(leftPart);
            var right = PolynomialParser.Parse(rightPart);

            return(new Equation(left, right));
        }