Skip to content

Commit

Permalink
fix: recognize bracket operator
Browse files Browse the repository at this point in the history
  • Loading branch information
harshcut committed Oct 19, 2020
1 parent 4f37a67 commit c314272
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions conversions/infix_to_postfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void convert(char infix[], char postfix[])
s.tos = -1; // initalize the tos

int i, j = 0, pr;
char ch;
char ch, temp;

for (i = 0; infix[i] != '\0'; i++)
{
Expand All @@ -142,20 +142,40 @@ void convert(char infix[], char postfix[])
}
else
{
while (isEmpty(s) == 0) // check if stack is empty
if (ch == '(')
{
pr = prcnd(ch, s.arr[s.tos]); // check operator precedence

if (pr == 1)
push(&s, ch);
}
else
{
if (ch == ')')
{
break; // if ch has a greater precedence than s.arr[s.tos]
while ((temp = pop(&s)) != '(')
{
postfix[j] = temp;
j++;
}
}
else
{
while (isEmpty(s) == 0) // check if stack is empty
{
pr = prcnd(ch,
s.arr[s.tos]); // check operator precedence

if (pr == 1)
{
break; // if ch has a greater precedence than
// s.arr[s.top]
}

postfix[j] = pop(&s);
j++;
}

push(&s, ch); // push ch to stack
}

postfix[j] = pop(&s);
j++;
}

push(&s, ch); // push ch to stack
}
}

Expand Down

0 comments on commit c314272

Please sign in to comment.