综合计算器(栈实现)

描述

输入一串字符,计算结果,例如1+2*3-23,计算结果

解决思路

  1. 遍历表达式,区分数字、字符,并分别入到不同的栈;
  2. 如果是数字,直接入栈
  3. 如果是字符
    • 字符栈为空或是当前符号优先级高于字符栈栈顶符号,直接入栈
    • 否者就是当前符号优先级低于字符栈栈定符号,需要将数字栈出栈俩个数字,字符栈出栈一个字符,进行运算,结果入到数字栈,一尺类推,
  4. 最后顺序将数字、字符栈中的元素计算
  5. 最后数字栈中的唯一元素就是结果

实现

  1. 栈的实现
public class MyStack {
    private int maxSize;
    private int top = -1;
    private Object[] array;

    public MyStack(int maxSize) {
        this.maxSize = maxSize;
        array = new Object[maxSize];
    }

    public void push(Object object) {
        if (top == maxSize) {
            throw new RuntimeException("超出最大栈深度");
        }
        array[++top] = object;
    }

    public Object peek() {
        return array[top];
    }

    public Object pop() {
        if (top <= -1) {
            throw new RuntimeException("已到栈底");
        }
        Object cur = array[top];
        top--;
        return cur;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void show() {
        for (int i = 0; i <= top; i++) {
            System.out.println(array[i]);
        }
    }
}
  1. 计算实现

/**
 * 字符串计算
 */
public class StrCalculation {

    public static void main(String[] args) {
        String str = "2*3+11-5+8*5";
        StrCalculation sc = new StrCalculation();
        sc.calculation(str);
    }

    public boolean compareChar(char char1, char char2) {
        if (char1 == '*' || char1 == '/') {
            if (char2 == '+' || char2 == '-') {
                return true;
            } else {
                return false;
            }
        } else {
            if (char2 == '*' || char2 == '/') {
                return false;
            } else {
                return true;
            }
        }
    }

    public void calculation(String str) {
        MyStack numStack = new MyStack(100);
        MyStack charStack = new MyStack(100);
        String num = "";
        //区分数字字符入栈
        for (int i = 0; i < str.length(); i++) {
            if (!isNum(str.charAt(i))) {
                //符号入栈
                if (charStack.isEmpty()) {
                    charStack.push(str.charAt(i));
                } else if (compareChar((Character) charStack.peek(), str.charAt(i))) {
                    int num1 = Integer.parseInt(numStack.pop() + "");
                    int num2 = Integer.parseInt(numStack.pop() + "");
                    char oper = (char) charStack.pop();
                    int cal = cal(num1, num2, oper);
                    numStack.push(cal);
                    charStack.push(str.charAt(i));
                } else {
                    charStack.push(str.charAt(i));
                }
            } else {
                //数字入栈
                if (i == str.length() - 1) {
                    numStack.push(str.charAt(i));
                } else if (isNum(str.charAt(i + 1))) {
                    num += str.charAt(i);
                    continue;
                } else {
                    num += str.charAt(i);
                    numStack.push(num);
                    num = "";
                }
            }
        }
        while (true) {
            if (charStack.isEmpty()) {
                break;
            }
            int num1 = Integer.parseInt(numStack.pop() + "");
            int num2 = Integer.parseInt(numStack.pop() + "");
            char oper = (char) charStack.pop();
            int cal = cal(num1, num2, oper);
            numStack.push(cal);
        }
        System.out.println(numStack.peek());

    }

    public int cal(int num1, int num2, char oper) {
        int resul = 0;
        switch (oper) {
            case '*':
                resul = num1 * num2;
                break;
            case '-':
                resul = num2 - num1;
                break;
            case '+':
                resul = num1 + num2;
                break;
            default:
                break;
        }
        return resul;
    }

    public boolean isNum(char c) {
        return Character.isDigit(c);
    }
}

这里只是个大概实现,比如定义运算优先级、运算等都是取了几个符号作为代表,可以自行添加其他符号。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容