Beispiel #1
0
        private void WordCase(string word)
        {
            List <Element> interpretedWord         = new WordInterpreter(word, additionalPriority).InterpretWord();
            var            previousElementAsNumber = previousElement as Number;

            if ((previousElementAsNumber is null ? false : previousElementAsNumber.value < 0) &&
                (interpretedWord.Last() is Constant ||
                 interpretedWord.Last() is Variable))                                                     // TODO Needs looking into
            {
                decodedEquation.Add(new Operation(OperationType.Addition, additionalPriority));
            }
            decodedEquation.AddRange(interpretedWord);
            previousElement = decodedEquation.Last();
        }
Beispiel #2
0
        public static List <Element> DecodeEquationLegacyCode(string input)
        {
            List <Element> result = new List <Element>();

            if (input.Length < 1)
            {
                throw new InvalidEquationException();
            }

            List <Bracket>  openBrackets         = new List <Bracket>();
            List <Function> twoArgumentFunctions = new List <Function>();
            int             additionalPriority   = 0;
            bool            isEqualitySign       = false;
            List <int>      commas = new List <int>();

            commas.Add(0);
            Element previousElement = null;

            string[] array = SplitLegacyCode(input);

            foreach (var item in array)
            {
                if (item[0] == '(')
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Constant ||
                            previousElement.elementType == ElementType.Parameter ||
                            previousElement.elementType == ElementType.Unknown)
                        {
                            result.Add(new Operation(OperationType.Multiplication, additionalPriority));
                        }
                    }
                    var bracket = new Bracket(BracketType.Opening);
                    commas.Add(0);
                    openBrackets.Add(bracket);
                    result.Add(bracket);
                    additionalPriority++;
                    previousElement = result.Last();
                }
                else if (item[0] >= '0' && item[0] <= '9' || item[0] == '-')  // SM - B.Close, Fact, Number, Const/P
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Constant ||
                            previousElement.elementType == ElementType.Parameter ||
                            previousElement.elementType == ElementType.Unknown)
                        {
                            result.Add(new Operation(OperationType.Multiplication, additionalPriority));
                        }
                    }
                    result.Add(new Number(double.Parse(item, CultureInfo.InvariantCulture)));
                    previousElement = result.Last();
                }
                else if (item.ToLower()[0] >= 'a' && item.ToLower()[0] <= 'z')
                {
                    var temp = new WordInterpreter(item, additionalPriority).InterpretWord();
                    if (previousElement != null)
                    {
                        if ((previousElement.elementType == ElementType.Number ? (previousElement as Number).value < 0 : false) &&
                            (temp.Last().elementType == ElementType.Constant || temp.Last().elementType == ElementType.Parameter || temp.Last().elementType == ElementType.Unknown))
                        {
                            result.Add(new Operation(OperationType.Addition, additionalPriority));
                        }
                    }
                    foreach (var subitem in temp)
                    {
                        result.Add(subitem);
                    }
                    previousElement = result.Last();
                }
                else if (operators.Contains(item[0]))
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Function)
                        {
                            throw new InvalidEquationException();
                        }
                    }
                    result.Add(new Operation((OperationType)Operation.CharToOperationType(item[0]), additionalPriority));
                    previousElement = result.Last();
                }
                else if (item[0] == ')')
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Function)
                        {
                            throw new InvalidEquationException();
                        }
                    }
                    if (openBrackets.Count == 0)
                    {
                        if (!isEqualitySign)
                        {
                            var bracket = new Bracket(BracketType.Opening);
                            foreach (var subitem in result)
                            {
                                if (subitem.elementType == ElementType.Operation || subitem.elementType == ElementType.Function)
                                {
                                    (subitem as Prioritabel).priority = (int)Math.Log((subitem as Prioritabel).priority, Operation.MAX_PRIORITY) + 1;
                                }
                            }
                            result.Insert(0, bracket);
                            result.Add(new Bracket(BracketType.Closing, bracket));
                        }
                        else
                        {
                            int equalityIndex = result.FindLastIndex(c => { return(c.elementType == ElementType.Equality); });
                            var bracket       = new Bracket(BracketType.Opening);
                            foreach (var subitem in result)
                            {
                                if ((subitem.elementType == ElementType.Operation || subitem.elementType == ElementType.Function) && result.IndexOf(subitem) > equalityIndex)
                                {
                                    (subitem as Prioritabel).priority = (int)Math.Log((subitem as Prioritabel).priority, Operation.MAX_PRIORITY) + 1;
                                }
                            }
                            result.Insert(equalityIndex + 1, bracket);
                            result.Add(new Bracket(BracketType.Closing, bracket));
                        }
                    }
                    else
                    {
                        var bracket = openBrackets.Last();
                        openBrackets.Remove(bracket);
                        commas.Remove(commas.Last());
                        result.Add(new Bracket(BracketType.Closing, bracket));
                        additionalPriority--;
                    }
                    previousElement = result.Last();
                }
                else if (item[0] == '=' || item[0] == '<' || item[0] == '>')
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Function)
                        {
                            throw new InvalidEquationException();
                        }
                    }
                    if ((result.Last().elementType == ElementType.Operation) ? (result.Last() as Operation).operationType == OperationType.Multiplication : false)
                    {
                        result.RemoveAt(result.Count - 1);
                    }
                    if (openBrackets.Count > 0)
                    {
                        foreach (var subitem in openBrackets)
                        {
                            result.Remove(subitem);
                        }
                        openBrackets.Clear();
                        additionalPriority = 0;
                    }
                    result.Add(new Equality((EqualityType)Equality.StringToEqualityType(item)));
                    isEqualitySign  = true;
                    previousElement = result.Last();
                }
                else if (item[0] == ',')
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Function)
                        {
                            throw new InvalidEquationException();
                        }
                    }
                    if (commas.Last() == Comma.MAX_COMMAS)
                    {
                        throw new InvalidEquationException();
                    }
                    result.Add(new Comma());
                    commas[commas.Count - 1]++;
                    previousElement = result.Last();
                }
                else if (item[0] == '!')
                {
                    if (previousElement != null)
                    {
                        if (previousElement.elementType == ElementType.Function)
                        {
                            throw new InvalidEquationException();
                        }
                    }
                    result.Add(new Operation(OperationType.Factorial, additionalPriority));
                    previousElement = result.Last();
                }
            }
            if (openBrackets.Count > 0)
            {
                int count = openBrackets.Count;
                foreach (var subitem in openBrackets)
                {
                    result.Add(new Bracket(BracketType.Closing, subitem));
                }
                openBrackets.Clear();
                additionalPriority = 0;
            }

            if (isEqualitySign)  // removes unnessesery brackets to avoid sth like this: (((X)))
            {
                int   equalityCount     = result.Count(c => { return(c.elementType == ElementType.Equality); });
                int[] equalitySignIndex = new int[equalityCount];
                equalitySignIndex[0] = result.FindIndex(c => { return(c.elementType == ElementType.Equality); });
                for (int i = 1; i < equalitySignIndex.Length; i++)
                {
                    equalitySignIndex[i] = result.FindIndex(equalitySignIndex[i - 1] + 1, c => { return(c.elementType == ElementType.Equality); });
                }
                for (int i = 0; i < equalityCount + 1; i++)
                {
                    while (result[i == 0 ? 0 : equalitySignIndex[i - 1]].elementType == ElementType.Brackets && result[i == equalityCount + 1 ? result.Count - 1 : equalitySignIndex[i]].elementType == ElementType.Brackets)
                    {
                        result.RemoveAt(i == 0 ? 0 : equalitySignIndex[i - 1]);
                        result.RemoveAt(i == equalityCount + 1 ? result.Count - 1 : equalitySignIndex[i]);
                    }
                }
            }
            else
            {
                while (result[0].elementType == ElementType.Brackets && result[result.Count - 1].elementType == ElementType.Brackets)
                {
                    result.RemoveAt(0);
                    result.RemoveAt(result.Count - 1);
                }
            }
            if (result.Last().elementType == ElementType.Function)
            {
                throw new InvalidEquationException();
            }
            return(result);
        }