Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need to do if you really want as few parentheses as possible, is similar to what the algorithm you linked to says. However...</p> <ul> <li>You should store an operator for each <em>composite</em> operand in the <code>Stack</code>. Namely, the last operator used in the operand. You could use a second <code>Stack</code> for this. If the operand is not composite, you could add <code>null</code> to the second <code>Stack</code>, since there is no operator.</li> <li>Don't encapsulate the resulted <code>String</code> with parentheses. That is done elsewhere in the algorithm (see below).</li> </ul> <p>When you pop the top two values from each of the <code>Stack</code>s, you have 3 operators at hand: </p> <ul> <li>The current operator</li> <li>The last used operator in the first operand (if the operator exists)</li> <li>The last used operator in the second operand (if the operator exists)</li> </ul> <p>Depending on these three operators, you should encapsulate the first and/or second operand with parentheses, before combining them.</p> <p>You could use operator precedence to determine whether there should be parentheses. The order goes like this: <code>(none), {"*", "/"}, {"+", "-"}</code></p> <ul> <li>The first operand needs parentheses if and only if its operator has a lower precedence than the current operator.</li> <li>The second operand needs parentheses if its operator has a lower precedence than the current operator, or if they have equal precedence where the current operator is either <code>"/"</code> or <code>"-"</code>.</li> </ul> <p>The rest should be done the way your algorithm described.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload