中缀表达式

中缀表达式就是是我们常用的加减乘除的方式

这种表达式的缺点:就是无法解决当带有括号时的计算表达式

// 简单的实现一个栈 没有使用到JDK自带的栈
public class ArrayStack {
    public int arr[];
    public int top;
    private int size;

    public ArrayStack() {
    }

    public ArrayStack(int stackSize) {
        this.size = stackSize;
        this.arr = new int[size];
        this.top = -1;
    }

    public  void  push(int value){
        if (isFull()){
            System.out.println("栈满!");
            return;
        }
        top ++;
        arr[top] = value;
    }

    public  int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈空!");
        }
        int value = arr[top];
        top --;
        return value;
    }

    public  void  show(){
      if (isEmpty()){
          System.out.println("栈空!");
      }
        for (int i =top; i >=0 ; i--) {
            System.out.printf("arr[%d]=%d \n",i,arr[i]);
        }
    }

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

    public  Boolean isFull(){
        return  top == size-1;
    }
}

// 继承实现的栈 以及封装中缀表达式的部分计算功能
public class Calculator extends ArrayStack {
    public Calculator() {
        super();
    }

    public Calculator(int stackSize) {
        super(stackSize);
    }

    //返回运算符的优先级
     public int priority( int operator){
        if(operator == '*' || operator == '/'){
            return 1;
        }
        else if (operator =='+' || operator == '-'){
            return 0;
        }
        return  -1;
     }

     // 判断是否是运算符
     public Boolean isOper(char val){
        if(val=='*' || val=='/' ||val=='+' ||val =='-')
          return true;
        else
          return false;
     }

     public  int cal(int num1 ,int num2,int operator){
         int res = 0;
         switch(operator){
             case '+':
                  res = num1 + num2;
                 break;
             case '-':
                  res = num2 - num1 ;
                 break;
             case '*':
                 res = num1 * num2;
                 break;
             case '/':
                 res = num2 / num1 ;
                 break;
         }
         return  res;
     }

     public  int peek(){
         return  arr[top];
     }
}
  
  // 实现计算结果功能以及测试代码
  public static void main(String[] args) {
        Calculator numStack = new Calculator(10);
        Calculator operStack = new Calculator(10);

        String expression = "7000*3*4-3*5";
        char value =' ';
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int res = 0;
        String  num ="";
       while (true){

           value = expression.substring(index, index +1).charAt(0);
            // 判断是否是符号
            if (numStack.isOper(value)) {
                // 第一次比较是否是空的栈
                if (operStack.isEmpty()) {
                    operStack.push(value);
                } else {
                    //否则进行比较运算符的大小
                    if (operStack.priority(operStack.peek()) >= operStack.priority(value)) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        res = operStack.cal(num1, num2, operStack.pop());
                        numStack.push(res);
                        operStack.push(value);
                    } else {
                        // 优先级比较高的 直接压栈
                        operStack.push(value);
                    }
                }

            } else {
                num += value;

                if(index == expression.length()-1){
                    numStack.push(Integer.parseInt(num));
                }else{
                    /**
                     *  再判断下一位是否还是数字
                     *  注意事项
                     *  如果下一位还是数字先进行拼接,先不入栈,直到判断不是数字才进行入栈
                     *  入栈完成之后 一定要清空当前的拼接的字符串
                     *  还有一种情况要判断当前下一位既不是数字也不是运算符就是已经是处于最后的位置了
                     *  再进行判断是否是字符(数字)会报空指针异常
                     */
                        if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                            numStack.push(Integer.parseInt(num));
                            num ="";
                        }
                }

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

        while (true){

          if(operStack.isEmpty()){
              break;
          }
              num1 = numStack.pop();
              num2 = numStack.pop();
              res = operStack.cal(num1, num2, operStack.pop());
              numStack.push(res);


        }

        System.out.println("结果:"+numStack.pop());
    }

你可能感兴趣的:(算法,算法,线性代数)