Advertisement
Guest User

Untitled

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