JAVA数据结构 - 栈与中缀表达式,后缀表达式实现简单计算器

先进后出的数据结构,入栈,出栈 , 定义一个指针始终指向栈顶,没有数据时为-1

 

1.使用数组模拟实现栈-定义结构

class ArrayStack {
    private int MaxSize;   //表示栈的最大空间
    private int[] array;    //表示存放数据的数组
    private int top = -1;    //表示栈顶,起始位置为-1表示无数据

    public ArrayStack(int maxSize) {    初始化栈
        MaxSize = maxSize;
        array = new int[maxSize];
}}

2.栈的方法

   1.有数据时 top从0开始   所以 top=maxSize -1 时为满               2.top=-1时为空

   3.入栈时,将top上移,然后将数据添加到top指向位置             4.出栈时 将top下移,数据并没有真实出栈,只是top指向栈顶数据变化

   5.遍历栈时, 从栈顶开始遍历

    public Boolean isFull() {         当栈顶为最大空间-1时满,因为top从0开始
        return top == MaxSize - 1;
    }

    public Boolean isEmpty() {      当top=-1时,栈为空
        return top == -1;
    }

  添加一个数据, 入栈
    public void push(int num) {
        if (isFull()) {
            System.out.println("栈为满,不能添加~~");
            return;
        }
        top++;
        array[top] = num;        }

    * 弹出栈顶数据, 出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空,不能出栈~!");
        }
        int value = array[top];
        top--;
        return value;            }

二.中缀表达式与栈实现 简单计算器

  1.前缀表达式   (3+4)×5-6 对应的前缀表达式就是 - × + 3 4 5 6

     .从右往左扫描表达式  遇到数字入栈 ,遇到符号 弹出 俩个数字   number1  number2       使用 number1 运算符 number2

   2.中缀表达式   缀表达式就是常见的运算表达式,如(3+4)×5-6   人们常用计算方法

   3.后缀表达式   (3+4)×5-6 对应表达式就 3 4 + 5 × 6

      .从左往右扫描表达式  遇到数字入栈 ,遇到符号 弹出 俩个数字   number1  number2       使用 number2 运算符 number1

2.中缀表达式计算 大致思路

  1.准备两个栈      1. 存放数据栈       2.存放符号栈        

  2.若果是符号:  1.如果符号栈为空 或者 当前符号优先级大于 栈顶符号的优先级  则直接入栈     

                          2.如果 当前符号优先级小于 栈顶符号的优先级  则取出数据栈两个数字 num1, num2  取出一个运算符

                          做运 算 num2 运算符 num1,  得到运算结果入数据栈, 并将新符号入栈

3.最后 扫描完成,依次  取出两个数据 一个运算符 做运算,将结果入栈

 留在数据栈中的数字就为最后计算结果

列如表达式  expression = "3+2*3-1";   

  1.第一次扫描  数据栈   为3 ,2,3         符号栈为+ ,*        2.扫描到-时,  优先级小于*    取出数据栈两个数字运算得到6    然后入栈

  3.数据栈   为3,6 ,1       符号栈为+  ,-                       

   5.依次  取出两个数据 一个运算符 做运算,将结果入栈         6.最后数据栈数字为8

三.后缀表达式与栈实现 简单计算器

 1.后缀表达式更适合计算器计算,  而中缀表达式更适合人类计算    后缀表达式也叫   逆波兰表达式

  2.如下:      中缀表达式    (30+4)*5-6    转换为后缀  "30 4 + 5 * 6 -"          并将每个字符放入集合      [3, 4, +, 5, *, 6, -]

public List<String> getArrayList(String expression) {

        List<String> expresList  = new ArrayList<>();

        String[] s = expression.split(" ");
        for (String s1 : s) {
            expresList.add(s1);
        }
        return expresList;   }

3.遍历 [3, 4, +, 5, *, 6, -]  计算, 使用java 提供的 栈

   1.如果是数字直接入栈       2.如果是符号 取出栈中两个数字  与当前符号计算  并将结果入栈   3.最终栈中留下一个数字为结果

private int Calculator(List<String> arrayList) {
        Stack<String> stack = new Stack<>();
        for (String item : arrayList) {
            if (item.matches("\\d+")){
                stack.push(item);
            }else {
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int res= 0;
                if (item.equals("+")){
                    res=num2+num1;
                }else if (item.equals("-")){
                    res=num2-num1;
                }else if (item.equals("*")){
                    res=num2*num1;
                }else if (item.equals("/")){
                    res=num2/num1;
                }else {
                    throw new RuntimeException("符号错误!");
                }
                stack.push(res+"");
            }}
        return Integer.parseInt(stack.pop());   }     29

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章