//using System; class Class1 { public static void Main() { System.Console.WriteLine("Hello World!"); //中缀 => 后缀表达式 string s = "( 1.9 +(20 +41)/ (25*11) -3 ) * 2"; //中缀; //中缀 string S = ""; //后缀 char[] Operators = new char[s.Length]; int Top = -1; for (int i = 0; i < s.Length; i++) { char C = s[i]; switch (C) { case ' ': //忽略空格 break; case '+': //操作符 case '-': while (Top >= 0) //栈不为空时 { char c = Operators[Top--]; //pop Operator if (c == '(') { Operators[++Top] = c; //push Operator break; } else { S = S + c; } } Operators[++Top] = C; //push Operator S += " "; break; case '*': //忽略空格 case '/': while (Top >= 0) //栈不为空时 { char c = Operators[Top--]; //pop Operator if (c == '(') { Operators[++Top] = c; //push Operator break; } else { if (c == '+' || c == '-') { Operators[++Top] = c; //push Operator break; } else { S = S + c; } } } Operators[++Top] = C; //push Operator S += " "; break; case '(': Operators[++Top] = C; S += " "; break; case ')': while (Top >= 0) //栈不为空时 { char c = Operators[Top--]; //pop Operator if (c == '(') { break; } else { S = S + c; } } S += " "; break; default: S = S + C; break; } } while (Top >= 0) { S = S + Operators[Top--]; //pop Operator } System.Console.WriteLine(S); //后缀 //后缀表达式计算 double[] Operands = new double[S.Length]; double x, y, v; Top = -1; string Operand = ""; for (int i = 0; i < S.Length; i++) { char c = S[i]; if ((c >= '0' && c <= '9') || c == '.') { Operand += c; } if ((c == ' ' || i == S.Length - 1) && Operand != "") //Update { Operands[++Top] = System.Convert.ToDouble(Operand); //push Operands Operand = ""; } if (c == '+' || c == '-' || c == '*' || c == '/') { if ((Operand != "")) { Operands[++Top] = System.Convert.ToDouble(Operand); //push Operands Operand = ""; } y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响 x = Operands[Top--]; //pop 双目运算符的第一操作数 switch (c) { case '+': v = x + y; break; case '-': v = x - y; break; case '*': v = x * y; break; case '/': v = x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响 break; default: v = 0; break; } Operands[++Top] = v; //push 中间结果再次入栈 } } v = Operands[Top--]; //pop 最终结果 System.Console.WriteLine(v); System.Console.ReadLine(); } }
数据结构之应用"栈(Stack)"实现: 解析算术表达式及计算
最新推荐文章于 2024-06-26 11:04:18 发布