利用数组构建栈--实现一个计算器(版本一:只适用于个位数运算(不适合多位数计算))

package com.xurong.stack;
public class Calculater {

	public static void main(String[] args) {
		String expression = "3+2*6-2";
		ArrayStack2 numStack = new ArrayStack2(10);//存放数据的stack
		ArrayStack2 operStack = new ArrayStack2(10);
		int index = 0;
		int num1 = 0;
		int num2 = 0;
		int oper = 0;
		int res = 0;
		char ch = ' ';//将每次扫描得到的char保存到ch
		//会循环的取出expression字符
		while (true) {
			//扫描expression
			ch = expression.substring(index, index + 1).charAt(0);
			/*
			 * 逐个取出字符串中的每一个字符,并将其转变为char
			 * substring():String
			 * charAt():char
			 */
			if (operStack.isOper(ch)) {//如果是操作符
				if (!operStack.isEmpty()) {
					/*
					 * 如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符
					 * 就需要从数栈中pop处两个数,在符号栈中pop处一个符号,进行运算,将得到结果,入数栈,
					 * 然后将当前的操作符入符号栈
					 */
					if (operStack.priority(ch) <= operStack.priority(operStack.peak())) {
						num1 = numStack.pop();
						num2 = numStack.pop();
						oper = operStack.pop();
						res = numStack.cal(num1, num2, oper);
						//入栈数字
						numStack.push(res);
						//把当前ch入符号栈
						operStack.push(ch);
					} else {
						//如果当前的符号的优先级大于符号栈顶的符号优先级,直接入栈
						operStack.push(ch);
					}
				}else {
					//没符号,符号直接输入栈中
					operStack.push(ch);
				}
			} else {
				numStack.push(ch - 48);//'1' = 49,其与真实值相差48

			}
			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);//将计算结果入栈
		}
		//将数字栈的最后结果pop
		int res2 = numStack.pop();
		System.out.printf("表达式%s = %d",expression,res2);
	}

}


//定义一个ArrayStack表示用数组模拟栈
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];
	}
	
	//栈满
	public boolean isFull() {
		return top == maxSize - 1;
	}
	
	//栈空
	public boolean isEmpty() {
		return top == -1;
	}
	
	//入栈-push
	public void push(int value) {
		//先判断栈是否为满
		if (isFull()) {
			System.out.println("栈满");
			return;
		}
		top++;
		stack[top] = value;
	}
	
	//出栈-pop,将栈顶的数据返回
	public int pop(){
		//先判断栈是否为空
		if (isEmpty()) {
			//System.out.println("栈空,无法删除");
			throw new RuntimeException("栈空,没有数据-");
		}
		int value = stack[top];
		top--;
		return value;
		
	}
	public void list() {
		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 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 result = 0;//用于储存数据
		switch (oper) {
		case '+':
			result = num1 + num2;
			break;
		case '-':
			result = num2 - num1;//注意栈的存数顺序,需要用后出来的数减去前一个数
			break;
		case '*':
			result = num1 * num2;
			break;
		case '/':
			result = num2 / num1;
			break;

		default:
			break;
		}
		return result;
	}
	
	//增加一个方法,返回栈顶的值,区别于pop
	public int peak() {//char与int在一定调间下没区别
		return stack[top];
	}
	
}

运算结果:

表达式3+2*6-2 = 13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值