Transform the Expression - WA

#include <stdio.h>
#include <ctype.h>

int main()
{
int t;
scanf("%d", &t);

while(t--)
{
    char expression[400];
    expression[0] = '\0';

    scanf("%s", expression);

    char operatorStack[400];
    operatorStack[0] = '\0';
    int operatorStackLength = 0;
    char operandStack[400];
    operandStack[0] = '\0';
    int operandStackLength = 0;

    int i;

    for(i = 0; expression[i] != '\0'; i++)
    {
        if(isalpha(expression[i]))
        {
            operandStack[operandStackLength++] = expression[i];
        }
        else if(expression[i] == '(')
        {
            operatorStack[operatorStackLength++] = expression[i];
        }
        else if(expression[i] == ')')
        {
            operatorStackLength--;
            while(operatorStack[operatorStackLength] != '(')
            {
                operandStack[operandStackLength] =  operatorStack[operatorStackLength];
                operandStackLength++;
                operatorStackLength--;
            }
        }
        else
        {
            operatorStack[operatorStackLength++] = expression[i];
        }
    }

    printf("%s\n", operandStack);
}

return 0;

}

1 Like

test your program with ‘a+bc’, its showing ‘abc’ instead of 'abc+’

2 Likes

So we have to consider without bracket case only if there if one operand in the whole expression??

Thanks man that was the only problem :0