#include<iostream.h>
#include<conio.h>
#inlcude<ctype.h>
class infix
{
private:
cahr s[50];
int item;
public:
void getstr();
void putstr();
int prior (char);
};
void infix::getstr()
{
cout<<"\n\t enter the infix expression is:";
cin>>s;
}
int infix::prior(char m)
{
int p;
switch(m){
case ')':
p=4;
break;
case '^':
p=3;
break;
case '*':
p=-2;
break;
case '/':
p=2;
break;
case '+':
p=1;
break;
case '-':
p=0;
break;
case '(':
p=0;
break;
case '#':
p=-1;
break;
}
return (p);
}
void infix::putstr()
{
cout<<"\n\t the postfix expression is:";
char stk[50],item;
int i=0;
int top=0;
stk[top]='#';
while(s[i]!='\0')
{
item=s[i];
if (isalpha(item))
cout<<item;
else
if(item==')')
{
while (stk[top]!='(')
cout<<stk[top--1];
top--;
}
else
{
while(prior(stk[top])>=prior(item))
cout<<stk[top--];
top--;
}
else
{
while(prior(stk[top])>=prior(item))
couit<<stk[top--];
}}
void main(){
infix f;
clrscr();
cout<<"\n\tconversion of infix to postfix expression"<<endl;
cout<<"\n\t*******************************************"<<endl;
f.getstr();
f.putstr();
getch();
}
Conversion of infix to postfix expression
Input:
Enter the infix expression is:(a+b)/c
Output:
The postfix expression is:ab+c/
#include<conio.h>
#inlcude<ctype.h>
class infix
{
private:
cahr s[50];
int item;
public:
void getstr();
void putstr();
int prior (char);
};
void infix::getstr()
{
cout<<"\n\t enter the infix expression is:";
cin>>s;
}
int infix::prior(char m)
{
int p;
switch(m){
case ')':
p=4;
break;
case '^':
p=3;
break;
case '*':
p=-2;
break;
case '/':
p=2;
break;
case '+':
p=1;
break;
case '-':
p=0;
break;
case '(':
p=0;
break;
case '#':
p=-1;
break;
}
return (p);
}
void infix::putstr()
{
cout<<"\n\t the postfix expression is:";
char stk[50],item;
int i=0;
int top=0;
stk[top]='#';
while(s[i]!='\0')
{
item=s[i];
if (isalpha(item))
cout<<item;
else
if(item==')')
{
while (stk[top]!='(')
cout<<stk[top--1];
top--;
}
else
{
while(prior(stk[top])>=prior(item))
cout<<stk[top--];
top--;
}
else
{
while(prior(stk[top])>=prior(item))
couit<<stk[top--];
}}
void main(){
infix f;
clrscr();
cout<<"\n\tconversion of infix to postfix expression"<<endl;
cout<<"\n\t*******************************************"<<endl;
f.getstr();
f.putstr();
getch();
}
Conversion of infix to postfix expression
Input:
Enter the infix expression is:(a+b)/c
Output:
The postfix expression is:ab+c/
It it a stack that you've created , is'nt it? how is it called queue ???
ReplyDelete