I'm a newbie here, I just want to seek for help regarding my program. I want to implement an 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:
************************************************** ******************
Expand|Select|Wrap|Line Numbers
- #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