News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

infix _to_ postfix_conversion

Started by aruljothi, Mar 31, 2009, 11:00 AM

Previous topic - Next topic

aruljothi

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define N 100

void push(char *stk,int *sp,char item);
char pop(char *stk,int *sp);


void main()
{
  static  char stack[N],target[N];
  char  *t,*s,n1,n2,item,str[N];
  int p1,p2,i,top=-1;
  clrscr();
  printf("
Enter the infix notation :");
  gets(str);
  s=str;
  t=target;
  while(*s)
   {
    if(*s==' ' || *s=='   ')
    {
   s++;
     continue;
    }
   if(isdigit(*s) || isalpha(*s))
   {
   while(isdigit(*s) || isalpha(*s))
      {
       *t=*s;
       t++;
       s++;
      }
   }

   if(*s=='(')
   {
     push(stack,&top,*s);
     s++;
   }

   if(*s==')')
   {
    n1=pop(stack,&top);
    while(n1!='(')
     {
       // n1=pop(stack,&top);
      *t=n1;
      t++;
      n1=pop(stack,&top);
      }
    s++;
    }
  if(*s=='+'||*s=='*'||*s=='/'||*s=='%'||*s=='-')
    {
   if(top==-1)
     {
   push(stack,&top,*s);
   }
   else
   {
     n1=pop(stack,&top);
     while(priority(n1)>=priority(*s))
       {
        *t=n1;
        t++;
        n1=pop(stack,&top);
        }
       push(stack,&top,n1);
       push(stack,&top,*s);
        }
      s++;
       }
   }


while(top!= -1)
{
   n1=pop(stack,&top);
   *t=n1;
   t++;
}

t='