java实现简单计算器

计算(5*(4+4-3)/5+6)-10*(5+2-4)

package com.briup.algorithm.Stack.apply;

/**
 * @author 六月
 * @date 2022-08-13 11:26
 * @package com.briup.algorithm.Stack.apply
 * @title
 */
public class Calculator {
    public static void main(String[] args) {
        //设置表达式
        String expression = "(5*(4+4-3)/5+6)-10*(5+2-4)";
        //设置数组栈和符号栈
        StackArray numStack = new StackArray(10);//数字栈
        StackArray operStack = new StackArray(10);//符号栈
        //设值锁需要的值
        int index = 0;//将来用来做切割字符串的索引 和用来终止死循环
        int num1 = 0;//需要操作的数1 未来从数字栈弹出
        int num2 = 0;//需要操作的数2 未来从数字栈弹出
        int result = 0;//操作数运算的结果 未来需要重新入栈
        int oper = 0;//用来存放运算符/操作符 未来从符号栈弹出
        char ch = ' ';//扫描是用来存储expression的字符
        String s = "";//用来拼接多位数 注:使用是一定要使用字符对它进行初始化

        //写个死循环 符号栈和字符栈操作
        //目的是:把数字栈和符号栈分别存放好
        while (true){
            //依次得到expression的每个字符
            //substring(int begin,int end)获取从begin到end的字符串 返回的是字符串
            //String s;s.charAt(int i) 获取字符串s的第i个字符 返回的是一个字符
            ch=expression.substring(index,index+1).charAt(0);
            //判断是否是运算符
            if (operStack.isOper(ch)){
                if (operStack.isEmpty()){
                    operStack.push(ch);
                }else {
                    //如果是(得直接入栈 然后根据优先级的不同弹栈
                    if (ch=='('){
                        operStack.push(ch);
                    }else if (ch==')'){
                        //把(前的运算符全部弹出栈
                        while (!((operStack.peek()) =='(')){
                            //弹出两个数字和一个操作符
                            num1 = numStack.pop();
                            num2 = numStack.pop();
                            oper = operStack.pop();
                            //将运算结果重新弹入数字栈
                            numStack.push(numStack.cal(num1,num2,oper));
                        }
                        //最后把(弹出
                        operStack.pop();
                    }else {
                        //判断入栈的优先级 如果入栈的优先级小于或等于运算符栈的优先级
                        //数字栈弹出两个数字 操作符弹出一个操作符
                        //然后进行运算后重新弹入数字栈
                        if (operStack.priority(ch)<=operStack.priority(operStack.peek())){
                            num1=numStack.pop();
                            num2=numStack.pop();
                            oper=operStack.pop();
                            numStack.push(numStack.cal(num1,num2,oper));
                            operStack.push(ch);
                        }else {
                            operStack.push(ch);
                        }

                    }
                }
            }else {
                s=s+ch;
                if (index == expression.length()-1){
                    //如果ch已经是我们expression中的最后一位那么我们就直接入栈
                    numStack.push(Integer.parseInt(s));
                }else{//当前的运算符不是expression的最后一位
                    if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))){//如果后一位是运算符,则入栈
                        numStack.push(Integer.parseInt(s));
                        s="";
                    }
                }
            }
            index++;
            //终止死循环
            if (index==expression.length()){
                break;
            }
        }

        //将第一次循环后 数字栈剩下的 和 操作符栈剩余的进行运算
        while (true){
            if(operStack.isEmpty()){
                //当我们的符号栈为空的时候则计算到最后的结果,数栈中只有一个结果那就是我们的结果
                break;
            }
            num1 =  numStack.pop();
            num2 =  numStack.pop();
            oper =  operStack.pop();
            result = numStack.cal(num1,num2,oper);
            numStack.push(result);
        }
        int endresult = numStack.pop();
        System.out.println(expression+"="+endresult);
    }
}
class StackArray {

    private int maxCapacity;
    private int[] stackArrays;
    private int stackTop = -1;

    public StackArray (int maxCapacity){
        /**
         *@description: 有参构造函数,用来进行一些初始化
         *@params: 栈的最大存储容量
         */
        this.maxCapacity = maxCapacity;
        this.stackArrays = new int[maxCapacity];
    }

    public boolean isFull (){
        /**
         *@description: 栈是否已经满了
         *@returns: 如果已经满了返回true,没有满的话返回false
         */
        return stackTop == maxCapacity-1;
    }

    public boolean isEmpty (){
        /**
         *@description: 判断当前的栈是否为空
         *@returns: 如果是空的话就返回true,否则返回false
         */
        return stackTop==-1;
    }

    public void push(int element){
        if(isFull()){
            System.out.println("栈满");
        }
        stackTop ++;
        stackArrays[stackTop] = element;
    }

    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("现在栈为空");
        }
        return stackArrays[stackTop--];
    }

    public int peek(){
        return stackArrays[stackTop];
    }

    public void list(){
        if(isEmpty()){
            System.out.println("栈空,没有数据");
        }
        for (int i = stackTop; i >=0 ; i--) {
            System.out.printf("stackArrays[%d]=%d\n",i,stackArrays[i]);
        }
    }

    //确定优先级
    public int priority(int oper){
        if(oper =='*'||oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

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

    //计算方法
    public int cal(int num1,int num2,int oper){
        int res = 0; // res 用于存放计算的结果
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;// 注意顺序
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }

}

在这里插入图片描述

附:substring()和charAt()用法

substring()

返回值是一个字符串

  • substring(int begin)
    默认到最后一个结束
    s

substring(5)的输出结果就是"world"

  • substring(int begin,int end)
    输出从begin到end-1间的字符串
    在这里插入图片描述

substring(5,in9)输出结果为worl

charAt()

返回值是一个字符在这里插入图片描述

“helloword”.charAt(5)的结果就是’w’

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值