完整代码如下:
package com.wqc.stack;
import java.util.Arrays;
/**
* @author 高瞻远瞩
* @version 1.0
* @motto 算法并不可怕, 可怕的是你不敢面对它, 加油!别浮躁~冲击大厂!!!
* 演示栈实现综合计算器
*/
public class Calculator {
public static void main(String[] args) {
//初始化一个符号栈
ArrayStack2 ope = new ArrayStack2(10);
//初始化一个数字栈
ArrayStack2 num = new ArrayStack2(10);
String str = "3+2*6/2";
int index = 0;
String keepNum = "";
/**
* 思路1:用index下标来进行控制进行循环 好处是可以方便的查看下一个是不是数字 然后进行拼接 需要考虑的是
* 表达式最后是数字的情况的结束条件
*/
while(true){
String s = str.substring(index, index + 1);
if (s.matches("\\d")) {//如果匹配的是数字
keepNum += s;
//最后一位一定是数字 直接把拼接好的数字入栈即可
if(index == str.length() - 1){
num.push(Integer.parseInt(keepNum));
}else{
if (str.substring(index + 1, index + 2).matches("[+\\-*/]")) {
//只有下一个是符号的话 才把数字压入栈中 否则就一致进行拼接 压入栈的话 需要把 keepNum 变成空串
num.push(Integer.parseInt(keepNum));//如果只是一个数字的话把它压入栈中即可压入栈中
keepNum = "";
}
}
}
if (s.matches("[+\\-*/]")) {//如果是符号
// System.out.println(s.charAt(0));
if (ope.isEmpty()) {//如果栈为null 直接入栈
ope.push(s.charAt(0));
} else {//符号栈不为null的情况
if (ope.priority(s.charAt(0)) > ope.priority(ope.showTop())) {
ope.push(s.charAt(0));//如果当前元素的优先级大于栈顶元素,直接入栈
} else {//当前元素的优先级小于等于栈顶元素
//数栈弹两个 符号栈弹出1个 然后进行运算
int num1 = num.pop();
int num2 = num.pop();
int opera = ope.pop();
int value = num.cal(num1, num2, opera);//计算
//将计算结果压入数栈
num.push(value);
//当前元素符号压入符号栈中
ope.push(s.charAt(0));
}
}
}
if(index == str.length() - 1){
break;
}
index++;
}
/**
* 思路2:直接遍历 在处理多位数的时候 在后面多添加一个运算符 这样既可以少遍历一个 来查看下一个字符
* 同样可以作为中缀表达式中最后是数字的结束条件
*/
// StringBuffer stringBuffer = new StringBuffer(str);
// String str2 = stringBuffer.append("*").toString();//故意多添一个*
// String[] split = str2.split("");
// System.out.println(Arrays.toString(split));
// String temp = "";
// for (int i = 0; i < split.length - 1; i++) {
// String s = split[i];
// if (s.matches("\\d")) {//如果匹配的是数字
// temp += s;
// if (split[i+1].matches("[+\\-*/]")) {
// num.push(Integer.parseInt(temp));//如果只是一个数字的话把它压入栈中即可压入栈中
// temp = "";
// }
// }
// if (s.matches("[+\\-*/]")) {//如果是符号
System.out.println(s.charAt(0));
// if (ope.isEmpty()) {//如果栈为null 直接入栈
// ope.push(s.charAt(0));
// } else {//符号栈不为null的情况
// if (ope.priority(s.charAt(0)) > ope.priority(ope.showTop())) {
// ope.push(s.charAt(0));//如果当前元素的优先级大于栈顶元素,直接入栈
// } else {//当前元素的优先级小于等于栈顶元素
// //数栈弹两个 符号栈弹出1个 然后进行运算
// int num1 = num.pop();
// int num2 = num.pop();
// int opera = ope.pop();
// int value = num.cal(num1, num2, opera);//计算
// //将计算结果压入数栈
// num.push(value);
// //当前元素符号压入符号栈中
// ope.push(s.charAt(0));
// }
// }
// }
// }
//当遍历结束后 将剩余元素从数栈和符号栈中弹出 进行运算
while (true) {
if (ope.isEmpty()) {//如果符号栈为null的话 结束循环
break;
}
int num1 = num.pop();
int num2 = num.pop();
int opera = ope.pop();
int value = num.cal(num1, num2, opera);//计算
//将计算结果压入数栈
num.push(value);
}
//跳出循环时 说明最后计算的结果在数栈中 它就是计算的最终结果
System.out.println("最终的计算结果=" + num.pop());
}
}
class ArrayStack2 {
private int top;//栈顶
private int maxSize;
int[] arr;
public ArrayStack2(int maxSize) {//通过构造器给数组初始化大小
this.maxSize = maxSize;
arr = new int[this.maxSize];
top = -1;
}
//判断栈是否为满
public boolean isFull() {
if (top == maxSize - 1) {
return true;
}
return false;
}
//判断栈是否为null null只是表面上指针移动到-1 其实上面还有元素 只不过再下次入栈时把它覆盖了
public boolean isEmpty() {
if (top == -1) {
return true;
}
return false;
}
//演示入栈操作
public void push(int n) {
//判断栈是否为满
if (isFull()) {
System.out.println("栈已满...入栈失败");
return;
}
top++;
arr[top] = n;
}
//出栈操作
public int pop() {
//判断栈是否为null
if (isEmpty()) {
throw new RuntimeException("栈为空...出栈失败");
}
int temp = arr[top];
top--;
return temp;
}
//显示栈的情况 遍历栈
public void showStack() {
if (isEmpty()) {
System.out.println("栈为null...");
}
for (int i = top; i >= 0; i--) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
//显示栈顶元素的方法
public int showTop() {
return arr[top];
}
//判断一个操作符的优先级 假定*/的有限级一样 都是1
//+-的优先级一样 都是0 如果输入其他的 返回-1 数值越大 优先级越高
public int priority(int operator) {//char类型的可以用数字来比较
if (operator == '*' || operator == '/') {
return 1;
} else if (operator == '+' || operator == '-') {
return 0;
}
return -1;
}
//判断是不是一个运算符
public boolean isOperator(char operator) {
return operator == '+' || operator == '-' || operator == '*' || operator == '/';
}
public boolean isDigit(char operator) {
return operator >= '1' && operator <= '9';
}
//计算方法
public int cal(int n1, int n2, int operator) {
if (operator == '+') {
return n1 + n2;
} else if (operator == '-') {
return n2 - n1; //把后弹出的数作为减数
} else if (operator == '*') {
return n1 * n2;
} else if (operator == '/') {
return n2 / n1;//后弹的数作为除数
}
throw new RuntimeException();
}
}