package com.atguigu.stack;
import javax.print.CancelablePrintJob;
public class Calculator {
public static void main(String[] args) {
//根据思路完成表达式的运算
String expression = "700+2*6-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 = ' ';//将每次扫描得到的char保存到ch中
String keepNum = "";//用于拼接多位数
//开始while循环扫描expression
while (true) {
//依次得到expression的每一个字符
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);//? "1+3" '1'=>1
//分析思路:
//处理多位数问题
keepNum += ch;
//如果ch已经是expression的最后一位,就直接入栈
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="";
}
//利用下面代码处理多位数问题,代码存在问题,本身自带while循环,无需再循环了
}
//让index+1,并判断是否扫描到expression最后
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);
}
//将数栈最后的数pop出
int res2 = numStack.pop();
System.out.printf("表达式%s=%d", expression, res2);
}
}
//先创建一个栈,直接使用前面创建好的
//定义一个 ArrayStack 类表示栈,需要扩展功能
class ArrayStack2 {
private int maxSize; //栈的大小
private int[] stack; //数组,数组模拟栈,数据放在该数组中
private int top = -1;//top表示栈顶,初始化为-1
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//增加一个方法,返回当前栈顶的值,但不是真正的pop
public int peek() {
return stack[top];
}
//判断栈满
public boolean isFull() {
return top == maxSize;
}
//判断栈空
public boolean isEmpty() {
return top == -1;
}
//入栈-push
public void push(int value) {
//先判断栈是否满
if (isFull()) {
System.out.println("栈已满");
} else {
top++;
stack[top] = value;
}
}
//出栈-pop:将栈顶的数据返回
public int pop() {
//先判断是否空
if (isEmpty()) {
//抛出异常
throw new RuntimeException("栈空");
} else {
//return stack[top--];
int value = stack[top];
top--;
return value;
}
}
//显示栈的情况,遍历栈,遍历时,需要从栈顶显示数据
public void list() {
if (isEmpty()) {
System.out.println("栈空,没有数据");
} else {
//需要从栈顶显示数据
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d]=%dn", i, stack[i]);
}
}
}
//返回运算符的优先级,程序员自己确定,优先级使用数字表示,数字越大,则优先级越高
public int priority(int oper) {
if (oper == '*' || oper == '/') {//char底层还是int,所以可以使用比较 char和int是可以混用的
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
return -1;//假定目前表达式只有+ - * /
}
}
//判断是不是一个运算符
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;
}
return res;
}
}