java堆栈作业将数字放在堆栈上

huangapple 未分类评论44阅读模式
英文:

java Stack homework put number on stack

问题

以下是您提供的代码的翻译部分:

import java.util.Stack;

Stack<Integer> stk = new Stack<>();

int res = 0;

for (int i = 0; i < expression.length(); i++) {
    char ch = expression.charAt(i);

    if (Character.isDigit(ch)) {
        stk.push(ch - '0');
    } else {
        if (stk.isEmpty()) {
            return 0;
        }

        if (ch == '-') {
            res = stk.pop() - stk.pop();
            res = res * -1;
        }

        if (ch == '*') {
            res = stk.pop() * stk.pop();
        }

        if (ch == '+') {
            res = stk.pop() + stk.pop();
        }
        if (ch == '/') {
            res = stk.pop() / stk.pop();
        }
    }
}

return res;

请注意,上述翻译的代码部分是您提供的 Java 代码的翻译。如果您有任何进一步的问题或需要解决问题,请随时提问。

英文:

Hello eveyone in need help to answer about question in java stack

This is my question:
Complete the method below that evaluates a string representing an arithmetic expression in reverse Polish (postfix) notation. The string contains an expression consisting of numbers '0' to '9' and operators '+', '-', '*' and '/'.

For example, these are some possible input strings and the corresponding results:

Input: Output:
"12+" 3
"43*" 12
"123+" 5
"123
+" 7
"12+3*" 9
To help you solve this problem, you can use the standard class Stack<Integer> which implements a stack of integers:

Stack&lt;Integer&gt; stk = new Stack&lt;&gt;(); // create a stack
stk.push(5);                        // push an integer onto the top of the stack
int n = stk.pop();                  // pop an integer from the top of the stack
if ( stk.empty() ) ...              // test if the stack is empty

I started to answer this is my code:

    Stack&lt;Integer&gt; stk = new Stack&lt;&gt;();

int res=0;

        for (int i = 0; i &lt; expression.length(); i++) {
            char ch = expression.charAt(i);

            if (Character.isDigit(ch)) {
                stk.push(ch - &#39;0&#39;);
            } 
else {
                if(stk.isEmpty())
            {
                return 0;
            }

                if (ch == &#39;-&#39;) {
                    res = stk.pop() - stk.pop();
                    res=res*-1;
                }

                if (ch == &#39;*&#39;) {
                    res = stk.pop() * stk.pop();
                }


                if (ch == &#39;+&#39;) {
                    res = stk.pop() + stk.pop();
                }
                if (ch == &#39;/&#39;) {
                    res = stk.pop() / stk.pop();
                }
            }

}


     return res;

This is error that i recived, i don't understand what is not ok in this code.

error:

 An exception of type java.util.EmptyStackException was reported when executing this line:

    res = stk.pop() * stk.pop();

Exception in thread &quot;main&quot; java.util.EmptyStackException
    at java.util.Stack.peek(Stack.java:102)
    at java.util.Stack.pop(Stack.java:84)
    at Main.evaluate(Main.java:28)
    at Main.exitTest(Main.java:78)
    at Main.main(Main.java:104)

i need your help thanks for everyone.

答案1

得分: 0

To check if the stack is empty, use stk.isEmpty().

For a computation, pop two items from the stack, do the computation on them, and push it on the stack. For example, multiplication:

if (ch == '*') {
    stk.push(stk.pop() * stk.pop());
}
英文:

To check if the stack is empty, use stk.isEmpty().

For a computation, pop two items from the stack, do the computation on them, and push it on the stack. For example a multiplication:

if (ch == &#39;*&#39;) {
    stk.push(stk.pop() * stk.pop());
}

huangapple
  • 本文由 发表于 2020年4月5日 02:03:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/61032488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定