[数据结构]栈实现综合计算器解决中缀表达式

实现利用栈结构实现计算器功能分析:

首先我们需要一个字符串类型的中缀表达式:"32250+25*6-265"

为了实现计算功能我们需要两个栈结构:一个用来存储数字,为数栈,另一个用来存储运算符号,为符号栈(这里因为讨论的是中缀表达式所以暂未考虑小括号等问题)。

1.通过一个index(索引),来遍历我们的表达式。

2.如果发现是数字,则直接入数栈。

3.如果发现是符号则分为一下两种情况

3.1当前符号栈为空,直接入栈。

3.2如果符号栈有操作符则进行比较,如果当前的操作符优先级小于或者等于(<=)栈中的操作符,就需要从数栈中pop出两个数,再从符号栈中pop出一个符号,进行运算,得到的结果入数栈,然后将当前的操作符入符号栈,如果当前的操作符优先级大于栈中的表达式,则直接入符号栈。

4.当前表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,运算。

5.最后留在数栈中的数就是表达式的结果。

接下来上代码:

package com.godv.stack;

public class ArrayStack {
	private int maxSize;
	private int[] stack;
	private int top = -1;

	public ArrayStack(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("栈满");
		}
		stack[++top]=value;
	}
	//出栈
	public int pop() {
		if(isEmpty()) {
			throw new RuntimeException("栈空");
		}
		return stack[top--];
	}
	//遍历
	public void list() {
		if(isEmpty()) {
			System.out.println("栈空");
		}
		for(int i = top;i>=0;i--) {
			System.out.printf("stack[%d]=%d\n",i,stack[i]);
		}
	}
	//返回运算符的优先级 自定义的优先级  只考虑吧+-*/部分
	public static int priority(int oper) {
		if(oper == '*'||oper=='/') {
			return 1;
		}else if(oper == '+'||oper=='-') {
			return 0;
		}else {
			return -1;
		}
	}
	//判断是否为运算符
	public static boolean isOper(char val) {
		return val=='+'||val=='-'||val=='*'||val=='/';
	}
	
	//计算方法 
	public static int cal(int num1,int num2,int oper) {
		int res = 0;
		switch (oper) {
		case '+':
			res = num1+num2;
			break;
		case '-':
			res = num1-num2;
			break;
		case '*':
			res = num1*num2;
			break;
		case '/':
			res = num1/num2;
			break;

		default:
			break;
		}
		return res;
	}
	//peek查看栈顶
	public int peek() {
		return stack[top];
	}

}
package com.godv.stack;

public class Calculator {
	public static void main(String[] args) {
		String expression = "32250+25*6-265";
		ArrayStack numStack = new ArrayStack(100);
		ArrayStack operStack = new ArrayStack(100);
		int index = 0;
		int num1 = 0;
		int num2 = 0;
		int oper = 0;
		char ch = ' ';
		while (true) {
			if (index >= expression.length()) {
				break;
			}
			ch = expression.substring(index, index + 1).charAt(0);
			if (ArrayStack.isOper(ch)) {
				// 是运算符
				if (!operStack.isEmpty()) {
					if (ArrayStack.priority(ch) <= ArrayStack.priority(operStack.peek())) {
						// 小于等于的话
						num2 = numStack.pop();
						num1 = numStack.pop();
						oper = operStack.pop();
						int res = ArrayStack.cal(num1, num2, oper);
						numStack.push(res);
						operStack.push(ch);
					} else {
						operStack.push(ch);
					}
				} else {
					//
					operStack.push(ch);
				}
			} else {
				int temp = index;
				System.out.println("index:"+index);
				int len = 1;
//				"30+2*6-2"
				while(temp<expression.length()-1&&!ArrayStack.isOper(expression.substring(temp+1,temp+2).charAt(0))) {
					len++;
					temp++;
				}
				System.out.println("长度:"+len);
				System.out.println("截取出来的:"+expression.substring(index,index+len));
				numStack.push(Integer.parseInt(expression.substring(index,index+len)));
				index+=(len-1);
			}
			index++;
		}
		while (true) {
			if (operStack.isEmpty()) {
				break;
			}
			num2 = numStack.pop();
			num1 = numStack.pop();
			oper = operStack.pop();
			int res = ArrayStack.cal(num1, num2, oper);
			numStack.push(res);
		}
		System.out.printf("计算式%s的结果为:%d", expression, numStack.pop());
	}

}

猜你喜欢

转载自blog.csdn.net/we1less/article/details/106148287
今日推荐