您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法
使用栈实现综合计算器(图文)
第十三双眼睛2022-06-13【数据结构与算法】人已围观
简介使用栈实现综合计算器
使用栈计算表达式的值思路:
首先创建两个栈,一个数栈,一个操作符号栈
1通过一个index来遍历表达式
2如果扫描到的是一个数字,就直接入数栈
3如果扫描到的是一个符号,分如下情况讨论
3.1如果当前的符号栈为空,就直接入栈
3.2如果当前符号栈有操作符,就进行比较,如果当前扫描到的操作符优先级小于或者等于栈中的操作符,就需要从操作数中弹出两个数,从符号栈中弹出一个符号进行计算,
算出的结果入数栈,扫描到的符号入符号栈,如果扫描到的符号优先级大于栈中的操作符,就直接入符号栈
4当表达式扫描完毕,就从符号栈和数栈中弹出相应的符号和数,做运算。
5当运算完毕,符号栈中为空,数栈中只有一个数,就是运算结果。
代码实现:
测试一下:
结果为15
上面的计算过程存在问题,当数字存在两位的情况时,运算结果就是错误的。
稍微改动一下:
测试一下:3+20*7-2
结果为141
首先创建两个栈,一个数栈,一个操作符号栈
1通过一个index来遍历表达式
2如果扫描到的是一个数字,就直接入数栈
3如果扫描到的是一个符号,分如下情况讨论
3.1如果当前的符号栈为空,就直接入栈
3.2如果当前符号栈有操作符,就进行比较,如果当前扫描到的操作符优先级小于或者等于栈中的操作符,就需要从操作数中弹出两个数,从符号栈中弹出一个符号进行计算,
算出的结果入数栈,扫描到的符号入符号栈,如果扫描到的符号优先级大于栈中的操作符,就直接入符号栈
4当表达式扫描完毕,就从符号栈和数栈中弹出相应的符号和数,做运算。
5当运算完毕,符号栈中为空,数栈中只有一个数,就是运算结果。
代码实现:
package day003; import day002.ArrayStack; public class Calculator { public static void main(String[] args) { String expression = "3+2*7-2";//存在多位数的问题 ArrayStack opeStack = new ArrayStack(10); ArrayStack numStack = new ArrayStack(10); //定义索引,操作数1,操作数2,栈中的操作符,运算结果,扫描到的字符 int index = 0; int num1 = 0; int num2 = 0; int oper = 0; int result = 0; char ch = ' '; while (true){ ch = expression.substring(index,index+1).charAt(0); if(isOperater(ch)){ if(opeStack.isEmpty()){ opeStack.push(ch); }else{ if(priority(ch) <= priority(opeStack.peek())){ num1 = numStack.pop(); num2 = numStack.pop(); oper = opeStack.pop(); result = calc(num1, num2, oper); numStack.push(result); opeStack.push(ch); }else{ opeStack.push(ch); } } }else{ numStack.push(ch - 48); } index ++; if(index >= expression.length()){ break; } } while (true){ if(opeStack.isEmpty()){ break; } num1 = numStack.pop(); num2 = numStack.pop(); oper = opeStack.pop(); result = calc(num1,num2,oper); numStack.push(result); } System.out.printf("结果为%d",numStack.pop()); } public static int priority(int operater){ if(operater =='*' || operater =='/'){ return 1; }else if(operater=='+' || operater =='-'){ return 0; } return -1; } public static boolean isOperater(int val){ return val =='+' || val =='-' || val =='*' || val =='/'; } public static int calc(int num1,int num2,int operater){ int result = 0; switch (operater) { case '+': result = num1 + num2; break; case '-': result = num2 - num1; break; case '*': result = num1 * num2; break; case '/': result = num2 / num1; break; default: throw new RuntimeException("请输入合法的运算符"); } return result; } } |
结果为15
上面的计算过程存在问题,当数字存在两位的情况时,运算结果就是错误的。
稍微改动一下:
package day003; import day002.ArrayStack; public class Calculator { public static void main(String[] args) { String expression = "3+20*7-2";//存在多位数的问题 ArrayStack opeStack = new ArrayStack(10); ArrayStack numStack = new ArrayStack(10); //定义索引,操作数1,操作数2,栈中的操作符,运算结果,扫描到的字符 int index = 0; int num1 = 0; int num2 = 0; int oper = 0; int result = 0; char ch = ' '; // 增加变量,解决多为数 String keepNum = ""; while (true){ ch = expression.substring(index,index+1).charAt(0); if(isOperater(ch)){ if(opeStack.isEmpty()){ opeStack.push(ch); }else{ if(priority(ch) <= priority(opeStack.peek())){ num1 = numStack.pop(); num2 = numStack.pop(); oper = opeStack.pop(); result = calc(num1, num2, oper); numStack.push(result); opeStack.push(ch); }else{ opeStack.push(ch); } } }else{ //numStack.push(ch - 48); // 这里不能直接入栈了. keepNum += ch; if(index == expression.length()-1){ numStack.push(Integer.parseInt(keepNum)); }else{ if(isOperater(expression.substring(index+1,index+2).charAt(0))){ numStack.push(Integer.parseInt(keepNum)); keepNum =""; } } } index ++; if(index >= expression.length()){ break; } } while (true){ if(opeStack.isEmpty()){ break; } num1 = numStack.pop(); num2 = numStack.pop(); oper = opeStack.pop(); result = calc(num1,num2,oper); numStack.push(result); } System.out.printf("结果为%d",numStack.pop()); System.out.printf(""); } public static int priority(int operater){ if(operater =='*' || operater =='/'){ return 1; }else if(operater=='+' || operater =='-'){ return 0; } return -1; } public static boolean isOperater(int val){ return val =='+' || val =='-' || val =='*' || val =='/'; } public static int calc(int num1,int num2,int operater){ int result = 0; switch (operater) { case '+': result = num1 + num2; break; case '-': result = num2 - num1; break; case '*': result = num1 * num2; break; case '/': result = num2 / num1; break; default: throw new RuntimeException("请输入合法的运算符"); } return result; } } |
结果为141
Tags:
很赞哦! ()
上一篇:栈(图文)
下一篇:前缀,中缀,后缀表达式(图文)