中缀转后缀,不在循环内部的循环内部。

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

infix to postfix without loops inside loops

问题

List<String> infix2Postfix(List<String> infix) {
    Stack<String> opStack = new Stack<>();
    List<String> outPut = new ArrayList<>();

    for (String inComing : infix) {
        if (OPERATORS.contains(inComing)) {
            if (opStack.empty() || opStack.peek().equals("(")) {
                // Handle case when operator stack is empty or contains "("
                // ...
            } else {
                // Handle other cases when operators are present
                // ...
            }
        } else if ("()".contains(inComing)) {
            if (")".equals(inComing)) {
                // Handle closing parenthesis
                // ...
            } else {
                opStack.add(inComing);
            }
        } else {
            outPut.add(inComing);
        }
    }

    if (opStack.contains("(")) {
        throw new IllegalArgumentException(MISSING_OPERATOR);
    }

    while (!opStack.empty()) {
        outPut.add(opStack.pop());
    }

    return outPut;
}

请注意,我已经只保留了代码的翻译部分,而删除了问题和其他内容。如果您有任何更多的问题或需要进一步的帮助,请随时提问。

英文:

So i have an assignment to write infix to postfix method without any nested loops (loops in loops) and i didn't notice that and i wrote long method with nested loops, any idea how to fix it? here is the code
How can i divide them into smaller method while have stack?

List&lt;String&gt; infix2Postfix(List&lt;String&gt; infix) {
        Stack&lt;String&gt; opStack = new Stack&lt;&gt;();
        List&lt;String&gt; outPut = new ArrayList&lt;&gt;();


        for (String inComing : infix) {
            // When incoming is  one of  -,+,*,/,^
            if (OPERATORS.contains(inComing)) {
                if (opStack.empty() || opStack.peek().equals(&quot;(&quot;)) {
                    ....
                } else {
                    ....
                    if (inComingP == 4 &amp;&amp; inComingP == operatorP) {
                        ....
                    } else if (inComingP == operatorP) {
                       ...

                    } else if (inComingP &lt; operatorP) {

                        while (!opStack.empty() &amp;&amp; !opStack.peek().equals(&quot;(&quot;)) {
                          ...
                        }
                        opStack.push(inComing);

                    } else {
                        opStack.push(inComing);
                    }
                }

            }
            // when incoming is one of &quot;(&quot; , &quot;)&quot;
            else if (&quot;()&quot;.contains(inComing)) {
                if (&quot;)&quot;.equals(inComing)) {

                    while (opStack.size() != 0 &amp;&amp; !opStack.peek().equals(&quot;(&quot;)) {
                       ...
                    }
                    if (opStack.size() == 0) {
                        ...
                    } else {
                        opStack.pop(); 
                    }


                } else {
                    opStack.add(inComing);
                }
                
            } else {
                outPut.add(inComing);
            }

        }

        if (opStack.contains(&quot;(&quot;)) {
            throw new IllegalArgumentException(MISSING_OPERATOR);
        }
        while (!opStack.empty()) {

            outPut.add(opStack.pop());

        }
        return outPut;
    }

huangapple
  • 本文由 发表于 2020年4月9日 19:06:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61119726.html
匿名

发表评论

匿名网友

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

确定