Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally, you have to define the priority of each operation and process each of them one by one.</p> <pre><code> First your expression is: '`[ 5,-1, 6,-4,20,-2, 4]`' Do all '/' first: '`[ 5,-1, 6,-4, <b>5,-1, 0</b>]`' &lt;- 20/ 4 = 5+0 Then, do all '*': '`[ 5,-1,<b>30,-1, 0</b>,-1, 0]`' &lt;- 6* 5 = 30+0 Then, do all '-': '`[ 5,-1,30,-1, 0,-1, 0]`' &lt;- Nothing Then, do all '+' (sum positive): '`[<b>35,-1, 0,-1, 0,-1, 0</b>]`' &lt;- 5+30+0+0 = 35 + 0 + 0 </code></pre> <p><b>ADDED: </b> Here is the C code.</p> <pre><code>void ShowArray(int pX[], int pLength) &#123; int i; for(i = 0; i &lt; pLength; i++) printf("%3d ", pX[i]); printf("\n"); &#125; &nbsp; void ShiftArray(int pX[], int pIndex, int pSkip, int pLength) &#123; int i; for(i = pIndex; i &lt; (pLength - pSkip); i++) pX[i] = pX[i + pSkip]; &nbsp; for(i = (pLength - pSkip); i &lt; pLength; i++) pX[i] = 0; &#125; &nbsp; int main(void) &#123; const int OPERCOUNT = 4; const int PLUS = -1; // -1 Do last const int SUBT = -2; const int MULT = -3; const int DIV = -4; // -4 Do first &nbsp; int X[] = &#123;5, PLUS, 6, MULT, 20, DIV, 4&#125;; int XCount = 7; &nbsp; ShowArray(X, XCount); &nbsp; int i; for(i = OPERCOUNT; --i &gt;= 0; ) &#123; int OPER = -(i + 1); int j = 0; for(j = 0; j &lt; XCount; j++) &#123; int x = X[j]; if (x == OPER) &#123; if (x == PLUS) X[j - 1] = X[j - 1] + X[j + 1]; else if (x == SUBT) X[j - 1] = X[j - 1] - X[j + 1]; else if (x == MULT) X[j - 1] = X[j - 1] * X[j + 1]; else if (x == DIV ) X[j - 1] = X[j - 1] / X[j + 1]; ShiftArray(X, j, 2, XCount); &#125; &#125; &nbsp; ShowArray(X, XCount); &#125; &nbsp; int Sum = 0; int j; for(j = 0; j &lt; XCount; j++) &#123; int x = X[j]; if (x &gt; 0) Sum += x; &#125; printf("Result: %d\n", Sum); &#125; </code></pre> <p>And here is the result:</p> <pre><code> 5 -1 6 -3 20 -4 4 5 -1 6 -3 5 0 0 5 -1 30 0 0 0 0 5 -1 30 0 0 0 0 35 0 0 0 0 0 0 Result: 35 </code></pre> <p>This should works.</p> <p>Enjoy coding.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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