/// <summary> /// Non operation node construct. /// </summary> /// <param name="expression">given expression.</param> /// <returns>a BaseNode.</returns> private BaseNode ConstructTree(string expression) { BaseNode newNode; if (char.IsDigit(expression[0])) { // constant numerical node newNode = new ConstantNumbericalNode(Convert.ToDouble(expression)); return(newNode); } else if (char.IsLetter(expression[0])) { // Variable node if (!this.keyValuePairs.ContainsKey(expression)) { // if dictionary does not contain the expression key this.keyValuePairs.Add(expression, 0); } // construct the new node newNode = new VariableNode(expression); return(newNode); } return(null); }
private double Evaluate(BaseNode node) { double result = 0; if (node == null) { return(0); } // as a constant node ConstantNumbericalNode constantNode = node as ConstantNumbericalNode; if (constantNode != null) { result = constantNode.Value; return(result); } // as a variable VariableNode variableNode = node as VariableNode; if (variableNode != null) { result = this.keyValuePairs[variableNode.Name]; return(result); } // an operator node BinaryOperatorNode operatorNode = node as BinaryOperatorNode; if (operatorNode != null) { // choose operator switch (operatorNode.BinaryOperator) { case '+': result = this.Evaluate(operatorNode.Left) + this.Evaluate(operatorNode.Right); break; case '-': result = this.Evaluate(operatorNode.Left) - this.Evaluate(operatorNode.Right); break; case '*': result = this.Evaluate(operatorNode.Left) * this.Evaluate(operatorNode.Right); break; case '/': result = this.Evaluate(operatorNode.Left) / this.Evaluate(operatorNode.Right); break; } return(result); } return(0); }
private void TreeConstruct(Stack <BaseNode> treeStack, Queue <string> postfixQueue) { while (postfixQueue.Count > 0) { string currentPart = postfixQueue.Dequeue(); if (!string.IsNullOrEmpty(currentPart)) { // if next queue element is operand, push it to stack if (char.IsLetterOrDigit(currentPart[0])) { // if currentPart is a number. if (char.IsDigit(currentPart[0])) { ConstantNumbericalNode newNode = new ConstantNumbericalNode(Convert.ToDouble(currentPart)); treeStack.Push(newNode); } // if currentPart is a string else if (char.IsLetter(currentPart[0])) { VariableNode newNode = new VariableNode(currentPart); treeStack.Push(newNode); } } // if next queue element is operator else if (this.IsOperator(currentPart[0])) { switch (currentPart[0]) { case '+': Addition newAdd = new Addition(); newAdd.Right = treeStack.Pop(); newAdd.Left = treeStack.Pop(); treeStack.Push(newAdd); break; case '-': Minus newMin = new Minus(); newMin.Right = treeStack.Pop(); newMin.Left = treeStack.Pop(); treeStack.Push(newMin); break; case '*': Multiple newMul = new Multiple(); newMul.Right = treeStack.Pop(); newMul.Left = treeStack.Pop(); treeStack.Push(newMul); break; case '/': Divide newDiv = new Divide(); newDiv.Right = treeStack.Pop(); newDiv.Left = treeStack.Pop(); treeStack.Push(newDiv); break; } } } } }
/// <summary> /// construct a new tree based on the new expression. /// </summary> /// <param name="expression">return the root of the new tree.</param> private BinaryOperatorNode ConstructTree(string expression) { if (string.IsNullOrEmpty(expression)) { // empty expression return(null); } // create the tree stack to store nodes Stack <BaseNode> treeStack = new Stack <BaseNode>(); Queue <string> postfixQueue = new Queue <string>(); // get the postfix expression stored in a queue properly postfixQueue = this.ToPostFix(expression); // construct the tree while (postfixQueue.Count > 0) { string currentPart = postfixQueue.Dequeue(); if (!string.IsNullOrEmpty(currentPart)) { // if next queue element is operand, push it to stack if (char.IsLetterOrDigit(currentPart[0])) { // if currentPart is a number. if (char.IsDigit(currentPart[0])) { ConstantNumbericalNode newNode = new ConstantNumbericalNode(Convert.ToDouble(currentPart)); treeStack.Push(newNode); } // if currentPart is a string else if (char.IsLetter(currentPart[0])) { VariableNode newNode = new VariableNode(currentPart); treeStack.Push(newNode); } } // if next queue element is operator else if (this.IsOperator(currentPart[0])) { switch (currentPart[0]) { case '+': Addition newAdd = new Addition(); newAdd.Right = treeStack.Pop(); newAdd.Left = treeStack.Pop(); treeStack.Push(newAdd); break; case '-': Minus newMin = new Minus(); newMin.Right = treeStack.Pop(); newMin.Left = treeStack.Pop(); treeStack.Push(newMin); break; case '*': Multiple newMul = new Multiple(); newMul.Right = treeStack.Pop(); newMul.Left = treeStack.Pop(); treeStack.Push(newMul); break; case '/': Divide newDiv = new Divide(); newDiv.Right = treeStack.Pop(); newDiv.Left = treeStack.Pop(); treeStack.Push(newDiv); break; } } } } return((BinaryOperatorNode)treeStack.Pop()); }