1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

infix to postfix

Discussion in 'C' started by zeroeight, Oct 29, 2009.

  1. zeroeight

    zeroeight New Member

    Joined:
    Oct 29, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys,
    I'm a newbie here, I just want to seek for help regarding my program. I want to implement a infix to postfix conversion only accepting numbers. Right now, I already debugged the errors and encountering loop everytime I execute it.
    Here's my sample code:
    ********************************************************************
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    
    struct stack_elem
    {
     char elem[10];
     int topelem;
    } stckmain;
    void intoposconvert(char *infix, char *postfix, struct stack_elem *st)
    {
     char *inf, *pos;
     struct stack_elem *s;
     char temp;
     int tctr;
     inf = &infix[0];
     pos = &postfix[0];
     s = st;
    while(*inf){
     if ( *inf == '(' )
      {
       tctr=++s->topelem; /* increment the topelem */
       s->elem[tctr]=*inf; /*push*/
       temp=s->elem[tctr]; /*assign value pushed in stack in temp*/
       inf++;
      }
     else if ( *inf == ')' )
      {
       while ( temp != '(' )
        {
         tctr=s->topelem; /*assign top in tmp*/
         temp=s->elem[tctr]; /*assign value to be popped in temp*/
         --s->topelem; /*pop*/
         *pos=temp;
         pos++;
         inf++;
        }
      }
     else if (*inf == '*' || *inf == '/') /*data coming from ICP*/
      {
       if( s->topelem == -1 || s->elem[tctr] == '(' ) /*if stack is empty or entity in stack is '('*/
        {
         tctr=++s->topelem;
         s->elem[tctr]=*inf;
         temp=s->elem[tctr];
         inf++;
        }
       else /*else stack is not empty*/
        {
         temp=s->elem[tctr];
          if(temp == '+' || temp == '-')
           {
            tctr=++s->topelem;
     s->elem[tctr]=*inf;
     inf++;
           }
          else if (temp == '^')
           {
            tctr=s->topelem;
     temp=s->elem[tctr];
            --s->topelem;
            *pos=temp;
     pos++;
     inf++;
           }
          else if (temp == '*' || temp == '/')
          {
           tctr=s->topelem;
           temp=s->elem[tctr];
           --s->topelem;
           *pos=temp;
           pos++;
           inf++;
           tctr=++s->topelem;
           s->elem[tctr]=*inf;
          }
         }
       }
     else if (*inf == '+' || *inf == '-')
      {
       if( s->topelem == -1 || s->elem[tctr] == '(' ) /*if stack is empty or entity in stack is '('*/
        {
          tctr=++s->topelem;
          s->elem[tctr]=*inf;
          temp=s->elem[tctr];
          inf++;
        }
       else
        {
          temp=s->elem[tctr];
           if(temp == '*' || temp == '/' || temp == '^')
            {
              tctr=s->topelem;
       temp=s->elem[tctr];
              --s->topelem;
       *pos=temp;
       pos++;
       inf++;
            }
           else if(temp == '+' || temp == '-')
           {
      tctr=s->topelem;
      temp=s->elem[tctr];
      --s->topelem;
      *pos=temp;
      pos++;
      inf++;
      tctr=++s->topelem;
      s->elem[tctr]=*inf;
           }
         }
       }
     else if( isdigit(*inf) != 0)
      {
         temp=*inf;
         *pos=temp;
         pos++;
         inf++;
      }
     else
       printf("\n Not a valid input.");
     }
    }
    main()
    {
    char infix[50], postfix[50];
    struct stack_elem *st;
    clrscr();
    st = &stckmain;
    strcpy(stckmain.topelem, -1);
    strcpy(stckmain.elem[0], "");
    printf("Enter infix statement: ");
    /*scanf("%d",&infix[0]);*/
    fflush(stdin);
    gets(infix);
    intoposconvert(&infix[0], &postfix[0], st);
    printf("\n Result in postfix statement: ");
    printf("%s", postfix);
    getch();
    }
    ***********************************************************************
    Thanks! :)
    -AR
     
    Last edited by a moderator: Oct 29, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Instead of posting the whole code try giving the error descriptions
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    wtf?????
    Code:
    strcpy(stckmain.topelem, -1);
    
    strcpy copies strings. It doesn't do anything else. What string do you think is kept at non-existent address -1 in memory?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You'll find code a lot easier to debug if you indent it correctly. Master programmers
    *always* indent, without fail, because it makes navigating code a lot easier. To investigate your code I started by reformatting it. Make sure you use a programmer-friendly text editor, one that handles indentation correctly and ideally that puts the indentations in for you. If you're using vi, :set ai, and set tabs to something sensible like 4.

    You seem to duplicate the top of stack pointer: tctr and s->topelem. It is better not to
    do this; you don't need to mess around keeping them in step with each other, and you also don't
    risk getting any bugs caused by them accidentally getting out of step. It's fine if you use tctr as temporary storage so that you don't have to keep typing in "s->topelem" all the time, but the code doesn't do that; it relies on these two variables always being equal to each other.

    Have another look at the "while ( temp != '(' )" loop. Why does it increment inf every time? What should it do when "temp=s->elem[tctr];" assigns the '(' to temp? Remember the postfix should NOT include any brackets.

    Also you might want to look at the number handling logic. 5+(4*3) will currently* result in 543*+, which is fine if you only want to handle single digit numbers, but if you intend to handle numbers over 9 then this looks awfully like five hundred and forty-three.

    *i.e. when you've fixed the bugs in the while loop.
     

Share This Page