java在栈中使用中缀表达式实现不带括号的加减乘除运算

day4

package stack;

//中缀表达式
public class Calculator {
	public static void main(String[] args) {
		String expression = "100+2*6-2";// 这里有个问题,只能处理多位数

		// 创建两个栈,一个数栈,一个运算符栈
		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 = ' ';// 将每次扫描得到的char保存到ch
		String keepNum = "";// 用于拼接多位数
		// 开始用while语句循环扫描experssion
		while (true) {
			// 这里.substring(index, index + 1)返回了长度为1的一个子字符串
			// 然后使用charat找到字符串中的第一个字符
			ch = expression.substring(index, index + 1).charAt(0);
			// 判断ch是什么 做相应处理
			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);//对应ASCLL码表1的值为49
				// 当处理多位数时不能立即入栈
				// 需要向后一位继续扫描如果是符号则入栈,如果不是继续入栈 因此我们需要定义一个定义一个字符串变量用于拼接
				keepNum += ch;
				// 如果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;
			}
		}

		// 当表达式扫描完毕,就顺序的从数栈和符号栈中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);// 结果入栈
		}
		// pop出最后一个数栈中的数,这个数就是结果
		System.out.printf("表达式%s = %d", expression, numStack.pop());
	}
}

//先创建一个栈

//定义一个类表示栈结构
class ArrayStack2 {
	private int maxSize;// 定义栈的最大容量
	private int[] stack;// 数组模拟栈,数据就放在该数组里
	private int top = -1;// 表示栈顶 初始化为 -1

	// 构造器
	public ArrayStack2(int maxSize) {
		this.maxSize = maxSize;
		stack = new int[this.maxSize];
	}

	// 返回当前栈顶的值不出栈
	public int peek() {
		return stack[top];
	}

	// 判断栈满
	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("栈空");// 方法被终止且runtime异常可以不需要被捕获
		}
		int value = stack[top];
		top--;
		return value;
	}

	// 显示栈的情况,遍历时需要从栈顶开始显示数据
	public void list() {
		if (isEmpty()) {
			System.out.println("栈空");// 方法被终止且runtime异常可以不需要被捕获
		}

		for (int i = top; i >= 0; i--) {
			System.out.printf("stack[%d] = %d \n", i, stack[i]);
		}
	}

	// 返回运算符的优先级,使用数字表示,数字越大优先级越高
	public int priority(int oper) {// int 和 char可以混用 char本质也是数字
		if (oper == '*' || oper == '/') {
			return 1;
		} else if (oper == '+' || oper == '-') {
			return 0;
		} else {
			return -1;
		}

	}

	// 判断是否是一个运算符
	public boolean isOper(char val) { // 也可以按照int来比
		return val == '+' || val == '-' || val == '*' || val == '/';
	}

	// 计算方法
	public int cal(int num1, int num2, int oper) {// 这里oper也可以用char定义
		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;

		}
		return res;

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值