예제 #1
0
        // 计算表达树
        private String Calculate(CalculateTree T)
        {
            // 操作数左右孩子必为空
            if (T.getLchild() == null && T.getRchild() == null)
            {
                return(T.getValue());
            }

            String l = "", r = "";

            if (T.getLchild() != null)
            {
                l += Calculate(T.getLchild());
            }
            if (T.getRchild() != null)
            {
                r += Calculate(T.getRchild());
            }

            return(operate(T.getValue(), l, r));
        }
예제 #2
0
        // 构造表达树
        private void initCalculateTree(String expression)
        {
            Stack <char>          operationStack     = new Stack <char>();
            Stack <CalculateTree> calculateNodeStack = new Stack <CalculateTree>();


            int i = 0;

            expression += '\0';
            operationStack.Push('\0');

            while (i < expression.Length && (expression[i] != '\0' || operationStack.Count != 0))
            {
                // 过滤掉除操作符和数字以外的字符
                spiltChar(expression, ref i);

                // 开始建树
                if (!isOperationChar(expression[i]) || isNegative(expression, i))
                {   // 数字部分
                    String value = "";
                    value += expression[i++];
                    while (Char.IsDigit(expression[i]) || expression[i] == '.')
                    {
                        value += expression[i++];
                    }
                    CalculateTree T = new CalculateTree(value);
                    calculateNodeStack.Push(T);
                }
                else
                {
                    char t = judgPower(expression[i], operationStack.Peek());
                    switch (t)
                    {
                    case '>':
                    {
                        operationStack.Push(expression[i++]);
                        break;
                    }

                    case '<':
                    {
                        string sign = new string(operationStack.Peek(), 1);
                        operationStack.Pop();

                        CalculateTree second = calculateNodeStack.Peek();           // 后操作数
                        calculateNodeStack.Pop();
                        CalculateTree first = calculateNodeStack.Peek();            // 前操作数
                        calculateNodeStack.Pop();

                        CalculateTree T = new CalculateTree(sign, first, second);
                        calculateNodeStack.Push(T);
                        break;
                    }

                    case '=':
                    {
                        operationStack.Pop();
                        i++;
                        break;
                    }
                    }
                }
            }

            this.T = calculateNodeStack.Peek();
        }
예제 #3
0
 public CalculateTree(String value, CalculateTree lchild = null, CalculateTree rchild = null)
 {
     this.value  = value;
     this.lchild = lchild;
     this.rchild = rchild;
 }