1 建立两个栈,一个是用来存储数值,称为数栈,一个用与存储符号,叫符号栈
这两个栈可以用同一个类,我们可以用整形存储数字和符号,在栈中定义栈的操作,还要定义符号优先级的判断方法、计算方法、判断是否是运算符的方法
2 通过一个index(索引),遍历表达式
3 如果我们发现是数字就直接入栈,注意要拼接,因为不一定是一位
4 如果当前扫描到是一个符号,分以下两种情况
4.1 如果 符号栈为空,就直接入符号栈
4.2 如果符号栈有符号 ,就进行比较,如果当前的操作符的优先级小于或等于栈顶符号,
就需要从数栈Ppop出两个数,再从符号pop出一个符号,进行运算,将结果入数栈,再 把当前运算符入符号栈,如果当前的操作符优先级高于符号栈栈顶元素的优先级,就直 接入栈
5 当表达式扫描完毕,就顺序的从数栈的符号栈pop出相应的符号和数字,并运算
6 最后在数栈只有一个数字,就是表达式的结果
package Stack;
public class Calculator {
public static void main(String[] args) {
String expression = "70+2*6-2";
//创建两个栈,一个数字栈,一个符号栈
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
int index = 0;//用于扫描
int num1 = 0;//取出的数1
int num2 = 0;//取出的数2
int oper = 0;//取出的符号
int res = 0;//运算结果
String keepNum = "";//用于拼接多位数
char ch = ' ';//将扫描到的char保存到ch
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
//判断ch是什么,做出相应处理
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);*/
keepNum += ch;
//如果ch已经是最后一位了,就直接入栈
if(index==expression.length()-1)
{
numStack.push(Integer.parseInt(keepNum));
}
//判断下一个字符,如果是数字就继续扫描
else if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
//如果后一位运算符,就入栈
numStack.push(Integer.parseInt(keepNum));
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);
res = numStack.cal(num1, num2, oper);
numStack.push(res);//入栈
}
System.out.printf("表达式 %s = %d", expression, numStack.pop());
}
}
//定义类表示栈结构
class ArrayStack2 {
private int maxSize;//栈的大小
private int[] stack;//数组
private int top = -1;//栈顶
public ArrayStack2(int maxSize)//构造器
{
this.maxSize = maxSize;
stack = new int[maxSize];
}
//栈满
public boolean idFull() {
return top == maxSize - 1;
}
//栈空
public boolean isEmpty() {
return top == -1;
}
//入栈
public void push(int value) {
if (idFull()) {
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈
public int pop() {
if (idFull()) {
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
//遍历栈
public void list() {
if (isEmpty()) {
System.out.println("栈空");
return;
}
int temp = top;
while (true) {
if (temp == -1) {
break;
}
System.out.println(stack[temp]);
temp--;
}
}
//返回运算符的优先级,用数字表示
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;
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 int peek() {
return stack[top];
}
}