public Math_Node(Math_Node math_Node) { Operation = math_Node.Operation; Root_Node = math_Node.Root_Node; Value = math_Node.Value; if (math_Node.Left != null) { Left = new Math_Node(math_Node.Left); } if (math_Node.Right != null) { Right = new Math_Node(math_Node.Right); } }
/* *---------------------------One of the most important functions of the program---------------------------------------- * Called when the equal sign is written */ private double Calculate_Value() { List <Mouse_Data> L = new List <Mouse_Data>(); for (int i = 0; i < Screen.Count; i++) { Mouse_Data D = Screen[i]; if (D.Label != "equals" && !D.Calculated) { L.Add(D); } Screen[i].Calculated = true; } Math_Node Tree = new Math_Node(L); return(Tree.Calculate()); }
private void Build_Tree(string Formula, int start = 0, int end = -1) { //end = -1 means that the end is at default value if (end == -1) { end = Formula.Length - 1; } //Iterator int i = start; //IsDigit: Whether the Left Node should be built as Number node //IsP: Whether the Left Node should be built by recursion bool IsDigit = false, IsP = false; //Left Node is number if (IsDigit = char.IsDigit(Formula[i])) { String_Calc.Jump_Till_End_Number(Formula, ref i); } //Left Node is new formula else if (IsP = (Formula[i] == '(')) { String_Calc.Jump_Till_Out(Formula, ref i); i++; } //The Formula only consist of a number if (i > end) { Value = Extract_Number(Formula, start); Root_Node = true; return; } if (Formula[i] == '(') { Left = new Math_Node(); Right = new Math_Node(); //default to multply Left.Build_Tree(Formula, start, i - 1); int j = i; String_Calc.Jump_Till_Out(Formula, ref j); Right.Build_Tree(Formula, i + 1, j - 1); Operation = '*'; return; } //It is now assumed that the next character is the current Operator Operation = Formula[i]; //Build the Left Node if (IsDigit) { Left = new Math_Node(Extract_Number(Formula, start)); } else if (IsP) { Left = new Math_Node(); Left.Build_Tree(Formula, start + 1, i - 2); } //Now goto the Right Node i++; //The Right Node is a number if (char.IsDigit(Formula[i])) { Right = new Math_Node(Extract_Number(Formula, i)); } //The Right Node is a new formula else if (Formula[i] == '(') { Right = new Math_Node(); Right.Build_Tree(Formula, i + 1, end - 1); } }