/**
* 中缀表达式计算器
*/
public class Calculator {
public static void main(String[] args) {
// 表达式
String expression = "7*2*2-5+1-5+3-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 = ' ';
// 用于拼接多位数
String keepNum = "";
// 扫描表达式
while (true) {
// 获取表达式
ch = expression.substring(index, index + 1).charAt(0);
// 判断是否是运算符
if (operStack.isOper(ch)) {
// 判断当前栈是否为空
if (operStack.isEmpty()) {
// 入栈
operStack.push(ch);
} else {
// 判断运算符优先级,当前符号优先级 <= 栈里符号优先级
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 {
// 如果是数,则直接入栈
// char型数字减去48,才是原本的值,否则会是表里的值
keepNum += ch;
// 如果ch已经是expression的最后一位,直接入栈
if (index == expression.length() - 1) {
// 直接入栈
numStack.push(Integer.parseInt(keepNum));
// keepNum 清空!!!
keepNum = "";
} else {
// 判断后一位是否是运算符
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
// 如果后一位是操作符,则入栈
numStack.push(Integer.parseInt(keepNum));
// keepNum 清空!!!
keepNum = "";
}
}
}
// 指针后移
index++;
// 判断是否扫描到了字符串的最后
if (index >= expression.length()) {
break;
}
}
// 表达式扫描完毕,顺序的从 数栈、符号栈 中,pop出相应的数据,并运算
while (true) {
// 如果符号栈为空,则计算结束
if (operStack.isEmpty()) {
break;
}
// 出栈
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
// 运算结果
res = numStack.cal(num1, num2, oper);
// 临时运算结果入栈
numStack.push(res);
}
System.out.printf("表达式 %s = %d", expression, numStack.pop());
}
}
/**
* 表示栈结构
*
* @author Mulun
*/
class ArrayStack2 {
/**
* 栈的大小
*/
private int maxSize;
/**
* 数据
*/
private int[] stack;
/**
* 指针,指向栈顶 - 默认为-1
*/
private int top = -1;
/**
* 创建栈
*
* @param maxSize 栈的大小
*/
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()) {
throw new RuntimeException("栈已满");
}
// 指针后移,指向栈顶
top++;
// 入栈
stack[top] = value;
}
/**
* 出栈
*/
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
// 弹出栈顶的数据
int value = stack[top];
// 指针迁移,重新指向栈顶
top--;
return value;
}
/**
* 遍历栈
*/
public void list() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
// 定义指针,用于遍历
int i = top;
while (true) {
if (i < 0) {
break;
}
System.out.printf("stack[%d]=%d\n", i, stack[i]);
i--;
}
}
/**
* 返回运算符的优先级
*/
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
}
return -1;
}
/**
* 判断是否是运算符
*/
public boolean isOper(char val) {
return val == '*' ||
val == '/' ||
val == '+' ||
val == '-';
}
/**
* 计算结果
*
* @param num1 数字1
* @param num2 数字2
* @param oper 运算符
* @return 结果
*/
public int cal(int num1, int num2, int oper) {
// 运算结果
int res = 0;
switch (oper) {
case '+':
res = num2 + num1;
break;
case '-':
res = num2 - num1;
break;
case '*':
res = num2 * num1;
break;
case '/':
res = num2 / num1;
break;
default:
throw new IllegalArgumentException("参数输入错误");
}
return res;
}
/**
* 查看栈顶的值
*/
public int peek() {
return stack[top];
}
}
08-16
04-05
1299