The expression a*b/(a+b). To organize the computation of this expression using Polish notation algorithm. To use the system stack.

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
int main ()
{
P_STACK st;
Nd P_NODE;
char expr = "*+ab-cd";
char ch;
int a, b, c, d, i, aStep, bStep, cStep, dStep, add, mult, sub, res;
st = (P_NODE) malloc (sizeof (stack));
init_stack (st);
i = 0;
while (expr[i]!='\0')
{
nd=(P_NODE) malloc (sizeof (node));
nd->info=expr[i];
push (st, nd);
i++;
}
printf ("Enter the 4th number:");
scanf ("%d%d%d%d", &a, &b, &c, &d);
while (!is_stack_empty (st))
{
nd = pop (st);
if (!is_stack_empty (st))
switch (nd->info)
{
case 'd':
dStep=d;
break;
case 'c':
cStep=c;
break;
case '-':
sub=cStep-dStep;
break;
case 'b':
bStep=b;
break;
case 'a':
aStep=a;
break;
case '+':
add=bStep+aStep;
break;
case '*':
mult=add*sub;
break;
}
}
res=mult;
printf ("Result: [expr=%d] = [%d]", expr, res);
return 0;
}</string.h></stdlib.h></stdio.h>
```

Here is the library:

```
#ifndef STACK_H_
#define STACK_H_
typedef struct NODE NODE;
typedef struct NODE *P_NODE;
typedef struct STACK STACK;
typedef struct STACK *P_STACK;
struct NODE
{
int info;
Next P_NODE;
} node;
struct STACK
{
int count;
int size;
Top P_NODE;
} stack;
typedef enum {false, true} bool;
void init_stack (P_STACK st);
bool is_stack_empty (P_STACK st);
void push (P_STACK st, nd P_NODE);
P_NODE pop (P_STACK st);
void print_stack (P_STACK st);
#endif
```

asked June 5th 19 at 21:41

1 answer

answered on June 5th 19 at 21:43

In the code you have scored another expression: (a + b) * (c - d)

Where the implementation of a stack?

To implement:

1. For calculations you don't need 4 variables or 10 (depending on how many terms), two are enough for the usual binary operations (two operands). The result of intermediate calculations need to re-insert the stack in place pulled out of the operands and the operation.

2.The algorithm for computing direct Polish notation, well described on the wiki.

Thus, the stack here is not very suitable, except for the operations push and pop need to do the bypass list in the forward direction and insert/delete items at arbitrary positions.

Or as an option with the stack - use stack 2. Ie one extract postponed and the calculation results are placed in the second until the first is empty, then switch to the second, and so until in the stack will not be the only value is the result of the calculation.

Or one stack, but change the implementation. So there is no structure P_STACK.

Where the implementation of a stack?

To implement:

1. For calculations you don't need 4 variables or 10 (depending on how many terms), two are enough for the usual binary operations (two operands). The result of intermediate calculations need to re-insert the stack in place pulled out of the operands and the operation.

2.The algorithm for computing direct Polish notation, well described on the wiki.

need to read expression from left to right, considering the operator and next to him two operands. If some of these operand is another operator, then the computation of the first statement is delayed until, until it is calculated the new operator. Iteration of this process are repeated until, until the operator will not be computed, what must ultimately happen if the expression the number of operands is one more than the number of operations (in the case of binary operations). Once the operator is computed, it and its two operands are replaced with the received value (the operand). Since the operator and the two operands are replaced by the computed operand, it becomes one operator and one operand is less.

Thus, the stack here is not very suitable, except for the operations push and pop need to do the bypass list in the forward direction and insert/delete items at arbitrary positions.

Or as an option with the stack - use stack 2. Ie one extract postponed and the calculation results are placed in the second until the first is empty, then switch to the second, and so until in the stack will not be the only value is the result of the calculation.

Or one stack, but change the implementation. So there is no structure P_STACK.

Find more questions by tags C++

While on the stack is the operator and two operands, remove them, perform the operation and put the result on the stack.

If the expression is not over, then read the next element of the expression, put the operator or the operand on stack, go to the beginning of the algorithm.

If the expression is ended, and the stack is only one value, it is the result of the expression. If on the stack there's something else wrong. - Kenyatta commented on June 5th 19 at 21:46