Quantcast
Viewing all articles
Browse latest Browse all 39796

Why wrong answer for Transform the Expression problem?

Here is my code for Transform the Expression problem :-

#include<stdio.h>
#include<stdlib.h>

int prior(char ch)
{
    if(ch == '+')
    {
        return 1;
    }
    else if(ch == '-')
    {
        return 2;
    }
    else if(ch == '*')
    {
        return 3;
    }
    else if(ch == '/')
    {
        return 4;
    }
    else if(ch == '^')
    {
        return 5;
    }
    else if(ch == '(')
    {
        return 0;
    }

}

void push(char *stk,int *top,char ch )
{
    if(*top < 399)
    {
        stk[++(*top)] = ch;
    }
}

char pop(char *stk,int *top)
{
    if((*top)>=0)
    {
        char temp;

        temp = stk[(*top)--];
        return temp;

    }
}

int main()
{
    int n,stray;

    scanf("%d",&n);

    char *op = (char *)calloc(400,sizeof(char));
    char *ex = (char *)calloc(400,sizeof(char));
    char *string = (char *)calloc(400,sizeof(char));

    int i,t_op ,t_ex;
    char ch;

    for(i=0;i<n;i++)
    {
        scanf("%s",string);

        t_op = -1;
        t_ex = -1;

        while(*string != '\0')
        {
            if((*string>=65 && *string<=90) || (*string>=97 && *string<=122))
            {
                push(ex,&t_ex,*string);
            }
            else 
            {

                if(*string == '(')
                {
                    push(op,&t_op,*string);
                }
                else if(*string == ')')
                {
                    while((ch = pop(op,&t_op)) != '(')
                    {
                        push(ex,&t_ex,ch);
                    }
                }
                else if(prior(*string) > prior(op[t_op]))
                {
                    push(op,&t_op,*string);
                }
                else
                {
                    while(prior(*string) < prior(op[t_op]))
                    {
                        push(ex,&t_ex,pop(op,&t_op));
                    }

                    push(op,&t_op,*string);

                }


            }

            string++;
        }

        printf("%s\n",ex);

    }

    return 0;
}

Can anyone tell me why it is giving wrong answer and is there any link from where we can find the test cases for a given problem?


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>