By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Infix to Postfix conversion

P: 1
plzzzzzz help me out to run my source code .........
i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source code is given below:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #include<process.h>
  7. const int max=100;
  8. struct stack
  9. { char a[max];
  10.   int top;
  11. }s;
  12.  
  13. void push(char c)
  14. { if(s.top==max)
  15.    cout<<"\n Overflow";
  16.   else
  17.    { s.a[s.top]=c;
  18.      s.top++;
  19.    }
  20. }
  21.  
  22. char pop()
  23. { char x='#';
  24.   if(s.top==0)
  25.    cout<<"\n Underflow";
  26.   else
  27.    { s.top--;
  28.      x=s.a[s.top];
  29.    }
  30.    return(x);
  31. }
  32. void main()
  33. { clrscr();
  34.   stack s;
  35.   s.top=0;
  36.   char in[100],out[100];
  37.   char c;
  38.   int ch,n;
  39.   cout<<"\n Enter the infix expression:";
  40.   gets(in);
  41.  
  42.   n=strlen(in);
  43.  
  44.   for(int i=0;i<n;i++)
  45.   { if(isalpha(in[i]))
  46.      out[i]=in[i];
  47.  
  48.     if(in[i]=='+'||in[i]=='-')
  49.      { if(s.a[s.top]=='^'||s.a[s.top]=='*'||s.a[s.top]=='/'||s.a[s.top]=='+'||s.a[s.top]=='-')
  50.      { c=pop();
  51.        if(c!='#')
  52.        {    out[i]=c;
  53.           push(in[i]);
  54.        }
  55.  
  56.      }
  57.  
  58.     else
  59.      push(in[i]);
  60.      }
  61.     if(in[i]=='*'||in[i]=='/')
  62.        { if(s.a[s.top]=='^'||s.a[s.top]=='*'||s.a[s.top]=='/')
  63.      { c=pop();
  64.        if(c!='#')
  65.        {
  66.        out[i]=c;
  67.        push(in[i]);
  68.        }
  69.      }
  70.  
  71.      else
  72.       push(in[i]);
  73.        }
  74.     if(in[i]=='^')
  75.        { if(s.a[s.top]=='^')
  76.      { c=pop();
  77.        if(c!='#')
  78.        {
  79.        out[i]=c;
  80.        push(in[i]);
  81.        }
  82.      }
  83.      else
  84.       push(in[i]);
  85.        }
  86.     if(in[i]=='(')
  87.      push(in[i]);
  88.     if(in[i]==')')
  89.     { while(s.a[s.top]!='(')
  90.       { c=pop();
  91.     if(c!='#')
  92.     out[i]=c;
  93.       }
  94.       pop();
  95.     }
  96.  }
  97.  while(s.top!=0)
  98. {  c=pop();
  99.    if(c!='#')
  100.     out[i]=c;
  101. }
  102.  cout<<"\n The postfix expression is \n";
  103.  
  104.   puts(out);
  105.  getch();
  106. }
Oct 2 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.