/// <summary> /// Creates tree based on an input string and root node /// </summary> /// <param name="s"></param> /// <param name="baseNode"></param> public void CreateTree(string s, BaseNode baseNode) { // if the string is empty, we don't do anything. This is the base case to leave the recursion if (s == string.Empty) { return; } // if it's 's', or '+', or whatever, we create a dedicated class (watch first case to see the logic) if (s[0] == 's') { SinNode node = new SinNode(s, baseNode); // dedicated class baseNode.Insert(node); // we insert it to the current head node CreateTree(node.value, node); // we change the head node to the newly created one } else if (s[0] == 'c') { CosNode node = new CosNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '*') { // same as in the first 'if' MultiplicationNode node = new MultiplicationNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '+') { // same as in the first 'if' SumNode node = new SumNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '/') { DivisionNode node = new DivisionNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '-' && !(s[1] >= '0' && s[1] <= '9')) { SubstractionNode node = new SubstractionNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == 'l') { LnNode node = new LnNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '^') { PowerNode node = new PowerNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '!') { FactorialNode node = new FactorialNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == 'p' || (s[0] >= '0' && s[0] <= '9')) { // stuff below just parses number string toParseIntoNumber = string.Empty; int counter = 0; if (s[0] == 'p') { toParseIntoNumber = "p"; } else { while ((s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.') { toParseIntoNumber += s[counter]; counter++; } } if (toParseIntoNumber.Contains('.')) { toParseIntoNumber = toParseIntoNumber.Replace('.', ','); } string @newS = string.Empty; for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++) { newS += s[i]; } // same stuff as in the first 'if' NumberNode node = new NumberNode(newS, baseNode, toParseIntoNumber); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '-' && (s[1] >= '0' && s[1] <= '9')) { // negative number s = Plotter.GetStringFromIndex(s, 1); string toParseIntoNumber = string.Empty; int counter = 0; if (s[0] == 'p') { toParseIntoNumber = "p"; } else { do { toParseIntoNumber += s[counter]; counter++; } while (counter < s.Length && ((s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.')); } string @newS = string.Empty; for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++) { newS += s[i]; } NumberNode node = new NumberNode(newS, baseNode, "-" + toParseIntoNumber); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == 'x') { // same as in the first 'if' BasicFunctionXNode node = new BasicFunctionXNode(s, baseNode); baseNode.Insert(node); CreateTree(node.value, node); } else if (s[0] == '(' || s[0] == ' ') { s = GetStringFromIndex(s, 1); // practically delete that ( or ' ' CreateTree(s, baseNode); } else if (s[0] == ')') { // count how many times ')' appears, let this number be 'i', then our head node is gonna go 'i' levels up int i = 0; while (s[i] == ')' && (s[i] != ',' || s[i] != ' ')) { i++; if (i == s.Length) { break; } } for (int j = 0; j < i; j++) { if (baseNode.parent != null) { baseNode = baseNode.parent; } else { throw new Exception("Eror in your input"); } } s = GetStringFromIndex(s, i); CreateTree(s, baseNode); } else if (s[0] == ',') { if (baseNode.parent == null) { throw new Exception("Error in your input"); } // go one level up baseNode = baseNode.parent; s = GetStringFromIndex(s, 1); CreateTree(s, baseNode); } }
public LnNode(string input, BaseNode parentNode) { value = Plotter.GetStringFromIndex(input, 1); parent = parentNode; }
/// <summary> /// To be called when a tree needs to be built upon an input string /// </summary> /// <param name="s"></param> public void ProcessString(string s) { if (s[0] == 's') { root = new SinNode(s, null); } else if (s[0] == '*') { root = new MultiplicationNode(s, null); } else if (s[0] == '+') { root = new SumNode(s, null); } else if (s[0] == '/') { root = new DivisionNode(s, null); } else if (s[0] == '-' && !(s[1] >= '0' && s[1] <= '9')) { root = new SubstractionNode(s, null); } else if (s[0] == 'c') { root = new CosNode(s, null); } else if (s[0] == 'l') { root = new LnNode(s, null); } else if (s[0] == '^') { root = new PowerNode(s, null); } else if (s[0] == '!') { root = new FactorialNode(s, null); } else if (s[0] == 'x') { root = new BasicFunctionXNode(s, null); } else if (s[0] >= '0' && s[0] <= '9') { string toParseIntoNumber = string.Empty; int counter = 0; if (s[0] == 'p') { toParseIntoNumber = "p"; } else { do { toParseIntoNumber += s[counter]; counter++; } while (counter < s.Length && (s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.'); } string @newS = string.Empty; for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++) { newS += s[i]; } // same stuff as in the first 'if' root = new NumberNode(newS, null, toParseIntoNumber); } else if (s[0] == '-' && (s[1] >= '0' && s[1] <= '9')) { // negative number s = Plotter.GetStringFromIndex(s, 1); string toParseIntoNumber = string.Empty; int counter = 0; if (s[0] == 'p') { toParseIntoNumber = "p"; } else { do { toParseIntoNumber += s[counter]; counter++; } while (counter < s.Length && (s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.'); } string @newS = string.Empty; for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++) { newS += s[i]; } // same stuff as in the first 'if' root = new NumberNode(newS, null, "-" + toParseIntoNumber); } CreateTree(root.value, root); }