Prefix 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=len-1;
        while(i>=0)
        {
            if(isalpha(strn[i])||isdigit(strn[i]))
            {
                 string str;
                 str=strn[i];
                 stackk.push(str);
            }

            else
            {
                string first;
                first='(';
                first+=stackk.top();
                stackk.pop();
                string second=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

Popular Posts