Example #1
0
        void CombineTerms(char[] operators)
        {
            foreach (EquationElement element in Stack)
            {
                if (element is Term)
                {
                    ((Term)element).CombineTerms(operators);
                }
            }
            if (NeedSubTerms(operators) == false)
            {
                return;
            }

            int startIndex = 0;
            int startpoint = 0;

            while (startIndex < Stack.Count)
            {
                startIndex = FindOperator(startIndex, operators);
                if (startIndex < 0)
                {
                    return;
                }

                Term newterm = new Term();
                newterm.Parent = this;
                startIndex--;
                startpoint = startIndex;

                while (startIndex < Stack.Count)
                {
                    EquationElement item = Stack[startIndex];
                    if (item is EquationValue)
                    {
                        newterm.Add(item);
                        startIndex++;
                    }
                    if (item is Operator)
                    {
                        Operator op = item as Operator;
                        if (op == null || operators.Contains(op.Value) == false)
                        {
                            Stack.RemoveRange(startpoint, newterm.Stack.Count);
                            Stack.Insert(startpoint, newterm);
                            break;
                        }
                        newterm.Add(item);
                        startIndex++;
                    }

                    if (startIndex >= Stack.Count)
                    {
                        Stack.RemoveRange(startpoint, newterm.Stack.Count);
                        Stack.Insert(startpoint, newterm);
                        return;
                    }
                }
            }
        }
Example #2
0
        void CombineTopDown()
        {
            // ^연산자의 경우 가장 먼저 처리.
            // 2^3^4 일 경우엔 2^(3^4) right -> left(top->down) 형태로 처리
            char operatorCh = '^';

            foreach (EquationElement element in Stack)
            {
                if (element is Term)
                {
                    ((Term)element).CombineTopDown();
                }
            }
            int index = Stack.Count - 1;

            while (index > 0)
            {
                Operator op = Stack[index] as Operator;
                index--;
                if (op == null || op.Value != operatorCh)
                {
                    continue;
                }

                EquationElement left  = Stack[index];
                EquationElement right = Stack[index + 2];

                Term newterm = new Term();
                newterm.Parent = this;
                newterm.Add(left);
                newterm.Add(op);
                newterm.Add(right);

                // move signed to outer term
                if (((EquationValue)left).Signed)
                {
                    ((EquationValue)left).Signed = false;
                    newterm.Signed = true;
                }

                Stack.RemoveRange(index, newterm.Stack.Count);
                Stack.Insert(index, newterm);
            }
        }