簡體   English   中英

堆棧問題java。 后綴評估

[英]Stack problem java. Postfix Evaluation

嗨,我在運行程序時遇到問題。 在PostfixEvaluate()方法中,它接收字符串並解決postfix問題並返回它。 好吧,當我去運行它時,我得到了一堆隨機數(有些重復),我快要瘋了,因為我不知道還能嘗試什么,而我為此花了更多的時間。采取。

這是PostfixEvaluate方法:

   public int PostfixEvaluate(String e){
        //String Operator = "";
    int number1;
        int number2;
        int result=0;
        char c;
        //number1 = 0;
        //number2 = 0;

        for(int j = 0; j < e.length(); j++){
            c = e.charAt(j);
            if (c != '+'&& c!= '*' && c!= '-' && c!= '/') {
                //if (c == Integer.parseInt(e)) {   
                s.push(c); 
        }
            else {
                number1 = s.pop();
                number2 = s.pop();
                switch(c) {
                    case '+':
                    result = number1 + number2;
                    break;
                    case '-':
                    result = number1 - number2;
                    break;
                    case '*':
                    result = number1 * number2;
                    break;
                    case '/':
                    result = number1 / number2;
                    break;
                    } s.push(result);
                }
            System.out.println(result);
        } 
            return s.pop();
}

public static void main(String[] args) {
    Stacked st = new Stacked(100);
    String y = new String("(z * j)/(b * 8) ^2");
    String x = new String("2 3 + 9 *");
    TestingClass clas = new TestingClass(st);

    clas.test(y);


    clas.PostfixEvaluate(x);




 }
        }

這是堆棧類:

 public class Stacked  {

    int top;
    char stack[];
    int maxLen;

    public Stacked(int max) {
        top = -1; 
        maxLen = max; 
        stack = new char[maxLen];

        }

    public void push(int result) {
            top++;
            stack[top] = (char)result;

        }

    public int pop() {
        int x;
        x = stack[top];
        //top = top - 1;
        top--;

        return x;

    }

    public boolean isStackEmpty() {
            if(top == -1) {
                System.out.println("Stack is empty " + "Equation Good");

                return true;
            } 
            else 
                System.out.println("Equation is No good");
                return false;
    }

    public void reset() {

        top = -1;
    }

    public void showStack() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for(int j = top; j > -1; j--){
            System.out.println(stack[j]);
        }
        System.out.println(" ");
    }

    public void showStack0toTop() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for(int j=0; j>=top; j++){
            System.out.println(stack[j]); 
        }
        System.out.println(" ");
        }
    }

在我看來,您根本不處理空格。

這意味着當您放置一個空格時,它將在操作過程中將其從堆棧中彈出時,會將字符空間隱式轉換為它的ascii值(32)。 同樣,您似乎假設所有數字/結果都將是一位數字,並且將char從int強制轉換為int,這不是您想要執行的操作,因為那樣會將char轉換為char的ascii值,即'' -> 32,“ 3”-> 51,依此類推。

如果我是你,我將在PostfixEvaluate中為您的循環執行此操作:

while(!e.equals("")){
    string c;
    int space = e.indexOf(' ');
    if(space!=-1){
        c = e.substring(0,space);
        e = e.substring(space+2);
    } else{
        c = e;
        e = "";
    }
    if (!c.equals("+")&& !c.equal("*") && !c.equals("-") && !c.equals("/")) {
    //...
}

並更改堆棧以容納字符串或整數。

問題是您將char作為int推送到堆棧上,因此無意中使用了數字的ascii表示形式,這不是數字的實際值。

不用執行這種復雜的字符移動,而是使用String.split()將輸入字符串標記化。 例:

String[] tokens = e.split(" ");
for(String token:tokens){
    if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
        s.push(Integer.parseInt(token)); 
    } else {
        ....
    }
}

您需要先將字符串拆分為令牌:

/* Splits the expression up into several Strings,
 * all of which are either a number or and operator,
 * none of which have spaces in them. */
String [] expressionAsTokens = e.split(" ");

然后,您需要確保比較字符串,而不是字符:

//compare strings instead of chars
String token = expressionAsTokens[j];
if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
    s.push(Integer.parseInt(token)); 
} else {
    //same code as you had before
}

另外,是否有任何理由將所有內容都作為char數組存儲在Stacked類中? 您的pop()方法返回並返回整數,但所有內容都存儲為char

對於此應用程序,所有內容都應存儲為整數:

public class Stacked {
    int stack[]; // array is of type integer
    int top;
    int maxLen;

    // constructor

    public void push() {/*...*/}
    public int pop() {/*...*/} //pop returns an int as before

    //...
}

最后一點:注意加減數字的順序。我不記得后綴運算數是左優先還是右優先,但是請確保按正確的順序獲取它們。 就像您現在擁有的那樣, 2 3 - 4 *計算結果為4 * (3 - 2)而我認為應該是(2 - 3) * 4 這與加法和乘法無關,而與減法和除法無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM