Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int str[200];
  4. int com[600];
  5. void fun(int a){
  6.     int i=0;
  7.     int tmp=a;
  8.     while(tmp!=0){
  9.  
  10.         com[i]=tmp%2;
  11.         tmp=tmp/2;
  12.         i++;
  13.     }
  14. }
  15. int pre(char b)
  16. {
  17.     if(b == '!')
  18.         return 3;
  19.     else if(b == '&' || b == '|')
  20.         return 2;
  21.     else if(b == '-')
  22.         return 1;
  23.     else
  24.         return -1;
  25. }
  26. void eqn(string s)
  27. {
  28.     vector<char> vec;
  29.     int cnt=0;
  30.     for(int i=0;i<s.size();i++){
  31.         if(s[i]>='a'&&s[i]<='z'&&str[s[i]]==0){
  32.             str[s[i]]=1;
  33.             vec.push_back(s[i]);
  34.             cnt++;
  35.         }
  36.     }
  37.  
  38.     sort(vec.begin(),vec.end());
  39.     for(int i=0;i<vec.size();i++){
  40.         cout<<vec[i]<<"  ";
  41.     }
  42.     cout<<s<<endl;
  43.     for(int l=0; l<pow(2,cnt); l++)
  44.     {
  45.         fun(l);
  46.         for(int i=0;i<cnt;i++){
  47.             cout<<com[i]<<"  ";
  48.         }
  49.         stack<char>op;
  50.         stack<int>vl;
  51.         for(int i=0; i<s.size(); i++)
  52.         {
  53.             if(s[i]>='a'&&s[i]<='z'){
  54.                 for(int j=0;j<vec.size();j++){
  55.                     if(vec[j]==s[i]){
  56.                         vl.push(com[j]);
  57.                         break;
  58.                     }
  59.                 }
  60.             }
  61.             else if(s[i]=='(')
  62.                 op.push(s[i]);
  63.             else if(s[i]==')')
  64.             {
  65.                 while(op.top()!='(')
  66.                 {
  67.                     char ch=op.top();
  68.                             op.pop();
  69.                             int vl2=vl.top();
  70.                             vl.pop();
  71.                             if(ch=='!')
  72.                             {
  73.                                 vl2=(!vl2);
  74.                                 vl.push(vl2);
  75.                                 continue;
  76.                             }
  77.                             int  vl1=vl.top();
  78.                             vl.pop();
  79.  
  80.                             if(ch=='&')
  81.                             {
  82.                                 vl1=(vl1&vl2);
  83.                                 vl.push(vl1);
  84.                             }
  85.                             else if(ch=='|')
  86.                             {
  87.                                 vl1=(vl1|vl2);
  88.                                 vl.push(vl1);
  89.                             }
  90.                             else if(ch=='-')
  91.                             {
  92.                                 if(vl1==0 && vl2==0)
  93.                                     vl.push(1);
  94.                                 else
  95.                                     vl.push(vl2);
  96.                             }
  97.                         }
  98.                         op.pop();
  99.                     }
  100.                     else
  101.                     {
  102.                         while(!op.empty() && pre(op.top())>=pre(s[i]))
  103.                         {
  104.                             char c=op.top();
  105.                             op.pop();
  106.                             int vl2=vl.top();
  107.                             vl.pop();
  108.  
  109.                             if(c=='!')
  110.                             {
  111.                                 vl2=(!vl2);
  112.                                 vl.push(vl2);
  113.                                 continue;
  114.                             }
  115.  
  116.                             int  vl1=vl.top();
  117.                             vl.pop();
  118.  
  119.                             if(c=='&')
  120.                             {
  121.                                 vl1=(vl1&vl2);
  122.                                 vl.push(vl1);
  123.                             }
  124.                             else if(c=='|')
  125.                             {
  126.                                 vl1=(vl1|vl2);
  127.                                 vl.push(vl1);
  128.                             }
  129.                             else if(c=='-')
  130.                             {
  131.                                 if(vl1==0 && vl2==0)
  132.                                     vl.push(1);
  133.                                 else
  134.                                     vl.push(vl2);
  135.                             }
  136.                         }
  137.                         op.push(s[i]);
  138.                     }
  139.                 }
  140.  
  141.                 while(!op.empty())
  142.                 {
  143.  
  144.  
  145.                     char ch=op.top();
  146.                     op.pop();
  147.                     int vl2=vl.top();
  148.                     vl.pop();
  149.  
  150.                     if(ch=='!')
  151.                     {
  152.  
  153.                         if(vl2==1)
  154.                             vl2=0;
  155.                         else
  156.                             vl2=1;
  157.                         vl.push(vl2);
  158.                         continue;
  159.                     }
  160.                     int vl1=vl.top();
  161.                     vl.pop();
  162.                     if(ch=='&')
  163.                     {
  164.                         vl1=(vl1&vl2);
  165.                         vl.push(vl1);
  166.                     }
  167.                     else if(ch=='|')
  168.                     {
  169.                         vl1=(vl1|vl2);
  170.                         vl.push(vl1);
  171.                     }
  172.                     else if(ch=='-')
  173.                     {
  174.                         if(vl1==0 && vl2==0)
  175.                             vl.push(1);
  176.                         else
  177.                             vl.push(vl2);
  178.                     }
  179.                 }
  180.  
  181.                 cout<<vl.top()<<endl;
  182.     }
  183.  
  184. }
  185. int main()
  186. {
  187.     string str;
  188.     cin>>str;
  189.     eqn(str);
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement