栈的应用场景
数组模拟栈的思路分析
代码截图如下:
其他的实现参考:https://blog.youkuaiyun.com/weixin_42220532/article/details/101314493
栈实现综合计算器
1、使用栈来实现综合计算器
2、思路分析(示意图)
要注意的一点是,一旦处理完了符号的优先级,则剩下的数据已经没有优先级的问题了,先算后面再算前面结果是一样的。
比如5 + 3*6 - 4,先算成5 + 18 - 4,然后先算18 - 4 = 14,最后5 + 14 = 19,结果正确。
3、代码实现
public class Calcuator {
public static void main(String[] args) {
// 根据前面思路,完成表达式的运算
String expression = "3+2*6-2";
//创建两个栈,数栈,符号栈
ArrayStack2 numStack = new ArrayStack(10);
ArrayStack2 operStack = new ArrayStack(10);
//定义需要的相关变量,扫描表达式的索引
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = ' '; // 将每次扫描得到的 char保存到ch
String keepNum = ""; //用于拼接多位数
//开始用while循环扫描 expression
while(true) {
//依次得到 expression 的每一个字符
ch = expression.substring(index, index+1).charAt(0);
//判断ch是什么,然后做相应的处理
if(operStack.isOper(ch)) { //如果是运算符
// 判断当前符号栈是否为空
if(!operStack.isEmpty()) {
//如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或等于栈中的操作符
//在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
if(operStack.priority(ch) <= operStack.priority(operStack.peek()) ) {
//
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//运算结果如数栈
numStack.push(res);
//当前操作符如符号栈
operStack.push(ch);
} else {
// 如果当前的操作符的优先级大于栈中的操作符,就直接入栈
operStack.push(ch);
}
}else {
//如果为空,直接入栈
operStack.push(ch);
}
} else {
// 如果扫描是数字,就直接入数栈
// numStack.push(ch - 48); 处理两位数字
// 思路分析
// 1.当处理多位数时,不能发现是一个数就立即入栈,可能是多位数
// 2.在处理数时,需要向 expression 的表达式的 index 后再看以为,如果是数就进行扫描,如果是符号才入栈
// 3.因此定义一个变量,用于拼接
//处理多位数
keepNum += ch;
if(index == expression.length()-1) {
// 判断ch是否为最后一位,如果是,直接入栈
numStack.push(Integer.parseInt(keepNum));
} else {
//判断下一个字符是不是数字,如果是数字,就继续扫描,如果是运算符,则入栈
// 注意只是看后一位,不是index++
if(operStack.isOper(expression.substring(index+1, index+2).charAt(0))) {
// 如果后一位是运算符,则入栈
numStack.push(Integer.parseInt(keepNum));
// 重要!!!清空 keepNum
keepNum = "";
}
}
}
// 让 index + 1,并判断是否扫描到expression最后
index++;
if(index >= expression.length()) {
break;
}
}
//当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行
while(true) {
// 如果符号栈为空,则计算到最后结果,数栈中只有一个数字【结果】
if(operStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res); // 入栈
}
// 将数栈的最后数,pop出来
System.out.printf("表达式%s = %d\n",expression,numStack.pop());
}
}
// 创建一个栈,直接使用前面创建好
//定义一个ArrayStack 表示栈,扩展功能
class ArrayStack {
private int maxSize; // 栈的大小
private int[] stack; // 数组,数组模拟栈,数据就在该数组
private int top = -1; // top 表示栈顶,初始化为 -1
// 构造器
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
// 判断栈满
public boolean isFull() {
return top == maxSize - 1;
}
// 判断栈空
public boolean isEmpty() {
return top == -1;
}
// 入栈 - push
public void push(int value) {
// 先判断栈是否满
if (isFull()) {
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
// 出栈 - pop,将栈顶的数据返回
public int pop() {
// 先判断栈是否空
if (isEmpty()) {
// 抛出异常来处理
throw new RuntimeException("栈空,没有数据··");
}
int value = stack[top];
top--;
return value;
}
// 显示栈的情况[遍历栈],从栈顶往下显示数据
public void list() {
if (isEmpty()) {
System.out.println("栈空,没有数据~~");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d]=%d\n", i, stack[i]);
}
}
// 返回运算符的优先级,优先级是程序员来确定的,优先级使用数字表示,
// 数字越大,则优先级越高
public int priority(int oper) {
if(oper == '*' || oper == '/') {
return 1;
}else if (oper == '+' || oper == '-') {
return 0;
}else {
return -1; // 假定目前的表达式只有,+,-,*,/
}
}
// 判断是不是一个运算符
public boolean isOper(char val) {
return val == '+' || val =='-' || val == '*' ||val == '/';
}
// 计算方法
public int cal(int num1,int num2,int oper) {
int res = 0; //res 用于存放计算的结果
switch(oper) {
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1; // 注意顺序
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
default:
break;
}
return res;
}
// 增加一个方法,可以返回当前栈顶的值,不是真正的pop
public int peek() {
return stack[top];
}
}
前缀、中缀、后缀表达式
前缀表达式(波兰表达式)
中缀表达式(即正常书写的表达式)
后缀表达式(逆波兰表达式)
逆波兰计算器实现参考:https://msd.misuland.com/pd/3632025315607316204