コード例 #1
0
        private void ResolveSummandToList(Summand summand, List <Summand> list)
        {
            Summand matchingVariable = list.Find(x =>
                                                 x.Variable.Equals(summand.Variable) && x.Order == summand.Order);

            if (matchingVariable != null)
            {
                matchingVariable.Coefficient += summand.Coefficient;

                if (summand.IsRhSide)
                {
                    matchingVariable.IsRhSide = summand.IsRhSide;
                }
            }
            else
            {
                list.Add(summand);
            }
        }
コード例 #2
0
        private void ProcessSummands(string inputString, List <Summand> list, char startingOperation)
        {
            string  coefficient   = String.Empty;
            string  variable      = String.Empty;
            string  exponent      = String.Empty;
            char    operation     = OperationConstants.PLUS_SIGN;
            bool    isRhSide      = false;
            bool    isRaisedToExp = false;
            bool    isNumExpected = false;
            bool    safeToResolve = false;
            Summand summand;

            for (int i = 0; i < inputString.Length; i++)
            {
                char value = inputString[i];
                if (Char.IsWhiteSpace(value))
                {
                    continue;
                }
                if (IsBracket(value))
                {
                    int    bracketIndex     = FindBracketIndex(inputString.Substring(i));
                    string bracketSubString = inputString.Substring(i + 1, bracketIndex - 1);
                    if (startingOperation == operation)
                    {
                        ProcessSummands(bracketSubString, list, OperationConstants.PLUS_SIGN);
                    }
                    else
                    {
                        ProcessSummands(bracketSubString, list, OperationConstants.MINUS_SIGN);
                    }
                    i += bracketIndex;
                    continue;
                }
                if (IsOperation(value))
                {
                    if (isNumExpected)
                    {
                        throw new Exception();
                    }
                    isRaisedToExp = false;
                    if (value == OperationConstants.EXPONENT_SIGN)
                    {
                        if (!String.IsNullOrEmpty(variable))
                        {
                            isRaisedToExp = true;
                            isNumExpected = true;
                            safeToResolve = false;
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    if (value == OperationConstants.EQUAL_SIGN)
                    {
                        if (safeToResolve)
                        {
                            summand = new Summand(coefficient, exponent, variable, operation, isRhSide);
                            if (startingOperation.Equals(OperationConstants.MINUS_SIGN))
                            {
                                summand.Coefficient = summand.Coefficient * -1;
                            }
                            ResolveSummandToList(summand, list);
                            coefficient   = String.Empty;
                            variable      = String.Empty;
                            exponent      = String.Empty;
                            operation     = OperationConstants.PLUS_SIGN;
                            isRaisedToExp = false;
                            safeToResolve = false;
                            isNumExpected = false;
                        }
                        else if (list.Count.Equals(0))
                        {
                            throw new Exception();
                        }
                        isRhSide = true;
                    }
                    else if (value == OperationConstants.MINUS_SIGN)
                    {
                        if (safeToResolve)
                        {
                            summand = new Summand(coefficient, exponent, variable, operation, isRhSide);
                            if (startingOperation.Equals(OperationConstants.MINUS_SIGN))
                            {
                                summand.Coefficient = summand.Coefficient * -1;
                            }
                            ResolveSummandToList(summand, list);
                            coefficient   = String.Empty;
                            variable      = String.Empty;
                            exponent      = String.Empty;
                            isRaisedToExp = false;
                            safeToResolve = false;
                            isNumExpected = false;

                            operation = OperationConstants.MINUS_SIGN;
                        }
                        else
                        {
                            if (String.IsNullOrEmpty(coefficient) && String.IsNullOrEmpty(variable) &&
                                operation == OperationConstants.MINUS_SIGN)
                            {
                                operation = OperationConstants.PLUS_SIGN;
                            }
                            else
                            {
                                operation = OperationConstants.MINUS_SIGN;
                            }
                        }
                    }
                    else if (value == OperationConstants.PLUS_SIGN)
                    {
                        if (safeToResolve)
                        {
                            summand = new Summand(coefficient, exponent, variable, operation, isRhSide);
                            if (startingOperation.Equals(OperationConstants.MINUS_SIGN))
                            {
                                summand.Coefficient = summand.Coefficient * -1;
                            }
                            ResolveSummandToList(summand, list);
                            coefficient   = String.Empty;
                            variable      = String.Empty;
                            exponent      = String.Empty;
                            isRaisedToExp = false;
                            safeToResolve = false;
                            isNumExpected = false;

                            operation = OperationConstants.PLUS_SIGN;
                        }
                        else
                        {
                            operation = OperationConstants.PLUS_SIGN;
                        }
                    }
                }
                else if (Char.IsNumber(value) || value == '.')
                {
                    if (String.IsNullOrEmpty(variable))
                    {
                        coefficient  += value;
                        isNumExpected = false;
                    }
                    else if (isRaisedToExp)
                    {
                        exponent     += value;
                        isNumExpected = false;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else if (Char.IsLetter(value))
                {
                    if (!isRaisedToExp)
                    {
                        variable     += value;
                        isNumExpected = false;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                if (!String.IsNullOrEmpty(coefficient) || !String.IsNullOrEmpty(variable))
                {
                    if (!isNumExpected)
                    {
                        safeToResolve = true;
                    }
                }
            }
            summand = new Summand(coefficient, exponent, variable, operation, isRhSide);
            if (startingOperation.Equals(OperationConstants.MINUS_SIGN))
            {
                summand.Coefficient = summand.Coefficient * -1;
            }
            ResolveSummandToList(summand, list);
        }