Java数据结构 利用数组模拟栈实现综合计算器

栈的核心简介

stack, 是一个先入后出(FILO-First In Last Out) 的有序列表, 是一个带限制的线性表,对于该表中的插入和删除只能在栈顶这一端进行, 而另一端为固定的一端, 称为栈底
出栈使用 pop()
入栈使用 push()

栈的应用场景

1. 子程序的调用:

在跳往子程序前, 会向将下一个指令的地址存到堆栈中, 直到子程序执行完后再将地址取出, 以回到原来的程序中

2. 处理递归调用

和子程序的调用类似, 只是除了存储下一个指令的地址外, 也将参数, 区域变量等数据存入堆栈中

3. 表达式的转换与求值

中缀表达式 转为 后缀表达式

// TODO

4. 二叉树的遍历

// TODO

5. 图形的深度优先(depth-first)搜索

// TODO

利用数组模拟栈实现综合计算器

package com.beyond.stack;

public class Calculator {
    
    
	public static void main(String[] args) {
    
    
		String expression = "300+2*5-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 (true) {
    
    
			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); // '1' => 1
				
				
				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 + 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.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;
		this.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 show() {
    
    
		if (isEmpty()) {
    
    
			System.out.println("栈空~~");
			return;
		}
		int x = top;
		while (true) {
    
    
			if (x < 0) {
    
    
				break;
			}
			System.out.println(stack[x]);
			x--;
		}
	}

	// 返回运算符的优先级
	public int priority(int oper) {
    
    
		if (oper == '*' || oper == '/') {
    
    
			return 1;
		} else if (oper == '+' || oper == '-') {
    
    
			return 0;
		} else {
    
    
			return -1;
		}
	}

	// 判断是不是一个运算符
	public boolean isOper(char value) {
    
    
		return value == '+' || value == '-' || value == '*' || value == '/';
	}

	// 瞅瞅栈顶元素的值
	public int peek() {
    
    
		return stack[top];
	}

	// 计算方法
	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;
		}
		return res;
	}
}

猜你喜欢

转载自blog.csdn.net/Beyond_Nothing/article/details/111831407
今日推荐