【数据结构】栈实现计算器

1.用栈实现计算器:

public class test {
          
   
    public static void main(String[] args) {
          
   
        String e = "70+2*6-4";
        Stack num = new Stack(10);
        Stack operate = new Stack(5);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch =  ;
        String keepNum = "";
        while (true){
          
   
            ch = e.substring(index,index+1).charAt(0);
            if (num.isOper(ch)){
          
   
                if (operate.isEmpty()){
          
   
                    operate.push(ch);
                }
                else{
          
   
                    if (num.caprty(ch)<=num.caprty(operate.getTop())){
          
   
                        num1 = num.pop();
                        num2 = num.pop();
                        oper = operate.pop();
                        res = num.cault(num2,num1,oper);
                        num.push(res);
                        operate.push(ch);
                    }
                    else{
          
   
                        operate.push(ch);

                    }

                }
            }else{
          
   
                keepNum+=ch;
                if (index==e.length()-1){
          
   
                    num.push(Integer.parseInt(keepNum));
                }else{
          
   
                if (num.isOper(e.substring(index+1,index+2).charAt(0))){
          
   
                    num.push(Integer.parseInt(keepNum));

                    keepNum = "";
                }

            }}
            index++;
            if (index>=e.length()){
          
   
                break;
            }
        }

        while (true){
          
   
            if (operate.isEmpty()){
          
   
                break;
            }
            num1 = num.pop();
            num2 = num.pop();
            oper = operate.pop();
            res = num.cault(num2,num1,oper);
            num.push(res);

        }

        System.out.println(e+"="+num.pop());




    }
}

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

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

    }

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

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

    public int getTop(){
          
   
        return stack[top];
    }

    public void push(int e){
          
   
        if (isFull()){
          
   
            System.out.println("满了");
            return;
        }
        top++;
        stack[top] = e;
    }

    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.println(stack[i]);
       }
    }

    public int caprty(int oper){
          
   
        if (oper==*||oper==/){
          
   
            return 1;
        }
        if (oper==+||oper==-){
          
   
            return 0;
        }
        return -1;
    }

    public boolean isOper(char oper){
          
   
        return oper==*||oper==/||oper==+||oper==-;

    }

    public int cault(int num,int num1,int oper){
          
   
        int res = 0;
        switch (oper){
          
   
            case +:
                res = num + num1;
                break;
            case -:
                res = num - num1;
                break;
            case *:
                res = num * num1;
                break;
            case /:
                res = num / num1;
                break;
            default:
                break;

        }
        return res;
    }


}
经验分享 程序员 微信小程序 职场和发展