private Function CreateTree() { Function currentNode = null; string token = listNotations.First(); switch (token) { case "c": currentNode = new Cosine(); listNotations.Remove(token); currentNode.Left = CreateTree(); break; case "/": currentNode = new Divide(); listNotations.Remove(token); currentNode.Left = CreateTree(); currentNode.Right = CreateTree(); break; case "e": currentNode = new Exp(); listNotations.Remove(token); currentNode.Left = CreateTree(); break; case "!": currentNode = new Factorial(); listNotations.Remove(token); currentNode.Left = CreateTree(); break; case "*": currentNode = new Multiply(); listNotations.Remove(token); currentNode.Left = CreateTree(); currentNode.Right = CreateTree(); break; case "l": currentNode = new NaturalLogarithm(); listNotations.Remove(token); currentNode.Left = CreateTree(); break; case "n": listNotations.Remove(token); string valueN = listNotations.First(); currentNode = new NaturalNumber(Convert.ToInt32(valueN)); listNotations.Remove(valueN); break; case "x": currentNode = new ParameterX(); listNotations.Remove(token); break; case "p": currentNode = new Pi(); listNotations.Remove(token); break; case "+": currentNode = new Plus(); listNotations.Remove(token); currentNode.Left = CreateTree(); currentNode.Right = CreateTree(); break; case "^": currentNode = new Power(); listNotations.Remove(token); currentNode.Left = CreateTree(); currentNode.Right = CreateTree(); break; case "r": listNotations.Remove(token); string valueR = listNotations.First(); currentNode = new RealNumber(Convert.ToDouble(valueR)); listNotations.Remove(listNotations.First()); break; case "s": currentNode = new Sine(); listNotations.Remove(token); currentNode.Left = CreateTree(); break; case "-": currentNode = new Substract(); listNotations.Remove(token); currentNode.Left = CreateTree(); currentNode.Right = CreateTree(); break; default: currentNode = new NaturalNumber(Convert.ToInt32(token)); listNotations.Remove(token); break; } return(currentNode); }
//Method //Insert function to the binary tree public Function Insert(ref string s) { Function root = null; switch (s[0]) { case '+': { root = new Plus(); s = s.Substring(2); while (s[0] != ',') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightFunc = Insert(ref s); s = s.Substring(1); } break; } case '-': { root = new Substract(); if (s[1] != '(') { break; } s = s.Substring(2); while (s[0] != ',') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightFunc = Insert(ref s); s = s.Substring(1); } break; } case '*': { root = new Multiply(); s = s.Substring(2); while (s[0] != ',') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightFunc = Insert(ref s); s = s.Substring(1); } break; } case '/': { root = new Divide(); s = s.Substring(2); while (s[0] != ',') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightFunc = Insert(ref s); s = s.Substring(1); } break; } case '^': { root = new Power(); s = s.Substring(2); while (s[0] != ',') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } s = s.Substring(1); while (s[0] != ')') { root.RightFunc = Insert(ref s); s = s.Substring(1); } break; } case 'n': { root = new n(); s = s.Substring(2); string[] parts = s.Split(')'); (root as n).Data = Convert.ToInt32(parts[0].ToString()); while (s[0] != ')') { s = s.Substring(1); } break; } case 'r': { root = new r(); s = s.Substring(2); string[] parts = s.Split(')'); (root as r).Data = Convert.ToDouble(parts[0].ToString()); while (s[0] != ')') { s = s.Substring(1); } break; } case 's': { root = new S(); s = s.Substring(2); while (s[0] != ')') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } break; } case 'c': { root = new c(); s = s.Substring(2); while (s[0] != ')') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } break; } case 'e': { root = new e(); s = s.Substring(2); while (s[0] != ')') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } break; } case 'l': { root = new l(); s = s.Substring(2); while (s[0] != ')') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } break; } case '!': { root = new Factorial(); s = s.Substring(2); while (s[0] != ')') { root.LeftFunc = Insert(ref s); s = s.Substring(1); } break; } case 'p': { root = new pi(); break; } case 'x': { root = new x(); break; } default: { root = new Digit(); (root as Digit).Data = Convert.ToInt32(s[0].ToString()); break; } } return(root); }