fix: recognize bracket operator

This commit is contained in:
Harsh Karande 2020-10-19 10:29:02 +05:30
parent 4f37a67cbd
commit c3142724fb

View File

@ -129,7 +129,7 @@ void convert(char infix[], char postfix[])
s.tos = -1; // initalize the tos s.tos = -1; // initalize the tos
int i, j = 0, pr; int i, j = 0, pr;
char ch; char ch, temp;
for (i = 0; infix[i] != '\0'; i++) for (i = 0; infix[i] != '\0'; i++)
{ {
@ -141,14 +141,32 @@ void convert(char infix[], char postfix[])
j++; // incement j j++; // incement j
} }
else else
{
if (ch == '(')
{
push(&s, ch);
}
else
{
if (ch == ')')
{
while ((temp = pop(&s)) != '(')
{
postfix[j] = temp;
j++;
}
}
else
{ {
while (isEmpty(s) == 0) // check if stack is empty while (isEmpty(s) == 0) // check if stack is empty
{ {
pr = prcnd(ch, s.arr[s.tos]); // check operator precedence pr = prcnd(ch,
s.arr[s.tos]); // check operator precedence
if (pr == 1) if (pr == 1)
{ {
break; // if ch has a greater precedence than s.arr[s.tos] break; // if ch has a greater precedence than
// s.arr[s.top]
} }
postfix[j] = pop(&s); postfix[j] = pop(&s);
@ -158,6 +176,8 @@ void convert(char infix[], char postfix[])
push(&s, ch); // push ch to stack push(&s, ch); // push ch to stack
} }
} }
}
}
while (isEmpty(s) == 0) // check if stack is empty while (isEmpty(s) == 0) // check if stack is empty
{ {