栈实现综合计算器
1、思路
使用栈完成表达式的计算思路
1.通过一个index值(索引),来遍历表达式;
2.如果发现是一个数字,就直接入数栈;
3.如果发现扫描到是一个符号,就分如下情况:
①如果发现当前的符号栈为空,就直接入栈;
②如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符,就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果入数栈,然后将当前的操作符入符号栈;如果当前的操作符的优先级大于栈中的操作符,就直接入符号栈。
4.当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行;
5.最后在数栈只有─个数字,就是表达式的结果
例如:计算3+2*6-2=?
2、代码编写
(1)先创建一个栈
//先创建一个栈
class ArrayStack2 {
private int maxSize;//栈的大小
private int[] stack;//用数组模拟栈
private int top = -1;//top表示栈顶,初始化为-1
//构造器
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
...
}
(2)判断是否栈满
//栈满
public boolean isFull() {
return top == maxSize - 1;
}
(3)判断是否栈空
//栈空
public boolean isEmpty() {
return top == -1;
}
(4)入栈
//入栈
public void push(int data) {
if (isFull()) {
System.out.println("栈满");
return;
} else {
top++;
stack[top] = data;
}
}
(5)出栈
//出栈
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
} else {
int value = stack[top];
top--;
return value;
}
}
(6)遍历栈,从栈顶开始显示数据
//遍历栈,从栈顶开始显示数据
public void show(){
if(isEmpty()){
System.out.println("栈空,无法遍历数据");
return;
}
for (int i = top; i >= 0 ; i--) {
System.out.println(stack[i]);
}
}
(7)返回当前栈顶的值
public int getTop(){
return stack[top];
}
(8)返回运算符的优先级,优先级使用数字表示,数字越大,则优先级越高
//返回运算符的优先级,优先级使用数字表示,数字越大,则优先级越高
public int priority(int operation) {
if (operation == '*' || operation == '/') {
return 1;
} else if (operation == '+' || operation == '-') {
return 0;
} else {
return -1;//假定目前的表达式只有+.-.*./
}
}
(9)判断是否为一个运算符
//判断是否为一个运算符
public boolean isOperation(int value) {
return value == '+' || value == '-' || value == '*' || value == '/';
}
(10)计算方法
//计算方法
public int calculate(int num1, int num2, int operation) {
int result = 0;
if(num2 == 0){
throw new ArithmeticException("除数不能为0");
}
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
default:
break;
}
return result;
}
(11)主程序完成表达式的运算
public static void main(String[] args) {
//主程序完成表达式的运算
String expression = "3+2*6-2";
//创建数栈和符号栈
ArrayStack2 numStack2 = new ArrayStack2(10);
ArrayStack2 operStack2 = new ArrayStack2(10);
int index = 0;//用于扫描
int num1 = 0;
int num2 = 0;
int operation = 0;
int result = 0;
char ch = ' ';//每次扫描得到char保存到ch
//while循环扫描表达式
while (true) {
/*if (ch == '\n') {
break;
}*/
ch = expression.substring(index, index + 1).charAt(0);//依次得到表达式的每一值
if (numStack2.isOperation(ch)) {//如果是运算符
if (operStack2.isEmpty()) {//如果当前符号栈为空,直接入符号栈
operStack2.push(ch);
} else {//如果当前符号栈不为空
if (operStack2.priority(ch) > operStack2.priority(operStack2.getTop()))//①如果当前操作符的优先级大于栈中操作符,直接入符号栈
{
operStack2.push(ch);
} else { //②如果当前的操作符的优先级小于或者等于栈中的操作符,就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果入数栈,然后将当前的操作符入符号栈
num1 = numStack2.pop();
num2 = numStack2.pop();
operation = operStack2.pop();
result = operStack2.calculate(num1, num2, operation);
numStack2.push(result);
operStack2.push(ch);
}
}
}
else {
numStack2.push(ch - 48);//'1'=>1
}
index++;
if(index >= expression.length()){
break;
}
}
while (true){
if(operStack2.isEmpty()){//如果运算符栈为空,说明全部运算结束,数栈存有最后结果
break;
}
num1 = numStack2.pop();
num2 = numStack2.pop();
operation = operStack2.pop();
result = numStack2.calculate(num1,num2,operation);
numStack2.push(result);
}
System.out.println(expression+"表达式计算结果:"+numStack2.pop());
}
3、运行结果
(1)计算3+26-2
(2)计算4/2+26-2+7
4、存在问题
如下图所示,当处理多位数时,不能发现是一个数就立即入栈,需要向表达式expression的index后再看一位,如果是数就继续扫描,如果是符号才入栈。
5、改进
5.1 思路
定义一个字符串变量,用于拼接,进而来处理多位数。
else {
//定义一个字符串变量,用于拼接,向表达式expression的index后再看一位,如果是数就继续扫描,如果是符号才入栈。
keepNum += ch;
//如果ch已经是表达式的最后一位,就直接入栈
if(index == expression.length()-1){
numStack2.push(Integer.parseInt(keepNum));
}
else {
if (operStack2.isOperation(expression.substring(index + 1, index + 2).charAt(0))) {
numStack2.push(Integer.parseInt(keepNum));//'1'=>1
//keepNum清空
keepNum = "";
}
}
}
5.2 运行结果
(1)表达式含有两位数
(2)表达式含有三位数