0

我试图查明我的前缀计算器类中的错误,但我在这样做时遇到了麻烦。

预期的输出应该是这样的: InFix: "2+4" (例如) PostFix: "24+" PreFix: " 24" (这里是错误)

这是计算前缀的类:

public class PreEva {
private String prefijo;
private String iString;

public PreEva(String iString){
    int longitud = iString.length(); //kinda redundant
    char p1[]=new char[longitud], p2[]=new char[longitud];
    prefijo="";
    int contp1=0, contp2=0;

    for(int i=longitud-1;i>=0;i--){
        char tmp=iString.charAt(i);
        switch(tmp){
            case '^':case')':
                if(contp2<=0){      //If p2 is empty, inputs the operator
                    p2[contp2]=tmp;
                    contp2++;
                    }
                else{
                    p2[contp2]=tmp;
                    contp2++;
                }break;
            case '/':case '*':
                if(contp2<=0){      //If p2 is empty, inputs the operator
                    p2[contp2]=tmp;
                    contp2++;
                }
                else{
                    if(p2[contp2-1]=='^'){
                        while(p2[contp2]-1=='^'){
                            for(int j=contp2; j>0; j--){    //Passes to p1
                                if(p2[contp2-1]==p2[contp2-2]){
                                    contp2=j-1; 
                                    j=0;
                                }
                                else{
                                    p1[contp1]=p2[j-1];
                                    contp1++;
                                }
                                contp2++;
                            }
                            p2[contp2]=tmp;
                            contp2++;
                        }
                    }
                    else{
                        p2[contp2]=tmp;
                        contp2++;
                    }
                }break;
            case '+':case '-':              
                if(contp2<=0){          //Ifthere's nothing in p2, inputs the operator
                    p2[contp2]=tmp;
                    contp2++;
                }
                else{
                    if(p2[contp2-1]==')'||p2[contp2-1]=='+'||p2[contp2-1]=='-'){
                        p2[contp2]=tmp;
                        contp2++;
                    }
                    else{
                        while(!(p2[i-1]==')'||p2[i-1]=='+'||p2[i-1]=='-')){
                            for(int j=contp2; j>0; j--){    //Paso a p1
                                if(p2[contp2-1]==p2[contp2-2]){
                                    contp2=j-1;
                                    j=0;
                                }
                                else{
                                    p1[contp1]=p2[j-1];
                                    contp1++;
                                }
                                contp2++;
                            }
                            p2[contp2]=tmp;
                            contp2++;
                        }
                    }
                }break;
            case '(':
                if(contp2<=0){              //If there is nothing adds to p2
                    p2[contp2]=tmp;
                    contp2++;
                }
                else{
                    for(int j=contp2; j>0; j--){    //To p1
                        if(p2[contp2-1]==')'){      //If the one under tmp is ')', drops the counter(subtracs from counter i believe is more accurate)
                            contp2=j-1;
                            j=0;
                        }
                        else{
                            p1[contp1]=p2[j-1];
                            contp1++;
                        }
                    }
                }break;
            default:p1[contp1]=tmp;          
            contp1++;
            break;
        }

    }
    for(int i=longitud-1; i>=0 ;i--){
        prefijo=""+prefijo+""+p1[i];
    }
 }

/* Retornar Resultado */
public String getOS(){
    return prefijo;
}

如果有人对如何改进我的代码或纠正此错误有任何建议,我们将不胜感激!

提前谢谢。

4

0 回答 0