0

Here I am converting an infix expression to a prefix expression.

For some test cases my result is perfect.

But for certain test cases I get an output which is correct according to some sites but wrong according to other sites. So I am in a dilemma as to whether my code is correct or not.

For example, if infix expression is:

A+B*C+D

then output according to my code is:

+A+*BCD

But some site mentions the output as:

++A*BCD 

for the same input.

#include<stdio.h>
#include<string.h>
int main()
{
    int top=-1,k=0,i,l,top2=-1,j,a,t,m,t1;
    char s[100] ,s2[100],n, main[100];
    char q[100],p[100];
    printf("\nenter string :");
    gets(main);
    for(i=strlen(main)-1;i>=0;i--)
    {
        if(main[i]=='(')
        {
          q[k]=')';
        }
        else if(main[i]==')')
        {
            q[k]='(';
        }
        else
        {
            q[k]=main[i];
        }

        q[k+1]='\0';
        k++;
    }
    k=0;
    strcat(q,")");
    s[++top]='(';
    for(i=0;i<strlen(q);i++)
    {

        if(q[i]>='a' && q[i]<='z'   || q[i]>='A' && q[i]<='Z' || q[i]>='0' && q[i]<='9')
        {
            p[k]=q[i];
            p[k+1]='\0';
            k++;
        }

        else if (q[i]=='(')
        {
            s[++top]=q[i];
        }

        else if (q[i]==')')
        {
            for(j=top;j>=0;j--)
            {
                if(s[j]!='(')
                {
                    s[top--];
                    p[k]=s2[top2];
                    p[k+1]='\0';
                    s2[top2--];
                    k++;

                }
                else
                    break;
            }
            s[top--];

        }
        else if(q[i]=='+' || q[i]=='-' || q[i]=='*' || q[i]=='/' || q[i]=='^' || q[i]=='%')
        {

              if(q[i]=='+' || q[i]=='-')
            {
               n='1';
            }
            else if(q[i]=='*' || q[i]=='/' || q[i]=='%')
            {
                n='2';
            }
            else if (q[i]=='^')
            {
               n='3';
            }

                t=n-48;

             if(top2!=-1)
             {
           for(j=top;j>=0;j--)
           {
              t1=s[j]-48;

               if(t<=t1 )
               {
                    s[top--];
                    p[k]=s2[top2];
                    p[k+1]='\0';
                    k++;
                    s2[top2--];

               }
               else if(s[j]=='(')
                        break;

            }
            }
             s[++top]=n;
             s2[++top2]=q[i];
        }
}
k=0;
for(i=strlen(p)-1;i>=0;i--)
{
    main[k]=p[i];
    main[k+1]='\0';
    k++;
}
printf("\nFINAL ANSWER :\n");
printf("\n%s",main);
}

11
  • ++A*BCD seems to be the correct one. Commented Apr 17, 2020 at 15:56
  • 1
    But according to the algorithm of infix to prefix conversion, output I get is: +A+*BCD Commented Apr 18, 2020 at 7:18
  • Then I think you have a bug somewhere. See also scanftree.com/Data_Structure/infix-to-prefix Commented Apr 18, 2020 at 8:39
  • 1
    the posted code causes the compiler to output a LONG string of warning messages. Starting with the variable declaration: main[100]; as 'main' is the (must have) name for the starting point function. When compiling, always enable the warnings, then fix those warnings. ( for gcc, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11 ) Note: other compilers use different options to produce the same results. Commented Apr 18, 2020 at 17:42
  • 1
    the posted code contains a LOT of calculated values that are never used and contains a LOT of unused variables. Please correct. Commented Apr 18, 2020 at 18:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.