1

I was trying to write a code to convert infix expression into postfix expression , I have the source code here and when I compile and give the input say 'a+b' it returns 'ab?' as the answer it does not show the operator symbols at all. Can you guys please tell me where I have gone wrong.!!

It also shows this warning when I compile

infpos.c:74:1: warning: non-void function does not return a value in all control paths [-Wreturn-type]

PS: I got to know what is the error.. Thank you

(The error was in the line 58 I had to use prefix increment but I had used postfix increment)

    #include<stdio.h>
    #include<ctype.h>
    #define max 30

    void main()
    {
        char inf[30],post[30];
        void convert(char [],char []);
                printf("Enter the infix exxpression\n");
                     scanf("%s",inf);

                convert(inf,post);

                printf("Postfix expression is\n");
                printf("%s\n",post);
    }

  void convert(char inf[],char post[])
  {
        int i,j=0,top=-1,f=1,test;
        char stack[30],ch,x;
        int check(char);
        int pre(char);

          for(i=0;inf[i]!='\0';i++)
          {
              ch = inf[i];
              test = check(ch);

              switch(test)
              {
                 case 1:
                    post[j++] = ch;
                    break;
                 case 2:
                    stack[++top] = ch;
                    break;
                 case 3:
                    while((x=stack[top--])!='(')
                        post[j++] = x;
                    break;
                 case 4:
                   do
                   {
                    if(top==-1)
                        f = 1;
                    else if(stack[top] == '(')
                        f = 1;
                    else if(pre(ch)>pre(stack[top]))
                        f = 1;
                    else
                    {
                        post[j++] = stack[top--];
                        f = 0;
                    }
                  }while(f==0);
                  stack[top++] = ch;
                  break;
           }
       }
          while(top!=-1)
              post[j++] = stack[top--];
         post[j] = '\0';
    }

     int pre(char op)
    {
        if(op == '+' || op=='-')
           return 1;
        else if(op == '/' || op=='*' || op =='%')
          return 2;
        else if(op=='^')
          return 3;
     }

      int check(char ch)
      {
            if(isalnum(ch))
              return 1;
           else if(ch=='(')
               return 2;
           else if(ch == ')')
               return 3;
           else if(ch == '+' || ch =='-' || ch=='/' || ch =='*' || ch=='%' || ch=='^')
             return 4;
          else 
               return 5;
    }
3
  • 1
    Well first of all fix the warning. What will pre return if none of the conditions are true? And debugging, debugging, debugging! Step through your code statement by statement while monitoring variables and their values, to see what's going on. Use pen and paper to keep track of the stack you have and other arrays. Commented Oct 20, 2021 at 6:28
  • 2
    OT: regardng: void main() There are only two valid signatures for main(). They are: int main( void ) and int main( int argc, char *argv[] ) Commented Oct 20, 2021 at 6:33
  • 1
    Re the warning: The function pre doesn't return a value in all cases Commented Oct 20, 2021 at 7:12

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.