Java implements four arithmetic expressions, infix expressions to postfix expressions

1. Analysis

The central idea: use suffix expression calculations.

Use the reverse Polish method;
infix expressions to postfix expressions follow the following principles:
1. When the operand is encountered, output directly;
2. When the stack is empty, when the symbol is encountered, it is pushed into the stack
3. When the left parenthesis is encountered, it is pushed into the stack .
4. When the right parenthesis is encountered, the pop operation is performed, and the popped elements are output until the left parenthesis is popped from the stack, and the left parenthesis is not output;
5. When other operators are encountered,'+''一''*'' When /', pop all stack item elements whose priority is greater than or equal to the operator, and then
push the operator to the stack; 6. Finally, the elements in the stack are popped out of the stack and output.

Code

package com.instance;

public class CalculateImp {

public String calculate(char[] str){
    int j = 0;


    //中缀表达式转后缀表达式
    double [] num = new double[100];
    char[] operator = new char[100];
    int index = 0;

    char[] stack = new char[100];
    double[] stackj = new double[100];
    int top = -1;

    int temp = 0; //正在拼写数字
    int flag = 0;//表示是否执行数字

    int flag2 = 0;//判断什么时候读完了

    while (true){

        if(j == str.length){
           flag2 = 1;
        }
        if(flag2==0&&str[j] >='0'&&str[j] <= '9'){//读到数字
            flag = 1;
            temp *= 10;
             temp += str[j]-'0';


        }else{//读到了符号,或者结束

            if(flag==1){//如果在拼写一个数字就将一个数字输出

                num[index] = temp;

                operator[index] = '?';

                index++;
                flag = temp = 0;
            }

            if(flag2==1){//如果字符串已经结束


                while (top>=0 ){
                    int q = top--;
                    if(stack[q] != '('){
                        operator[index++] =  stack[q];

                    }

                }
                break;

            }else {//读到一个符号

                if(top == -1||str[j] == '('){
                    stack[++top] = str[j];



                }else if(str[j] == ')'){//如果是右括号则一直出栈道左括号

                    while (top>=0&&stack[top]!='('){
                        operator[index++] =  stack[top--];

                    }
                    

                }else if(str[j]=='*'||str[j]=='/'){//如果是乘除,则乘除出栈,新符号入栈
                      while ((top>0)&&(stack[top] == '*'||stack[top] == '/')){
                          operator[index++] =  stack[top--];

                      }
                      stack[++top]=str[j];

                }else{//如果是加减,则四则运算出栈,新符号入栈

                    while ((top>=0)&&(stack[top] !='(')){//>=0是为了确保第0位可以出栈
                        operator[index++] =  stack[top--];

                    }

                    stack[++top]=str[j];

                }

            }

        }
        j++;
    }

    top = -1;
    double temp1  = 0;
    double temp2 = 0;
    //后缀表达式运算

    for(int i=0;i<index;i++){
        if(operator[i] == '?'){
            stackj[++top] = num[i];
        }else{
            temp1 =  stackj[top--];

            temp2 =  stackj[top--];

            switch (operator[i]){
                case  '+':
                    stackj[++top] = temp2+temp1;
                    break;
                case  '-':
                    stackj[++top] = temp2-temp1;
                    break;
                case  '*':
                    stackj[++top] = temp2*temp1;

                    break;
                case  '/':
                    stackj[++top] = temp2/temp1 ;
                    break;
            }

        }
    }
    double result = stackj[0];
    System.out.println("_____"+(result)+"______");
    return result + "";
}

public static void main(String[] args) {
    CalculateImp c = new CalculateImp();
    String title = "4/6+(9+4)";
    c.calculate(title.toCharArray());
}

}

Guess you like

Origin blog.csdn.net/qq_44688861/article/details/105107958