Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

infix to postfix conversion

Posted by Vinod on October 18, 2006


/**********************************************************************

-> This C++ Program is to convert a given infix expression
   (either parenthesized or unparenthesized) to postfix form

-> Ex. of infix expression is ::(a+b^c^d)*(c+d)

-> Data Structers used
     Stack:array

-> This program works in microsoft vc++ 6.0 environment.

************************************************************************/

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

class expression
{
private:
 char infix[100];
 char stack[200];
 int top;
 int r;
 char postfix[100];
public:
 void convert();
 int input_p(char);
 int stack_p(char);
 int rank(char);
};

int expression::input_p(char c)
{
 if(c==’+’ || c==’-')
  return 1;
 else if(c==’*’ || c==’/')
  return 3;
 else if(c==’^')
  return 6;
 else if(isalpha(c)!=0)
  return 7;
 else if(c==’(‘)
  return 9;
 else if(c==’)')
  return 0;
 else
 {
  cout<<”Invalid expression ::input error\n”;
  exit(0);
 }
}

int expression::stack_p(char c)
{
 if(c==’+’ || c==’-')
  return 2;
 else if(c==’*’ || c==’/')
  return 4;
 else if(c==’^')
  return 5;
 else if(isalpha(c)!=0)
  return 8;
 else if(c==’(‘)
  return 0;
 else
 {
  cout<<”Invalid expression  ::stack error\n”;
  exit(0);
 }
}

int expression::rank(char c)
{
 if(c==’+’ || c==’-')
  return -1;
 else if(c==’*’ || c==’/')
  return -1;
 else if(c==’^')
  return -1;
 else if(isalpha(c)!=0)
  return 1;
 else
 {
  cout<<”Invalid expression ::in rank\n”;
  exit(0);
 }
}

void expression::convert()
{
 cout<<”\n*************************************************\n”
  <<”This program converts the given infix expression\n”
  <<”in to postfix form”
                <<”\n*************************************************\n”;
 cout<<”Enter an infix expression ::\n”;
 cin>>infix;
 int l=strlen(infix);

 infix[l]=’)';
 infix[l+1]=”;

 //Convertion starts
 top=1;
 stack[top]=’(‘;

 r=0;
 int x=-1;

 int i=0;
 char next=infix[i];

 while(next!=”)
 {
  //Pop all the elements to outputin stack which have higher precedence
  while( input_p(next) < stack_p(stack[top]) )
  {
   if(top<1)
   {
    cout<<”invalid expression ::stack error\n”;
    exit(0);
   }

   postfix[++x]=stack[top];
   top–;

   r=r+rank(postfix[x]);
   
   if(r<1)
   {
    cout<<”Invalid expression  ::r<1\n”;
    exit(0);
   }
  }

  if(input_p( next ) != stack_p( stack[top]))
   stack[++top]=next;
  else
   top–;

  i++;
  next=infix[i];
 }
 postfix[++x]=”;

 if(r!=1 || top!=0)
 {
  cout<<”Invalid expression ::error in rank or stack\n”;
  exit(0);
 }

 cout<<”\n\nThe corresponding postfix expression is ::\n”;
 cout<<postfix<<endl;
}
int main()
{
 expression obj;
 obj.convert();
 return 0;
}
/************************************************************************

SAMPLE OUTPUT::
—————
 
*************************************************
This program converts the given infix expression
in to postfix form
*************************************************
Enter an infix expression ::
(a+b^c^d)*(c+d)
The corresponding postfix expression is ::
abcd^^+cd+*
Press any key to continue
**************************************************************************/

About these ads

95 Responses to “infix to postfix conversion”

  1. Umesh said

    Idiot

  2. Pia said

    this code doesn’t run…and has so many errors.

  3. atul jadhav said

    very efficient program
    thanking you

  4. Harsh said

    this code does not run…has got sooooo many errors..

  5. Yashar said

    thank you for writing this great one. i really needed it. mer30

  6. akash said

    i hate programs

  7. rajesh said

    it is very use ful

  8. aref said

    thank u very much
    this is very use full for me
    this is my project

  9. coder said

    lolz… moron..

  10. pEpo said

    nicE one

  11. samik said

    Great program! some modifications to be made if it is to be run in turbo C, which any programmer can do with his eyes closed

  12. albert said

    thanks this program bcoz dis is our project!!!!!tnks…

  13. PAKhawk said

    Vinods its a Good Hint of postExps for newbies 2 DS.
    Great!

  14. Launch said

    The program has so many errors…pls correct

  15. shalra said

    sooo bad code write ever u r fail……..

  16. hana said

    have u program convert infix to postfix using stack implementation linked list ?

  17. elie said

    thats my project thank you soo mu ch but can u repair for me cz there is 222 much errors plz i rly appreciate what u will do 10x

  18. adi said

    works like a dream , thanks a lot !!!!

  19. mlk said

    dn’t work
    :P

  20. new C++ said

    how to write A*(B+C)*D infix to postfix

  21. rema said

    thanks for you
    good job ;D

  22. zulfikar said

    Good man….

    But i have a problem with this?
    Q. i need function for expression like
    ( ( 10000000000000000000000001231234448563465435434723854278423 / 1111111111234623874627 ) *
    (2342384523 + 123124 – 34534534 ) * ( 1231263123242346 + 223423234346 * 234236536 ) )

    Need the algorithm or code

    If u can do it
    Mail me…….

  23. kaviraj said

    i need program coding for binary tree traversal…..pls send me dear frndzzz………..my id kavi.skr.raj@gmail.com

  24. jhen said

    for infix to postfix conversion

    it does’nt run,pls pki edit naman poh kac proj namin yan pls

  25. mahesh said

    good job man!
    tis codin was really useful for me.
    thanks a lot.

  26. abhay bhuva said

    aa program c ma banav…

  27. Rithu said

    hey ths program is not in C language…

  28. uden said

    difficult for me to understand nd it’s so long.

  29. [...] Ø infix to postfix conversion [...]

  30. analyn said

    ” tHERE’S something wrong in your program!!!!!!!!!!!!”

  31. karen said

    there are so many errors

  32. Eng.ch bilawal rehman said

    so gud i like it
    but not so tough

  33. jhayehm said

    are u using eclipse

  34. jhayehm said

    are u using java eclipse ???

  35. sds said

    Apna dimag bhi Lagao

  36. dev said

    begin new world with c | c++ | c sharp | data structure | more
    with code

  37. dev said

    adwaasd

  38. Sunil said

    A great article on ‘Infix to Postfix Conversion’ with Visuals and Examples at the below link:

    http://www.rawkam.com/?p=762

  39. ognub said

    What the fuck is tis? loosing my time. so many errors..
    run it before publishing next tym……….

  40. pwede bang malaman ung code sa tc++ using goto

  41. prapti said

    I can’t understand this

  42. Anand said

    Ha ha very useful program

  43. jenefer said

    Y0u IDIOT
    What the hell is this
    your program is useless & has incomplete expressions
    Run it & then publish otherwise take it & go to HELL

  44. rakie said

    inta pedda program et;la nerchukuntaru ra

  45. soli said

    Hi:

    i have a project same as what you did , i have a problem to complile.can you help me?

  46. soli said

    i copy this code and try to run it but it’s not working.

  47. minal said

    i hate programing:(

  48. MJ said

    do you have this program in C?

  49. iLLuSioN said

    wooow man…. simple errors. THIS WORKS.

    CHANGE ALL THE SINGLE QUOTES, MAYBE THE SITE F*ED THEM UP. PUT NEW ONES.

    AND PUT top– where the programmer had put only top- (2 occurrences in total).

    Anyone who’ve tried making this once could do it easily after reading this one. :)

  50. Veec said

    //infix[l+1]=”;

    what is the above line all about?? why isit infix equals to ” symbol??

  51. Mohammad NMohammad said

    Thank you for this code I have it as homework

  52. Prince said

    What d hell u r mr…

  53. Prince said

    Who is this fu..er
    all people who say that this is a good work are mo..er fu..er
    go to hell u all si..er fu..er
    who fu..er says that this fucker done a great job
    first know progran n coding then try to upload such a copy code
    else insert fingets in ur asssss
    go to hell u all who appreciate this fuck code…

  54. NOLSKEEE said

    this code sucks!
    this is just our assignment..

  55. HVB said

    I executed the program and got 25 errors… why the hell this program is uploaded on net

  56. muneeb said

    yar very good job
    an efficient program

    those who are saying that there are errors
    these errors are only the symbolic errors

  57. ali said

    For The Best Code and Solution to all other Problems Visit:
    http://pakk-hungama.blogspot.com

  58. Mishi said

    Yes this site is Really awsome. do visit. Wonderfullllll..Thanx Ali
    http://pakk-hungama.blogspot.com

  59. July said

    Thanx Ali. it Help me Alot.

  60. Syed said

    If you want c++ code for conversion of Infix to Postfix an evaluation contact me @ gousekaleemuddin.syed@gmail.com

  61. Prateek Mathur said

    Can we have the same program in C too??

  62. NiNo said

    how to write (A+B^C^D)*(C+D) in a postfix expression??

  63. madhav said

    code is understandeable but too lengthy can sum one make it bit shorter

  64. this program is very useful.thank you brother.

  65. ayesha said

    it has got 102 errors :( i thought it would be all perfect but alas!

  66. Abelfeid hussen said

    It is intersting!

  67. sambodhi sharma said

    it is nice
    no no very nice
    keep it up frd’s

  68. sambodhi sharma said

    thnx vinod

  69. This program very nice, this is my destiny, thank you very much :)

  70. niyaz said

    no logic is there and prgm is very lengthy,,

  71. madhu said

    not running dude

  72. chetan said

    very poor

  73. its very complex logic

  74. Zei said

    why is my result turns like this
    Infix: a+b
    Postfix: ab+#$@#$@$

  75. mandana said

    thanks for the code. but there is a problem, it closes the window immediately after showing the answer,so i had to add system(“PAUSE”). and noww it shows some character after the answer. can u help me solve this problem plz?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 102 other followers

%d bloggers like this: