
2
∗
2
∗
3
−
1
+
4
+
7
−
3
2*2*3-1+4+7-3
2∗2∗3−1+4+7−3
代码实现:
import java.util.Scanner;
class ArrayStack{
private int maxSize;//栈的大小
private int stack[];//数组模拟栈
private int top = -1;//栈顶初始化为-1
//构造器
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//栈满
public boolean isFull(){
return top == maxSize - 1;
}
//栈空
public boolean isEmpty(){
return top == -1;
}
//入栈-push
public void push(int value){
//先判断栈是否满
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//返回当前栈顶的值,top值不变
public int peek() {
return(stack[top]);
}
//出栈-pop
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]=%dn",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 == '/';
}
//计算方法
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;
}
}
public class Calculator {
public static void main(String[] args) {
// TODO Auto-generated method stub
String expression = "40+2*6-2";
//创建两个栈,数栈和符号栈
ArrayStack numStack = new ArrayStack(10);
ArrayStack operStack = new ArrayStack(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()) {
//如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或等于栈中的操作符
//就需要从数栈中pop出两个数
//从符号栈中pop出一个符号,进行运算,得到结果,入数栈,然后将当前的操作符号入符号栈
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'=> 1
//改进版,可对多位数生效
keepNum += ch;
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清空
keepNum = "";
}
}
}
//让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出即为结果
System.out.printf("表达式%s = %d",expression, numStack.pop());
}
}