逆波兰计算器 发表于 2019-11-19 | 分类于 数据结构 | 阅读次数: | 字数统计: 196 一:思路分析 二:代码实现123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657public class PolandNotation { public List<string> getListString(string suff) { string[] split = suff.Split(' '); List<string> list = new List<string>(); foreach (var item in split) { list.Add(item); } return list; } public int calculate(List<string> list) { Stack<string> stack = new Stack<string>(); foreach (var item in list) { Regex regExp = new Regex("^\\d+$");//匹配的是多位数 if (regExp.IsMatch(item)) { //入栈 stack.Push(item); } else { int num2 = Convert.ToInt32(stack.Pop()); int num1 = Convert.ToInt32(stack.Pop()); int res = 0; if (item=="+") { res = num1 + num2; } else if (item == "-") { res = num1 - num2; } else if (item == "*") { res = num1 * num2; } else if (item == "/") { res = num1 / num2; } else { throw new Exception("运算符有问题! "); } //把res入栈 stack.Push(""+res); } } return Convert.ToInt32(stack.Pop()); } } 三:源码地址Github