一、思路分析
这里我们使用自定义的栈结构完成一个小练习:简单的不带括号的中缀表达式计算器的实现。分析思路如下:
- 首先创建一个类来自定义栈结构以及所需要用到的一些基本方法。
- 实例化两个栈来分别存放数字和符号。
- 建立一个索引index来对中缀表达式进行遍历。
- 开始扫描遍历表达式:
- 如果是数,我们就查看表达式下一位 ,如果下一位也是数,就进行拼接操作,直到下一位不是数,然后将得到的数直接入栈。
- 如果是符号:
- 如果当前符号栈为空,直接将符号入栈。
- 如果符号栈不为空,就比较当前操作符与符号栈栈顶操作符, 如果当前操作符优先级小于或者等于栈中操作符,就从数栈中pop出两个数,再从符号栈中pop出一个符号,进行运算,将运算结果入数栈,再将当前符号入符号栈。 如果当前操作符优先级大于栈中操作符 ,就直接入栈。
- 当前表达式扫描完毕,就顺序从数栈中pop出两个数,从符号栈pop出一个符号进行运算。
- 直到数栈中只剩下一个数,就是表达式结果。
二、代码实现
1.首先创建一个类来定义栈结构以及要用到的基本方法
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 boolean isFull() {
return top == maxSize - 1;
}
//栈空
public boolean isEmpty() {
return top == -1;
}
//返回当前栈顶值
public int peak() {
return stack[top];
}
//入栈
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 > -1; 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) {
if (val == '+' || val == '-' || val == '*' || val == '/') {
return true;
}
return false;
}
//计算
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;
}
return res;
}
}
2. 利用自定义的栈结构按照分析思路完成计算器
import java.util.Scanner;
/**
* @author dankejun
* @create 2020-03-30 17:42
*/
public class Calculator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("请输入表达式:");
String expression = s.next();
// String expression = "3+2*6-2";
// String expression = "70+2*6-4";
//创建数栈和符号栈
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;
char ch = ' ';//将每次扫描到的char保存到ch
String keyNum = "";
//扫描表达式
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.peak())) {
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 {//如果是数字,进行拼接后,直接入栈
keyNum += ch;
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keyNum));
} else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numStack.push(Integer.parseInt(keyNum));
//情况keyNum
keyNum = "";
}
}
}
index++;
if (index >= expression.length()) {
break;
}
}
while (true) {
if (operStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
if (oper == '+' && !operStack.isEmpty() && operStack.peak() == '-') {
oper = '-';
}
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
System.out.printf("表达式%s=%d",expression,numStack.pop());
}
}
测试一下: