Pages

Friday, April 10, 2015

Converting Infix to Postfix using Stacks in C++


#include<iostream>
#include<conio.h>
#include<cstring>
#include<stdlib.h>
using namespace std;
const char size=100;
class stack
{
private:
                char data[size];
                int top;
public:  
                int topnum;
                bool isempty()
                {
                                return top==-1;
                }
                bool isfull()
                {
                                return top==size-1;
                }
                void push(char j)
                {
                                if(isfull())
                                {
                                                cout<<"cannot add new item "<<endl;
                                }
                                else
                                {
                                                top++;
                                                data[top]=j;
                                                topnum=top;
                                }
                }
                void pop(char &j)
                {
                                if (isempty())
                                {
                                                cout<<"stack full"<<endl;
                                }
                                else
                                {
                                                topnum=top;
                                                j=data[top];
                                                top--;
                                }
                }

                void display()
                {
                                cout<<"The Postfix Form Is : ";
                                for(int i=0;i<=top;i++)
                                {
                                                cout<<data[i];
                                }
                }
};

void main()
{
                stack a;
                stack b;
                char c;
                char infex[size];
                cout<<" Enter The  Infex Formula :";
        cin.get(infex,size);
       
        char ch;
        int lenght;
        lenght=strlen(infex);
        for(int i=0;i<lenght;i++)
        {
                if(infex[i]=='+'||infex[i]=='-'||infex[i]=='*'||infex[i]=='/'||infex[i]=='('||infex[i]==')')
                {
                        if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(')
                                a.push(infex[i]);                                                                                                              
                        else if(infex[i]=='+'||infex[i]=='-')
                        {
                                if(a.topnum=='*'||a.topnum=='/')
                                {
                                        a.pop(ch);
                                       c=ch;
                                        while(ch!='('&& ch!=a.isempty())
                                        {
                                                b.push(ch);
                                                a.pop(ch);

                                        }
                                        a.push(infex[i]);
                                }
                                else
                                        a.push(infex[i]);
                                }
                        else if(infex[i]==')')
                        {
                                a.pop(ch);
                                c=ch;
                                while(c!='(')
                                {
                                        b.push(c);
                                        a.pop(ch);
                                        c=ch;
                                }
                        }
                        }
                else
                        b.push(infex[i]);
                }
        while(!a.isempty())
        {
                a.pop(ch);
               c=ch;
                b.push(c);    
        }
        b.display();
        cout<<endl;      
getch();
}

No comments:

Post a Comment