//Step #2: Break values in Expression (exclude operators and parentheses) into appropriate type of Nodes private void expToNodes(ref string s, ref Cell sb, ref List <Node> nodes, string t = "") { Node var = null; OpNode op; if (s != "") { //Better error handling. if ((s.Length <= 3) && (Regex.IsMatch(s[0].ToString(), @"^[a-zA-Z]") == true) && (Regex.IsMatch(s.Substring(1, s.Length - 1), @"^[0-9]") == true)) { int col = Convert.ToInt32(s[0]) - 65; int row = Convert.ToInt32(s.Substring(1, s.Length - 1)) - 1; if (_variables[row, col].Text != null)//&&(Regex.IsMatch(_variables[row, col].Text, @"^[0-9]") == true)) { var = new VariableNode(_variables[row, col], s); sb.getToKnow(ref _variables[row, col]); } } else if ((Regex.IsMatch(s, @"^[0-9]") == true) | ((s == "Positive Infinity") | (s == "Negative Infinity") | (s == "infi") | (s == "neg infi") | (s.ToString() == "NEG INFI") | (s.ToString() == "INFI"))) { var = new ConstantNode(s); } if ((nodes.Count != 0) && (nodes[nodes.Count - 1].Height == 0)) { nodes[nodes.Count - 1].RightChild = var; } else { nodes.Add(var); } s = ""; } if (t != "") { op = new OpNode(t); nodes.Add(op); } }
// it is a construct function to evaluate all the nodes under root, handy. When it has parameters, I will write another construct loop under the Node class to evaluate them. public ConstantNode calculate(OpNode sth) { ConstantNode baseLeft; ConstantNode baseRight; //handle the +/- value if (sth.LeftChild == null) { if (sth.Height == 1) { sth.LeftChild = new ConstantNode("0"); } } //Calculate left + {handle the +/- value} if ((sth.LeftChild.LeftChild != null) && (sth.LeftChild.RightChild != null)) { OpNode l = (OpNode)sth.LeftChild; baseLeft = sth.calculate(l); } //{handle the +/- value} in the left else if ((sth.LeftChild is OpNode) && sth.LeftChild.RightChild == null) { OpNode l = (OpNode)sth.LeftChild; l.RightChild = sth; sth.LeftChild = new ConstantNode("0"); return(this.calculate(l)); } else { if ((Regex.IsMatch(sth.LeftChild.getName[0].ToString(), @"^[a-zA-Z]") == true) || (sth.LeftChild.getName == " ")) { VariableNode l = (VariableNode)sth.LeftChild; baseLeft = new ConstantNode(l.getNumbricValue.ToString()); } else { ConstantNode l = (ConstantNode)sth.LeftChild; baseLeft = new ConstantNode(l.getNumbricValue.ToString()); } } //Calculate right, there is NO NEED to handle the +/- value !!! if ((sth.RightChild.LeftChild != null) && (sth.RightChild.RightChild != null)) { OpNode r = (OpNode)sth.RightChild; baseRight = sth.calculate(r); } else { if ((Regex.IsMatch(sth.RightChild.getName[0].ToString(), @"^[a-zA-Z]") == true) || (sth.RightChild.getName == " ")) { VariableNode r = (VariableNode)sth.RightChild; baseRight = new ConstantNode(r.getNumbricValue.ToString()); } else { ConstantNode r = (ConstantNode)sth.RightChild; baseRight = new ConstantNode(r.getNumbricValue.ToString()); } } float result; switch (sth._op) { case "+": result = baseLeft.getNumbricValue + baseRight.getNumbricValue; break; case "-": result = baseLeft.getNumbricValue - baseRight.getNumbricValue; break; case "*": result = baseLeft.getNumbricValue * baseRight.getNumbricValue; break; case "/": if (baseRight.getNumbricValue == 0) { throw new Exception(); } else { result = baseLeft.getNumbricValue / baseRight.getNumbricValue; } break; default: result = 0; break; } ConstantNode tmp = new ConstantNode(result.ToString()); return(tmp); }