数据结构和算法:打卡第八天

八、栈实现综合计算器

1、问题提出

请输入一个表达式:【7*2*2-5+1-5+3-3】点击计算

2、思路分析

  1)通过一个index值,来遍历表达式

  2)如果发现是一个数字就直接入数栈

  3)如果发现扫描到的是一个符号,就分以下三种情况:

        a. 如果当前符号栈为空,就直接入栈

        b. 如果当前符号栈中有操作符,就进行比较:

                ① 如果当前的操作符的优先级小于或者等于栈中的操作符的优先级,就需要从数栈中pop出两个数,在符号栈中pop出一个符号,进行运算,将得到的结果入数栈,然后将当前的操作符直接入符号栈。

                ② 如果当前的操作符的优先级大于栈中的操作符的优先级,将当前的操作符直接入符号栈。

  4)当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行

  5)最后在数栈中只有一个数字,就是表达式的结果

3、代码实现

package stack;

public class Calculator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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 = ' ';
		String keepNum = "";
		while(true){
			ch = expression.substring(index, index+1).charAt(0);
			if(operStack.isOper(ch)){
				if(!operStack.isEmpty()){
					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{
				//numStack.push(ch - 48);
				keepNum += ch;
				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 = "";
					}
				}
			}
			index++;
			if(index >= expression.length()){
				break;
			}
		}
		while(true){
			if(operStack.isEmpty()){
				break;
			}
			num1 = numStack.pop();
			num2 = numStack.pop();
			oper = operStack.pop();
			res =  numStack.cal(num1, num2, oper);
			numStack.push(res);
		}
		int res2 = numStack.pop();
		System.out.printf("表达式:%s = %d", expression, res2);
	}

}
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;
	}
	public int pop(){
		if(isEmpty()){
			throw new RuntimeException("栈已空");
		}
		int value = stack[top];
		top--;
		return value;
	}
	public void showStack(){
		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 peek(){
		return stack[top];
	}
	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;
		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;
	}
}








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值