Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   infix to postfix (http://www.go4expert.com/forums/infix-to-postfix-t19916/)

zeroeight 29Oct2009 11:23

infix to postfix
 
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

shabbir 29Oct2009 14:56

Re: infix to postfix
 
Instead of posting the whole code try giving the error descriptions

xpi0t0s 30Oct2009 13:41

Re: infix to postfix
 
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?

xpi0t0s 30Oct2009 14:27

Re: infix to postfix
 
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.


All times are GMT +5.5. The time now is 23:29.