예제 #1
0
        public static void TestExpression(string expression, int expectedResult)
        {
            /*
             * this function tests the evaluate function for the Expression Parser.
             * It takes a string literal of the expression, evaluates it, and then compares
             * it to the same expression (in integer form) solved by the compiler
             */
            Console.WriteLine("Testing Expression Parser for the equation: " + expression);
            ExpressionTree parserTestTree = ExpressionParser.CreateTree(expression);
            int            treeResult     = parserTestTree.Evaluate();

            Console.WriteLine("Evaluated Expression:  " + treeResult);
            Console.WriteLine("Expected Result:       " + expectedResult);
            Debug.Assert(treeResult == expectedResult);
            Console.WriteLine();
        }
        public static ExpressionTree CreateTree(string formula)
        {
            ExpressionTree tree = new ExpressionTree();

            string[]               formulaArray = formula.Split();
            int                    n, currentOperationNum = -1, previousOperationNum = -1;
            Node <string>          leftChildOperand, rightChildOperand, parentOperator;
            Stack <Node <string> > operands  = new Stack <Node <string> >();
            Stack <Node <string> > operators = new Stack <Node <string> >();

            for (int i = 0; i < formulaArray.Length; i++)
            {
                Node <string> newNode = new Node <string> {
                    Value = formulaArray[i], leftChild = null, rightChild = null
                };

                if (int.TryParse(newNode.Value, out n)) // tests if the node's value is an integer or not
                {
                    operands.Push(newNode);
                }
                else
                {
                    switch (newNode.Value)
                    {
                    case "+":
                        currentOperationNum = 0;
                        break;

                    case "-":
                        currentOperationNum = 1;
                        break;

                    case "*":
                        currentOperationNum = 2;
                        break;

                    case "/":
                        currentOperationNum = 3;
                        break;

                    case "^":
                        currentOperationNum = 4;
                        break;
                    }

                    if (previousOperationNum == -1)
                    {
                        previousOperationNum = currentOperationNum;
                    }
                    else if (currentOperationNum <= previousOperationNum)
                    {
                        rightChildOperand = operands.Pop();
                        leftChildOperand  = operands.Pop();

                        parentOperator = operators.Pop();

                        parentOperator.rightChild = rightChildOperand;
                        parentOperator.leftChild  = leftChildOperand;

                        operands.Push(parentOperator);
                    }
                    previousOperationNum = currentOperationNum;
                    operators.Push(newNode);
                }
            }
            while (operators.Count > 0)
            {
                rightChildOperand = operands.Pop();
                leftChildOperand  = operands.Pop();

                parentOperator = operators.Pop();

                parentOperator.rightChild = rightChildOperand;
                parentOperator.leftChild  = leftChildOperand;

                operands.Push(parentOperator);
            }
            tree.rootNode = operands.Pop();
            return(tree);
        }