Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int prece(char c)
  4. {
  5.     if(c=='!')
  6.         return 3;
  7.     else if(c=='&'||c=='|')
  8.         return 2;
  9.     else if(c=='->'||c=='<->')
  10.         return 1;
  11.     else
  12.         return -1;
  13. }
  14. int infixtopostfix(string s,int p, int q,int r)
  15. {
  16.     for(p=0;p<2;p++)
  17.         for(q=0;q<2;q++)
  18.            for(r=0;r<2;r++)
  19.     {
  20.      cout<<p<<"\t"<<q<<"\t"<<r<<endl;
  21.     stack<char>st;
  22.     st.push('N');
  23.     //int l=s.length();
  24.     stack<char> ns;
  25.     for(int i=0; i<s.size(); i++)
  26.     {
  27.        // if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))
  28.        if(s[i]>='p' && s[i]<='r')
  29.         {
  30.  
  31.            // ns.push(s[i]);
  32.            if(s[i]=='p')
  33.             ns.push(p);
  34.             if(s[i]=='q')
  35.                 ns.push(q);
  36.             if(s[i]=='r')
  37.                 ns.push(r);
  38.         }
  39.  
  40.         else if(s[i]=='(')
  41.             st.push(s[i]);
  42.         else if(s[i]==')')
  43.         {
  44.             while(st.top()!='N' && st.top()!='(')
  45.             {
  46.                 char c=st.top();
  47.                 st.pop();
  48.                 ns.push(c);
  49.             }
  50.             if(st.top()=='(')
  51.             {
  52.                 char c=st.top();
  53.                 st.pop();
  54.             }
  55.  
  56.         }
  57.         else
  58.         {
  59.             while(st.top()!='N' && prece(s[i])<=prece(st.top()))
  60.             {
  61.                 char c=st.top();
  62.                 st.pop();
  63.                 ns.push(c);
  64.             }
  65.             st.push(s[i]);
  66.         }
  67.     }
  68.     while(st.top()!='N')
  69.     {
  70.         char c=st.top();
  71.         st.pop();
  72.         ns.push(c);
  73.     }
  74.  
  75.     while(!ns.empty())
  76.     {
  77.         cout<<ns.top();
  78.         char c = ns.top();
  79.         ns.pop();
  80.         if(c=='!')
  81.         {
  82.             char x = ns.top();
  83.             ns.pop();
  84.             //ns.push(negative(x));
  85.             if(x==1)
  86.                 x=0;
  87.             else
  88.                 x=1;
  89.             //ns.push(z);
  90.         }
  91.         else if(c=='&')
  92.             {
  93.                 char x=ns.top();
  94.         ns.pop();
  95.         char y=ns.top();
  96.         ns.pop();
  97.         int z=x&y;
  98.         ns.push(z);
  99.  
  100.         }
  101.         else if(c=='|')
  102.         {
  103.             char x=ns.top();
  104.             ns.pop();
  105.             char y=ns.top();
  106.             ns.pop();
  107.             char z=x|y;
  108.             ns.push(z);
  109.         }
  110.         else if(c=='->')
  111.         {
  112.              char x=ns.top();
  113.              ns.pop();
  114.              char y=ns.top();
  115.              ns.pop();
  116.             if(x==1 && y==0)
  117.                 return 0;
  118.             else
  119.                 return 1;
  120.  
  121.         }
  122.  
  123.  
  124.  
  125.         ns.pop();
  126.     }
  127.     return ns.top();
  128. }
  129. }
  130.  
  131. int main()
  132. {
  133.  
  134.     char s[100];
  135.     int p,q,r;
  136.     cout<<"Enter a string:"<<endl;
  137.     cin>>s;
  138. //    for(int i=0; i<2; i++)
  139. //    {
  140. //        for(int j=0; j<2; j++)
  141. //        {
  142. //            for(int k=0; k<2; k++)
  143. //            {
  144.                 int x= infixtopostfix(s,0,0,0);
  145. //                cout<<i<<"\t"<<j<<"\t"<<k<<"\t"<<s<<endl;
  146. //            }
  147. //        }
  148. //    }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement