算法和数据结构——栈的应用(计算器)

package com.structure.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class StackActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stack);
        //定义相关变量
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);
        String expression = "130+2*5-6";
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum = "";
        //依次得到expression的每一个字符
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            //判断是不是运算符
            if (operStack.isOprea(ch)) {
                //判断符号栈是不是空的
                if (!operStack.isEmpty()) {
                    // 如果操作符栈中有操作符,就进行比较操作符的优先级,如果当前操作符小于或等于栈顶的操作符,就从数栈中pop出两个数
                    //再从符号栈中pop出一个符号,进行运算,将得到的结果入数栈,然后将当前的操作符号入栈
                    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 {
                //如果是数直接入数栈
                keepNum += ch;
                //如果ch已经是expression的最后一位就直接入栈
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    if (operStack.isOprea(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);
        }
        Log.i("tag", "最后的结果为" + res);
    }
}

class ArrayStack {
    private int maxSize;
    private int top = -1;
    private int[] stack;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    /**
     * 判断栈是不是空的
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 判断栈是不是满的
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 入栈
     */
    public void push(int value) {
        if (isFull()) {
            Log.i("tag", "栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    /**
     * 出栈
     */
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int res = stack[top];
        top--;
        return res;
    }

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

    /**
     * 判断符号的优先级
     */
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        }
        return -1;
    }

    /**
     * 计算的方法
     */
    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 = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
        }
        return res;
    }

    /**
     * 获取栈顶的元素
     */
    public int peek() {
        return stack[top];
    }

}

你可能感兴趣的:(算法和数据结构——栈的应用(计算器))