一、计算思路
- 使用两个栈表示:数字栈、符号栈
- 遍历表达式的每一位,如果是数直接入栈
- 如果是符号需要分情况考虑
- 如果符号栈中有操作符,如果当前要加进去的操作符优先级小于栈顶的操作符,就从数字栈顶的pop出两个数字一个符号,得到的结果再入数字栈,当前符号入符号栈(也就是先用符号栈里的,不用当前的),如果当前符号优先级高于栈顶符号,那么就入栈
- 最后顺序执行,留在数字栈的最后一个数是结果
二、实现思路
- 创建栈,包含这样的属性maxSize、stack数组、top指针,实现其:
- 构造方法
- isFull()
- isEmpty()
- peek()
- push():入栈
- pop():出栈
- list():遍历栈元素
- priority():返回符号优先级
- isOper():是不是运算符
- cal():计算
- 有了栈,就可以创建两个栈:数字栈和符号栈
- 需要这么几个变量:
- int index:索引,扫描表达式用
- int result:结果
- int num1、num2:两个数
- int oper:表示符号,不过符号char也可以用int来表示,到时候再转就ok
- ch:char表示通过索引保存的元素
- String keepNum:用于保存字符串
- while循环遍历expression表达式,遍历每一个元素,查看这个元素是不是运算符,如果是字符:
- 判断符号栈是否为空,为空就放进去
- 如果符号栈有操作符就进行比较,如果当前符号优先级小于或等于符号栈栈顶优先级,就从数栈弹出两个元素,从符号栈弹出一个元素进行计算,结果入数栈,当前符号入符号栈
- 如果当前符号大于栈顶符号优先级直接入栈
- 还需要考虑多位数的问题
- 判断数是否是最后一位,如果是直接入数栈
- 不是的话,向后看一位,如果下一位是数就用keepNum(就是用来保存多位数的)进行字符串拼接,直到下一位不是数了为止,把字符串压入数栈
- 直到index >= expression.length,让整个循环结束
- 最后计算剩余的栈中数和符号,弹两个数一个符号,然后计算再压入数栈,直到
三、代码
public class Calculator {
public static void main(String[] args) {
String expression = "7*2*2-5+1-5+3-4";
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(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 {
keepNum += 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 = "";
}
}
}
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);
}
int res2 = numStack.pop();
System.out.printf("表达式 %s = %d", expression, res2);
}
}
class ArrayStack2 {
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public int peek() {
return stack[top];
}
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 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;
}
}