Post to Infix conversion of an expression
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
class infixtoprefix
{
int len;
char strn[100];
stack<string> stackk;
public:
infixtoprefix()
{
cin>>strn;
len=strlen(strn);
}
int convert()
{
int i=0;
while(i<len)
{
if(isalpha(strn[i])||isdigit(strn[i]))
{
string str;
str=strn[i];
stackk.push(str);
}
else
{
string first;
first='(';
string second=stackk.top();
stackk.pop();
first+=stackk.top();
stackk.pop();
first+=strn[i];
first+=second;
first+=')';
stackk.push(first);
}
i++;
}
}
void display()
{
cout<<stackk.top();
}
};
int main()
{
infixtoprefix *obj = new infixtoprefix();
obj->convert();
obj->display();
}
Comments
Post a Comment