infix to postfix conversion


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

-> 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

97 thoughts on “infix to postfix conversion

  1. 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

  2. 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…….

  3. Pingback: INDEX « Data Structures through C & C++ for beginners

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

  5. 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. :)

  6. 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…

  7. 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 )

Google+ photo

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

Connecting to %s