I am given an arithmetic formula containing operators +, -, *, / and parentheses (which might or might not change the natural precedence of operators). An example would be the following one: a / b + f – (c + d) * e – a * c. and I am asked to use a stack (implemented as a linked list) in order to keep track of the operands and the operators: an example of how my program should work is the following:
The problem that I have difficulty understanding is how can I distinguish the precedence of the operands!
Here is an incomplete version of the code that I wrote:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct btnode Btree;
typedef struct node s_Node;
struct btnode {
char info;
Btree *left;
Btree *right;
};
struct node {
char element;
s_Node*next;
};
typedef struct{
s_Node *top_stack;
} stack_t;
int IsOperator(char c);
main () {
FILE* fp;
stack_t operands;
stack_t operators;
char c;
operands=NewStack();
operators=NewStack();
fp= fopen ("Myfile.txt", "r");
if (fp== NULL)
printf (" FILE COULD NOT BE OPENED");
else
{
c=getc(fp);
while (!feof (fp))
{
if ( c== ' ');
else
{
printf ("Here is your character: %c\n", c);
if (IsOperator (c))
Push (c, &operands);
else if ( isalpha (c))
}
c=getc(fp);
}
}
}
int IsOperator(char c)
{
switch(c)
{
case '+':
case '-':
case '/':
case '*':
return 1;
default:
return 0;
}
}
stack_t NewStack()
{
stack_t *n_stack;
n_stack=(stack_t*)malloc(sizeof(stack_t));
n_stack->top_stack=NULL;
return (*n_stack);
}
int Push(char e, stack_t *q)
{
s_Node *nn;
nn= (s_Node*)malloc(sizeof(s_Node));
if(Full(*q))
{
printf("\n\t Stack is Full !! \n\n");
return 0; // return 0 if enstack NOT successful
}
else
{
nn->element=e; // Storing the elemnt read inside the the new node
nn->next=q->top_stack; // Pointing the new node to the top of the stack
q->top_stack=nn; // Changing the top of the stack
return 1;
}
}
Thank you in advance!
for algorithm you are using, operands has no precedence. But in bottom-up shift-reduce parser, it does have precedence as @WhozCraig said at comment of this post below.
The operands always be pushed into operand stack and will be popped out 2 and calculated with an operator then pushed again to operand stack as a single operand.
For your formula: a / b + f – (c + d) * e – a * c
push
to operand stackoperator:
/
push
to operator stackoperator: /
b
push
to operand stackoperator: /
+
+
<= /
-> pop /, a & b -> a / b -> push to operand stack+
to operator stackoperator: +
f
operator: +
-
-
<= +
-> pop +, (a/b) & f -> (a/b) + f -> push to operand stackoperator: -
(
operator: - (
c
operator: - (
+
operator: - ( +
d
operator: - ( +
)
operator: -
*
*
> -
push to operator stackoperator: - *
e
*
> -
push to operand stackoperator: - *
-
-
<= *
pop *, (c + d) & e -> (c + d) * e -> push to operand stack-
<= -
pop -, ((a/b)+f) & ((c+d)*e) -> ((a/b)+f) - ((c+d)*e) -> push to operand stackoperator: -
a
operator: -
*
*
> -
push to operator stackoperator: - *
c
operator: - *
end of line
result: ((((a/b)+f)-((c+d)*e))-(a*c))