我的Java数据结构学习--5.1----栈(中缀表达式)
package Demo1.stackArray; //计算机栈实现 public class calculatorStack { public static void main(String[] args) { calculatorStack calculatorStack = new calculatorStack(); String test = "7000+20*6*10-4*3*100"; ArrayStack numberStack = new ArrayStack(10);//数字 //字符 ArrayStack operStack = new ArrayStack(10); int index = 0; int num1 = 0; int num2 = 0; int oper = 0; int result = 0; char ch = ' '; String keepNumber = ""; while (true) { //第一次得到表达式的每一个字符 ch = test.substring(index, index + 1).charAt(0); //判断ch是什么,做出什么处理 if (calculatorStack.isOper(ch)) { //判断栈空 if (!operStack.stackIsEmpty()) { if (calculatorStack.priority(ch) <= calculatorStack.priority(operStack.peek())) { num1 = numberStack.pop(); num2 = numberStack.pop(); oper = operStack.pop(); result = calculatorStack.cal(num1, num2, oper); //把结果压栈 numberStack.push(result); operStack.push(ch); } else { //优先级 operStack.push(ch); } } else { operStack.push(ch); } } else { //处理多位数时,不能发现一个数就立即入栈(70 != 7 0) // numberStack.push(ch-48);//ASCII 0 ---48 //所以一定要扫描到符号为止。 keepNumber += ch; //如果是最后一位 if (index == test.length() - 1) { numberStack.push(Integer.parseInt(keepNumber)); } else { //判断下一位是不是数字,如果是数字,就继续扫描 if (calculatorStack.isOper(test.substring(index+1, index + 2).charAt(0))) { //后一位是操作符,则入栈 numberStack.push(Integer.parseInt(keepNumber)); //重要 keepNumber = "";//清空 } } } index++; //判断是否扫描到最后 if (index >= test.length()) { break; } } //最后的结果 while (true) { //如果符号栈为空,则计算到最后的结果,则数栈中只有一个结果 if (operStack.stackIsEmpty()) { break; } num1 = numberStack.pop(); num2 = numberStack.pop(); oper = operStack.pop(); result = calculatorStack.cal(num1, num2, oper); //把结果压栈 numberStack.push(result); // operStack.push(ch); } System.out.println("表达式:" + test + " :---->结果:" + numberStack.pop()); } //字符优先级判断(使用数字表示优先级,优先级越高,数字越大) public int priority(int oper) { if (oper == '*' || oper == '/') { return 1; } else if (oper == '+' || oper == '-') { return 0; } else { return -1; } } //判断是不是运算符 public boolean isOper(char val) { if (val == '*' || val == '/' || val == '+' || val == '-') { return true; } else return false; } //计算方法 public int cal(int num1, int num2, int oper) { int result = 0; switch (oper) { case '+': result = num1 + num2; break; case '-': result = num2 - num1; break; case '*': result = num1 * num2; break; case '/': result = num2 / num1; break; default: System.out.println("无效"); break; } return result; } }
表达式:7000+20*6*10-4*3*100 :---->结果:7000
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南