java用栈实现计算器完整版
作者:互联网
java用栈实现计算器完整版
栈类
class Stack {
private int maxSize;
private int[] stack;
private int top = -1;
public Stack(int maxSize) {
this.maxSize = maxSize;
stack = new int[maxSize];
}
// 返回栈顶元素
public int peek() {
return stack[top];
}
public void show() {
if (isEmpty()) {
System.out.println("栈空的");
return;
}
for (int i = top; i >= 0; --i) {
System.out.println("stack[" + i + "] ==> " + stack[i]);
}
}
public void push(int value) {
if (isFull()) {
System.out.println("栈满了");
return;
}
// 插入数据
stack[++top] = value;
}
public Integer pop() {
if (isEmpty()) {
System.out.println("栈是空的");
return null;
}
// pop
return stack[top--];
}
public boolean isFull() {
return (top == maxSize - 1);
}
public boolean isEmpty() {
return (top == -1);
}
/**
* 运算符优先级判断
*/
public int priority(int opera) {
int priority = -1;
char[] operas = {'*', '/', '+', '-'};
if (opera == operas[0] || opera == operas[1]) {
priority = 1;
} else if (opera == operas[2] || opera == operas[3]) {
priority = 0;
}
return priority;
}
/**
* 判断是不是一个运算符
*/
public boolean isOpera(char val) {
return ((val == '+') || (val == '-') || (val == '*') || (val == '/'));
}
public int cal(int num1, int num2, int opera) {
int result = 0;
switch (opera) {
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
default:
break;
}
return result;
}
}
测试类
public class CalculatorDemo {
public static void main(String[] args) {
String expression = "700+20*6-4";
// 创建两个栈
Stack numStack = new Stack(10);
Stack operaStack = new Stack(10);
int index = 0;
int num1;
int num2;
int oper;
int result;
char ch;
StringBuilder keepNum = new StringBuilder();
// 扫描
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
// 判断扫描的是什么
// 如果是符号
if (operaStack.isOpera(ch)) {
// 判断符号栈里有没有元素
if (!operaStack.isEmpty()) {
// 处理
// 拿到栈顶和新元素的优先级
int topPriority = operaStack.priority(operaStack.peek());
int chPriority = operaStack.priority(ch);
// 比较
if (chPriority < topPriority) {
// 如果优先级小于栈顶
// 先计算栈顶也就是
num1 = numStack.pop();
num2 = numStack.pop();
oper = operaStack.pop();
result = operaStack.cal(num1, num2, oper);
// 运算后把结果入数栈
numStack.push(result);
operaStack.push(ch);
keepNum.delete(0, keepNum.length());
} else {
// 如果小于直接入栈
operaStack.push(ch);
}
} else {
// 直接入栈
operaStack.push(ch);
}
} else {
// 如果是数字,直接入数栈,但是考虑到编码问题,char转int会增加48
// 此时考虑多位数
keepNum.append(ch);
// 如果ch是expression的最后一位
if (expression.length() - 1 == index) {
// 此时直接入栈
numStack.push(Integer.parseInt(keepNum.toString()));
} else {
// 判断下一个字符是不是符号
if (operaStack.isOpera(expression.substring(index + 1, index + 2).charAt(0))) {
// 下一位是运算符时push
numStack.push(Integer.parseInt(keepNum.toString()));
// 清空keepNum
keepNum.delete(0, keepNum.length());
}
}
}
// index右移并判断结束
if (++index >= expression.length()) {
break;
}
}
// 循环运行完毕,顺序的从两个栈中取对应的元素进行运算
while (true) {
if (operaStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operaStack.pop();
result = operaStack.cal(num1, num2, oper);
// 运算后把结果入数栈
numStack.push(result);
}
// 结束
System.out.println("表达式" + expression + "的结果是 ==> " + numStack.pop());
}
}
标签:numStack,java,int,return,用栈,result,operaStack,完整版,public 来源: https://www.cnblogs.com/coderDreams/p/16066575.html