不含括号
public class Calculator {
public static void main(String[] args) {
String expression = "70+2*6-4";
ArrayStack2 numberstack = 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 keepnumber = "";
while (true) {
//以此得到expression的每一个字符
ch = expression.substring(index, index + 1).charAt(0);
if (operstack.isOper(ch)) {//如果是运算符
//判断当前符号栈是否为空
if (!operstack.isEmpty()) {
//如果不为空 进行比较当前符号的优先级和栈顶的优先级
//若小于或等于则数栈pop两个,数符号栈pop一个数 进行运算 结果入数栈
if (operstack.priority(ch) <= operstack.priority(operstack.peek())) {
num1 = numberstack.pop();
num2 = numberstack.pop();
oper = operstack.pop();
res = numberstack.cal(num1, num2, oper);
//把运算的结果入数栈
numberstack.push(res);
//把当前符号入符号栈
operstack.push(ch);
} else {
//若大于 则直接入符号栈
operstack.push(ch);
}
} else {
//如果为空 直接入符号栈
operstack.push(ch);
}
} else {//如果是数字 直接入数栈
// '1' 和 1相差48
//numberstack.push(ch - 48);
//当处理多位数时 不能发现是一个数就立即入栈 因为他可能是多位数
//在处理数,需要向expression的表达式的index 后再看一位 如果是数就进行扫描 符号就入栈
keepnumber += ch;
//如果ch已经是expression的最后一位 则直接入栈
if (index == expression.length() - 1) {
numberstack.push(Integer.parseInt(keepnumber));
} else {
if (operstack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numberstack.push(Integer.parseInt(keepnumber));
keepnumber = "";//重要!!清空
}
}
}
//让index+1,并判断是否扫描到expression的最后
index++;
if (index >= expression.length()) {
break;
}
}
//表达式扫描完毕 就顺序的从数栈和符号栈pop出相应的数和符号 并运算
while (true) {
//如果符号栈为空 则计算到最后的结果 数栈只有一个结果
if (operstack.isEmpty()) {
break;
}
num1 = numberstack.pop();
num2 = numberstack.pop();
oper = operstack.pop();
res = numberstack.cal(num1, num2, oper);
numberstack.push(res);
}
System.out.printf("表达式%s=%d", expression, numberstack.pop());
}
}
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 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 == '/';
}
//计算方法 char和运算符可混用
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;
}
//可以返回当前栈顶的值,但不是真正的pop
public int peek() {
return stack[top];
}
}