使用栈完成表达式计算思路
- 通过一个index值(索引),来遍历表达式
- 如果是一个数值,就直接入 ==数栈==
- 如果是符号,当前的符号栈为空,就直接入 ==符号栈== ,如果符号栈有操作符,就进行比较,==如果当前的操作符优先级小于或者等于栈中的操作符==,就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到的结果,入数栈,然后将当前的操作符入符号栈。==如果当前的操作符的优先级大于栈中的操作符,就直接入符号栈==。
- 当表达式扫描完后,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行。
- 最后在数栈只有一个数字,就是表达式的结果。
ArrayStack.java
package top.snailstudy.stack;
public class ArrayStack {
private int maxSize;//栈的大小
private int[] stack;//数组
private int top = -1; //没有数据,初始化为-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;
}
//入栈
public void push(int value){
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈
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 peek(){
return stack[top];
}
//数字越大优先级越高
public int priority(int oper){
if(oper == '*' || oper == '/'){
return 1;
}else if(oper == '+' || oper == '-'){
return 0;
}else {
return -1;
}
}
//判断是不是一个运算符
public boolean isOper(char c){
return c == '+' || c == '-' || c== '*' || c=='/';
}
//计算方法
public int cal(int num1,int num2,int oper){
int res = 0;//用于存放计算结果
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;
}
public static void main(String[] args) {
//表达式
String expression = "3+2*6-2";
ArrayStack numStack = new ArrayStack(10);
ArrayStack operStack = new ArrayStack(10);
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = ' ';
String keepNum = "";//用于拼接多位数
while (true){
ch = expression.substring(index,index+1).charAt(0);
//判断是符号
if(operStack.isOper(ch)){
if(!operStack.isEmpty()){
//优先级小于栈内符号的
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); //ch是字符,要转成10进制
//处理多位数
keepNum += ch;
//如果ch已经是expression的最后一位,就直接入栈
if(index == expression.length() -1){
numStack.push(Integer.parseInt(keepNum));
}else {
if(operStack.isOper(expression.substring(index,index+2).charAt(0))){
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
if(index >= expression.length()){
break;
}
}
while (true){
//如果符号栈为空,则计算到最后的结果
if(operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1,num2,oper);
//把运算的结果入数栈
numStack.push(res);
}
System.out.printf("表达式 %s = %d",expression,numStack.pop());
}
}
输出结果 表达式 3+2*6-2 = 13