【数据结构与算法(java)】栈实现综合计算器
栈实现综合计算器
思路:
-
通过一个index值遍历表达式
-
如果发现是数字,直接入栈
-
如果发现是符号,
如果符号栈为空,则入栈
如果符号栈不为空,则比较当前操作符的优先级:如果当前操作符的优先级小于等于栈中的操作符,就需要从数栈中pop出两个数,再从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
-
当表达式扫描完毕,就顺序地从数栈和符号栈中pop出相应的数和符号,并运行
-
最后在数栈只有一个数字,就是表达式的结果
public class Calculator {
public static void main(String[] args) {
String expression = "3+20*6-2";
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
int index = 0;
int num1;
int num2;
int oper;
int res=0;
String keepNum;
char ch;
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
if (operStack.isOper(ch)) {
if (!operStack.isEmpty() && 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);
index++;
} else {
keepNum="";
while(index<expression.length()&&!operStack.isOper(expression.charAt(index))) {
keepNum+=expression.charAt(index);
index++;
}
numStack.push(Integer.parseInt(keepNum));
}
if (index==expression.length()) {
while (!operStack.isEmpty()) {
num1=numStack.pop();
num2=numStack.pop();
oper=operStack.pop();
res=numStack.cal(num1, num2, oper);
numStack.push(res);
}
break;
}
}
System.out.println(res);
}
}
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 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 res = stack[top];
top--;
return res;
}
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) {
return switch (oper) {
case '*', '/' ->
1;
case '+', '-' ->
0;
default ->
-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:
throw new AssertionError();
}
return res;
}
public int peek() {
return stack[top];
}
}
下面实现括号功能:
- 优先级调整:
- 把左括号
(
的优先级设为 0,比加减运算的优先级还低。 - 乘除运算的优先级提升为 2。
- 把左括号
- 括号处理逻辑:
- 左括号:直接压入运算符栈,只有遇到右括号时才会弹出。
- 右括号:持续进行计算,直到找到对应的左括号,然后将左括号弹出。
public class Calculator {
public static void main(String[] args) {
String expression = "3*(2+6)-2";
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
int index = 0;
int num1;
int num2;
int oper;
int res = 0;
char ch;
String keepNum = "";
while (true) {
if (index >= expression.length()) {
break;
}
ch = expression.charAt(index);
if (ch == '(') {
operStack.push(ch);
index++;
} else if (ch == ')') {
while (operStack.peek() != '(') {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
operStack.pop(); // 弹出左括号
index++;
} else if (operStack.isOper(ch)) {
while (!operStack.isEmpty() &&
operStack.priority(ch) <= operStack.priority(operStack.peek()) &&
operStack.peek() != '(') {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
operStack.push(ch);
index++;
} else {
keepNum = "";
while (index < expression.length() &&
!operStack.isOper(expression.charAt(index)) &&
expression.charAt(index) != '(' &&
expression.charAt(index) != ')') {
keepNum += expression.charAt(index);
index++;
}
numStack.push(Integer.parseInt(keepNum));
}
}
while (!operStack.isEmpty()) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
System.out.printf("表达式 %s = %d\n", 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 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 res = stack[top];
top--;
return res;
}
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) {
return switch (oper) {
case '*', '/' -> 2;
case '+', '-' -> 1;
case '(' -> 0;
default -> -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:
throw new AssertionError("不支持的运算符: " + (char)oper);
}
return res;
}
public int peek() {
return stack[top];
}
}