Program converts the given infix expression in to postfix form



#include
#include
#include
#include

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< <<”in to postfix form”;
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<}
int main()
{
expression obj;
obj.convert();
return 0;
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!