void Woodification(TempNode node, ref int i, OperatorsType operatorsType) { if (node.Datas.Count > i + 1) { OperatorNode operatorNode = new OperatorNode(); operatorNode.ThisOperatorsType = operatorsType; if (node.Datas[i + 1].Data == "-" || node.Datas[i + 1].Data == "+") { node.Datas[i + 1].Data = node.Datas[i + 1].Data + node.Datas[i + 2].Data; node.Datas.RemoveAt(i + 2); } operatorNode.LeftSide = node.Datas[i - 1]; operatorNode.RightSide = node.Datas[i + 1]; for (int j = i - 1; j <= i + 1; j++) { node.Datas.RemoveAt(i - 1); } node.Datas.Insert(i - 1, operatorNode); i--; } else { node.Datas.RemoveAt(i); i--; } }
void addition(Node node) { if (node is TempNode) { TempNode tempNode = (TempNode)node; for (int i = 0; i < tempNode.Datas.Count; i++) { if (tempNode.Datas[i] is TempNode || tempNode.Datas[i] is OperatorNode) { addition(tempNode.Datas[i]); } else if (tempNode.Datas[i] is OperatorNode) { addition(((OperatorNode)tempNode.Datas[i]).LeftSide); } if (tempNode.Datas[i].Data == "+") { Woodification(tempNode, ref i, OperatorsType.Addition); } else if (tempNode.Datas[i].Data == "-") { Woodification(tempNode, ref i, OperatorsType.Subtraction); } } } }
void Calculation() { string T = textBox1.Text; List <string> tempTs = new List <string>(); int index = 0; for (int i = 0; i < T.Length; i++) { if (CheckOperator(T[i]) || T[i] == '%' || T[i] == '(' || T[i] == ')') { if (i - index != 0) { tempTs.Add(T.Substring(index, i - index)); } tempTs.Add(T[i].ToString()); index = i + 1; } } if (index != T.Length) { tempTs.Add(T.Substring(index, T.Length - index)); } index = 0; List <string> ss = new List <string>(); percentCheck(tempTs); TempNode tn = brackets(ChengeNode(tempTs)); multiplication(tn); addition(tn); decimal d = Calc(tn); textBox1.Text = decimal.Round(d, 2, MidpointRounding.AwayFromZero).ToString(); }
TempNode ChengeNode(List <string> strings) { TempNode tempNode = new TempNode(); for (int i = 0; i < strings.Count; i++) { tempNode.Datas.Add(new Node() { Data = strings[i] }); } return(tempNode); }
bool BracketsContains(TempNode node) { bool Next = false; for (int i = 0; i < node.Datas.Count; i++) { if (node.Datas[i].Data == "(") { Next = true; break; } } return(Next); }
TempNode brackets(TempNode node) { int sindex = -1; for (int i = 0; i < node.Datas.Count; i++) { if (node.Datas[i].Data == "(") { sindex = i; } else if (node.Datas[i].Data == ")") { if (sindex != -1) { TempNode tempNode = new TempNode(); for (int ii = sindex + 1; ii < i; ii++) { tempNode.Datas.Add(node.Datas[ii]); } node.Datas.RemoveRange(sindex, tempNode.Datas.Count + 2); node.Datas.Insert(sindex, tempNode); i = sindex - 1; sindex = -1; if (BracketsContains(node)) { brackets(node); } } } else { } } if (BracketsContains(node) == false) { if (node.Datas[node.Datas.Count - 1].Data == ")") { node.Datas.RemoveAt(node.Datas.Count - 1); } } return(node); }
void multiplication(Node node) { if (node is TempNode) { TempNode tempNode = (TempNode)node; for (int i = 0; i < tempNode.Datas.Count; i++) { if (tempNode.Datas[i] is TempNode) { multiplication((TempNode)tempNode.Datas[i]); } if (tempNode.Datas[i].Data == "×") { Woodification(tempNode, ref i, OperatorsType.Multiplication); } else if (tempNode.Datas[i].Data == "÷") { Woodification(tempNode, ref i, OperatorsType.Division); } } } }
decimal Calc(TempNode tempNode) { return(tempNode.Calc()); }
void BarketsChenge(TempNode tempNode) { }