1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

why it doesnot works???

Discussion in 'C++' started by evangel, Jul 20, 2009.

  1. evangel

    evangel Banned

    Joined:
    Jul 19, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Nepal,Kathmandu
    Code:
    //Infix to postfix conversion
    
     #include <iostream>
     #include <cstring>
     #define max 20
      using namespace std;
    
     int tonum(char x)
     {
        if (x=='*')
        return 4;
        else if(x=='/')
        return 3;
        else if(x=='+')
        return 2;
        else
        return 1;
     }
     int preced(char x,char y)
     {
        int temp1=tonum(x);
        int temp2=tonum(y);
        if(temp1>=temp2)
        return 1;
        else
        return 0;
     }
     int isopn(char x)
     {
        int y=x;
        if(y<=57 && y>=48)
        return 1;
        else
        return 0;
     }
    
     class conv
     {
        char stack[max];
        int i,j;
        public:
           conv()
           {
           i=-1;
           j=0;
           }
           void push(char x);
           char pop();
           int isempty();
     };
     void conv::push(char x)
     {
        if(i==max-1)
        cout<<"\nStack Overflow: ";
        else
        stack[++i]=x;
     }
     char conv::pop()
     {
        return stack[i--];
     }
     int conv::isempty()
     {
        if(i<0)
        return 1;
        else
        return 0;
     }
    
     int main()
     {
        conv c;
        char out[max];
        char input[max];
        int i,j=0,len;
        char var,current;
        clrscr();
    
        cout<<"\nEnter the input Expression: ";
        cin>>input;
        clrscr();
        cout<<"\nInfix Expression: "<<input;
        len=strlen(input);
        //cout<<"\nlength="<<len;
    
        for(i=0,j=0;i<=len;i++)
        {
            var=input[i];
            if(isopn(var)==1)
            {
                out[j++]=var;
    
            }
            else if(var=='*')
            {
                if(c.isempty()==1)
                {
                    c.push(var);
                }
                else
                {
                current=c.pop();
                if(preced(current,var)==0)
                    c.push(var);
                else
                out[j++]=c.pop();
                }
            }
             else if(var=='/')
            {
                if(c.isempty()==1)
                {
                    c.push(var);
                }
                else
                {
                current=c.pop();
                if(preced(current,var)==0)
                    c.push(var);
                else
                out[j++]=c.pop();
                }
            }
            else if(var=='+')
            {
                if(c.isempty()==1)
                {
                    c.push(var);
                }
                else
                {
                current=c.pop();
                if(preced(current,var)==0)
                    c.push(var);
                else
                out[j++]=c.pop();
                }
            }
            else if(var=='-')
            {
                if(c.isempty()==1)
                {
                    c.push(var);
                }
                else
                {
                current=c.pop();
                if(preced(current,var)==0)
                    c.push(var);
                else
                out[j++]=c.pop();
                }
            }
        }
    
        cout<<"\nEquivalent Postfix conversion is: ";
        cout<<out;
        system("pause");
        return 0;
     }
    
     
    Last edited by a moderator: Jul 21, 2009

Share This Page