//Remade without root public void setVar(string varName, double varValue) { //Put Variables in a Dictionary if (variables.ContainsKey(varName)) { variables[varName] = varValue; } else { variables.Add(varName, varValue); } //Check if regex is just a variable like "A1" thus root would be a varnode opNode sample = new opNode(); Type t = sample.GetType(); varNode sample2 = new varNode(); Type t2 = sample2.GetType(); if (root.GetType() == t2) { sample2 = (varNode)root; if (sample2.nname == varName) { sample2.nnumber = varValue; root.nnumber = varValue; } return; } else if (root.GetType() == t) { setVarroot((opNode)this.rroot, varName, varValue); } }
//https://www.geeksforgeeks.org/expression-tree/ //1) If character is operand push that into stack //2) If character is operator pop two values from stack make them its child and push current node again. //At the end only element of stack will be root of expression tree. public ExpTree(string expression) { root = new opNode(); opNode opTemp; varNode varTemp; numNode numTemp; regex = expression; Queue <string> post = new Queue <string>(); post = postfix(); Stack <Node> expbuilder = new Stack <Node>(); string item = ""; char op; double num; var isNumeric = double.TryParse(item, out num); while (post.Count > 0) { item = post.Dequeue(); if (item == "+" || item == "-" || item == "*" || item == "/" || item == "^") { opTemp = new opNode(); op = item[0]; opTemp.eexp = op; opTemp.lleft = expbuilder.Pop(); opTemp.rright = expbuilder.Pop(); expbuilder.Push(opTemp); } else { isNumeric = double.TryParse(item, out num); if (isNumeric) { numTemp = new numNode(); numTemp.nnumber = num; expbuilder.Push(numTemp); } else { varTemp = new varNode(); varTemp.nname = item; expbuilder.Push(varTemp); } } } root = expbuilder.Pop(); }
//Inorder search of the varNode type, and after found checking is varName matches private void setVarroot(opNode localroot, string varName, double varValue) { opNode sample = new opNode(); Type t = sample.GetType(); varNode sample2 = new varNode(); Type t2 = sample2.GetType(); if (!(localroot == null)) { if (t == localroot.lleft.GetType()) { setVarroot((opNode)localroot.lleft, varName, varValue); } else { if (t2 == localroot.lleft.GetType()) { sample2 = (varNode)localroot.lleft; if (sample2.nname == varName) { sample2.nnumber = varValue; //localroot.lleft = sample2; } } } if (t == localroot.rright.GetType()) { setVarroot((opNode)localroot.rright, varName, varValue); } else { if (t2 == localroot.rright.GetType()) { sample2 = (varNode)localroot.rright; if (sample2.nname == varName) { sample2.nnumber = varValue; //localroot.rright = sample2; } } } } }
//https://www.geeksforgeeks.org/expression-tree/ //1) If character is operand push that into stack //2) If character is operator pop two values from stack make them its child and push current node again. //At the end only element of stack will be root of expression tree. public ExpTree(string expression) { opNode opTemp = new opNode(); varNode varTemp = new varNode(); numNode numTemp = new numNode(); regex = expression; Queue <string> post = new Queue <string>(); post = postfix(); Stack <Node> expbuilder = new Stack <Node>(); string item = ""; char op; double num; var isNumeric = double.TryParse(item, out num); while (post.Count > 0) { item = post.Dequeue(); if (item == "+" || item == "-" || item == "*" || item == "/" || item == "^") { opTemp = new opNode(); op = item[0]; opTemp.eexp = op; opTemp.lleft = expbuilder.Pop(); opTemp.rright = expbuilder.Pop(); expbuilder.Push(opTemp); } else { isNumeric = double.TryParse(item, out num); if (isNumeric) { numTemp = new numNode(); numTemp.nnumber = num; expbuilder.Push(numTemp); } else { varTemp = new varNode(); varTemp.nname = item; expbuilder.Push(varTemp); } } } //Added to handle if the expression is just a number or variable like "A1" or just "5" if (expbuilder.Peek().GetType() == varTemp.GetType()) { root = new varNode(); root = expbuilder.Pop(); } else if (expbuilder.Peek().GetType() == numTemp.GetType()) { root = new numNode(); root = expbuilder.Pop(); } else if (expbuilder.Peek().GetType() == opTemp.GetType()) { root = new opNode(); root = expbuilder.Pop(); } else { //ERROR! } }