ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

栈——实现综合计算器(中缀表达式)

2021-08-31 15:00:06  阅读:200  来源: 互联网

标签:oper 中缀 num2 int res val 计算器 表达式 num1


代码如下:

public class Calculator {
    public static void main(String[] args) {
        String expression = "7*2-5*2";
        int length = expression.length();
        CalcuStack numStack = new CalcuStack(length);
        CalcuStack operStack = new CalcuStack(length);
        char[] chars = expression.toCharArray();
        String keepNum = "";
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        for (int i = 0; i < chars.length; i ++) {
            if (operStack.isOper(chars[i])) {
                operStack.list();
                if (!operStack.isEmpty()) {
                    if (operStack.priority(chars[i]) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(chars[i]);
                    } else {
                        operStack.push(chars[i]);
                    }
                } else {
                    operStack.push(chars[i]);
                }
            } else {
                int count = 0;
                for (int a = i; a + count < chars.length; count ++) {
                    if (operStack.isOper(chars[a + count])) {
                        i = i + count - 1;
                        break;
                    }
                    keepNum += chars[a + count];
                }
                numStack.push(Integer.parseInt(keepNum));
                keepNum = "";
            }
        }
        numStack.list();
        operStack.list();
        while (true) {
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }

        int res2 = numStack.pop();
        System.out.printf("%s=%d\n", expression, res2);
    }
}

class CalcuStack {
    private int maxSize;
    private int[] stack;
    private int top = -1;
//    构造函数
    public CalcuStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }
//    判断是否栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }
//    判断是否栈空
    public boolean isEmpty() {
        return top == -1;
    }
//    获取栈顶数据,不出栈
    public int peek() {
        return stack[top];
    }
//    入栈
    public void push(int data) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top ++;
        stack[top] = data;
    }
//    出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空,无数据");
        }
        int value = stack[top];
        top --;
        return value;
    }
//    遍历栈
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空,无数据");
            return;
        }
        for (int i = top; i >= 0; i --) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }
//    判断计算符优先级
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }
//    判断是否运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }
//    计算方法
    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 = num1 / num2;
                break;
            default:
                break;
        }
        return res;
    }
}

 

标签:oper,中缀,num2,int,res,val,计算器,表达式,num1
来源: https://www.cnblogs.com/mmql-bjz/p/15210369.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有