#include"iostream.h"
#include"conio.h"
#include"string.h"
#include"ctype.h"
class infix
{
int stack[10],target[10],top;
char *s,*t;
public:
void setexpr(char *str);
void push(int c);
void pop();
void convert();
void priority(char x);
} ;
void infix::setexpr(char *str)
{
s=str;
}
void infix::push(int c)
{
if(top==10)
{
cout<<"stack is full:";
}
else
{
top++;
stack[top]=c;
}
}
void infix::pop()
{
if(top==-1)
{
cout<<"stack is empty:";
}
else
{
int item=stack[top];
top--;
}
}
void infix::convert()
{
while(*s)
{
if(*s==' '||*s=='\t')
{
s++;
continue;
}
if(isalpha(*s) || isdigit(*s))
{
while(isalpha(*s) || isdigit(*s))
{
*t=*s;
s++;
t++;
}
}
if(*s=='(')
{
push(*s);
s++;
}
if(*s=='+'||*s=='-'||*s=='*'||*s=='/'||*s=='$'
||*s=='%')
{
if(top!=-1)
{
opr=pop();
if(opr!='(')
{
while(priority(opr) >= priority(*s))
{
*t=opr;
t++;
opr=pop();
}
push(opr);
push(*s);
s++;
}
else
{
push(*t);
t++;
}
}
}
void infix::priority(char x)
{
if(x=='+'||x=='-')
{
return 1;
}
if(x=='$')
{
return 3;
}
if(x=='/'|| x=='*')
{
return 2;
}
else
{
return 0;
}
}
void infix::show()
{
cout<
}
void main()
{
clrscr();
char arr[10];
cout<<"enter the infix experesion:";
cin>>arr;
infix i;
i.setexpr(arr);
i.convert();
i.show();
getch();
}
No comments:
Post a Comment