java实现栈的中缀综合计算器,没有做小数点的运算,如果要做自己加判断即可,源代码和步骤分析如下:
/**
* 栈实现中缀综合计算器:
* 1.通过一个Index值(索引),来遍历表达式
* 2.如果发现是一个数字,就直接入栈
* 3.如果发现是一个符号,有如下情况:
* 3.1发现当前符号栈为空,直接入栈
* 3.2符号栈有操作符,就进行比较:
* 3.2.1如果当前操作符的优先级小于或等于栈中的操作符,就需要从数栈中pop两个数,符号栈中pop出一个符号进行运算,结果入数栈,
* 然后将操作符入符号栈
* 3.2.2如果当前操作符优先级大于符号栈中的操作符,就直接入符号栈
* 4.当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,进行运算,并将结果入数栈
* 5.最后在数栈中的数字,就是表达式的结果。
*/
public class CalculatorDemo {
public static void main(String[] args) {
// 分别创建数栈和符号栈
Calculator numList = new Calculator(10);
Calculator markList = new Calculator(10);
String express = "33+4*5-6";
String numStr = "";
int index = 0;
int num1;
int num2;
int mark;
int res;
// 循环检索表达式
while (true) {
// 检索表达式
char ch = express.substring(index, index+1).charAt(0);
// 判断是否为符号
if (markList.isMark(ch)) { // 符号则入符号栈
// 判断符号栈是否为空
if (!markList.isEmpty()) { // 不为空
// 判断优先级
if (markList.priority(ch) <= markList.priority(markList.peek())) { // 优先级小于或等于的情况
// 当前操作符的优先级小于或等于符号栈中操作符的优先级,需要数栈pop两个数,符号栈pop一个符号,计算结果入数栈
// 当前操作符入符号栈
num1 = numList.pop();
num2 = numList.pop();
mark = markList.pop();
res = numList.cal(num1,num2,mark);
numList.push(res);
markList.push(ch);
}else { // 优先级大于的情况
markList.push(ch);
}
}else { // 为空
markList.push(ch);
}
}else { // 如果是数字
// 如果发现是一个数字,就直接入栈
// numList.push(ch-48);
numStr += ch;
if (index < express.length()-1) {
if (markList.isMark(express.substring(index+1, index+2).charAt(0))) {
System.out.println("numStr: " + numStr);
numList.push(Integer.parseInt(numStr));
numStr = "";
}
}else {
numList.push(Integer.parseInt(numStr));
numStr = "";
}
}
// 跳出
index++;
if (index >= express.length()) {
break;
}
}
//当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,进行运算,并将结果入数栈
while (true) {
if (markList.isEmpty()) {
break;
}
num1 = numList.pop();
num2 = numList.pop();
mark = markList.pop();
res = numList.cal(num1,num2,mark);
numList.push(res);
}
System.out.printf("表达式:%s=%d", express, numList.pop());
}
}
class Calculator {
int maxSize;
int top;
int[] stack;
public Calculator(int maxSize) {
this.maxSize = maxSize;
this.top = -1;
stack = new int[this.maxSize];
}
// 判断栈空
public boolean isEmpty() {
return top == -1;
}
// 判断栈满
public boolean isFull() {
return top == this.maxSize;
}
// 入栈
public void push(int e) {
// 判断栈满
if (isFull()) {
System.out.println("栈满,元素不能入栈");
return;
}
top++;
stack[top] = e;
}
// 出栈
public int pop() {
// 判断栈空
if (isEmpty()) {
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
// 查看栈顶元素
public int peek() {
// 判断栈空
if (isEmpty()) {
throw new RuntimeException("栈空");
}
return stack[top];
}
// 判断是否为符号
public boolean isMark(int e) {
if (e == '+' || e == '-' || e == '*' || e == '/') {
return true;
}else {
return false;
}
}
// 优先级判断
public int priority(int ch) {
if (ch == '*' || ch == '/') {
return 1;
}else if (ch == '+' || ch == '-') {
return 0;
}else {
return -1;
}
}
// 计算
public int cal(int num1, int num2, int mark) {
int res = 0;
switch (mark) {
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
}
return res;
}
}