package stack;
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 = ' ';// 将每次扫描得到的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 {
// 如果处理多位数时候,不能直接入栈
// 在处理数时,需要向expression的表达式index后再看一位,如果是数,进行拼接
// 需要定义一个变量,用于拼接
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清空
keepNum = "";
}
}
}
// 让index+1,并判断是否扫描到expression最后
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());
}
}
class ArrayStack2 {
private int maxSize;// 栈的大小
private int[] stack;// 数组模拟栈,数据放在该数组中
private int top = -1;// 栈顶
public ArrayStack2(int maxsize) {
this.maxSize = maxsize;
stack = new int[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;
}
// 出栈-pop
public int pop() {
if (isEmpty()) {
System.out.println("栈空");
return -1;
}
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 == '/';
}
// 计算方法
public int cal(int num1, int num2, int oper) {
int res = 0;// res用于存放计算的结果
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 int peek() {
return stack[top];
}
}
使用栈实现简单的计算器(不包含括号)
最新推荐文章于 2024-06-06 14:29:15 发布