栈实现表达式计算

在这里插入图片描述

package com.pro.stack;

public class CalculatorTest {

	public static void main(String[] args) {
		String expression = "7*2*2-5+1-5+3-4";
		// 创建两个栈,数栈,一个符号栈
		CalculatorStack numStack = new CalculatorStack(10);
		CalculatorStack operStack = new CalculatorStack(10);
		// 定义相关变量
		int index = 0;// 用于扫描
		int num1 = 0;
		int num2 = 0;
		int oper = 0;
		int res = 0;
		int ch = ' ';// 将每次扫描得到的char保存到ch
		String keepNum="";//用于拼接多位数
		while (true) {
			// 依次得到expression的每一个字符
			ch = expression.substring(index, index+1).charAt(0);
			
			// 判断ch是什么,然后做相应处理
			if (operStack.isOper((char) ch)) {// 如果是运算符
				// 判断当前符号栈是否为空,不为空就进行比较操作符的优先级
				if (!operStack.isEmpty()) {
					// 若优先级小于或等于当前栈中操作数的优先级,就需要pop出两个数,
					// 并从符号栈中pop出一个符号,进行运算,将的得到结果,入数栈,然后将当前的操作符入栈
					if (operStack.prioty(ch) <= operStack.prioty(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);// 1+3
				}
			} else {// 如果是数,直接入数栈
//				numStack.push(ch - 48);
				
				//处理多位数
				
				keepNum+=(ch-48);
				
				//如果ch已经是expression的最后一位,就直接入栈
				if (index == expression.length() - 1) {
					numStack.push(Integer.parseInt(keepNum));
				}else{
				
					//判断下一个字符是不是数字,如果是数字,就继续扫描,如果是运算符,则入栈
					//注意是看后一位,不是index++
					if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))) {
						//如果后一位是运算符,则入栈 keepNum = "1" 或者 "123"
						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.println("表达式" + expression + "=" + numStack.pop());

	}

}
package com.pro.stack;

public class CalculatorStack {
	private int maxSize;//栈的大小
	private int[] stack;//数组,数组模拟栈,数据就放在该数组中
	private int top=-1;//top表示栈顶,初始化为-1
	
	//构造
	public CalculatorStack(int maxSize) {
		this.maxSize=maxSize;
		stack=new int[this.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 int peek() {
		return stack[top];
	}
	
	//遍历栈
	public void list() {
		if (isEmpty()) {
			System.out.println("栈空,没有数据~~");
			return;
		}
		for (int i = top; i >=0; i--) {
			System.out.println("stack["+i+"]="+stack[i]);
		}
	}
	
	//返回运算符的优先级,优先级有程序员确定.优先级使用数字表示,
	//数字越大,则优先级越高
	 public int prioty(int oper) {
		if(oper=='*'||oper=='/'){
			return 1;
		}else if (oper=='+'||oper=='-') {
			return 0;
		}else{
			return -1;
		}
	}
	
	//判断是不是元素符
	 public boolean isOper(char ch) {
		return ch=='+'||ch=='-'||ch=='*'||ch=='/';
	}
	
	 //计算方法
	 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;
	} 
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值