Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int str[200];
- int com[600];
- void fun(int a){
- int i=0;
- int tmp=a;
- while(tmp!=0){
- com[i]=tmp%2;
- tmp=tmp/2;
- i++;
- }
- }
- int pre(char b)
- {
- if(b == '!')
- return 3;
- else if(b == '&' || b == '|')
- return 2;
- else if(b == '-')
- return 1;
- else
- return -1;
- }
- void eqn(string s)
- {
- vector<char> vec;
- int cnt=0;
- for(int i=0;i<s.size();i++){
- if(s[i]>='a'&&s[i]<='z'&&str[s[i]]==0){
- str[s[i]]=1;
- vec.push_back(s[i]);
- cnt++;
- }
- }
- sort(vec.begin(),vec.end());
- for(int i=0;i<vec.size();i++){
- cout<<vec[i]<<" ";
- }
- cout<<s<<endl;
- for(int l=0; l<pow(2,cnt); l++)
- {
- fun(l);
- for(int i=0;i<cnt;i++){
- cout<<com[i]<<" ";
- }
- stack<char>op;
- stack<int>vl;
- for(int i=0; i<s.size(); i++)
- {
- if(s[i]>='a'&&s[i]<='z'){
- for(int j=0;j<vec.size();j++){
- if(vec[j]==s[i]){
- vl.push(com[j]);
- break;
- }
- }
- }
- else if(s[i]=='(')
- op.push(s[i]);
- else if(s[i]==')')
- {
- while(op.top()!='(')
- {
- char ch=op.top();
- op.pop();
- int vl2=vl.top();
- vl.pop();
- if(ch=='!')
- {
- vl2=(!vl2);
- vl.push(vl2);
- continue;
- }
- int vl1=vl.top();
- vl.pop();
- if(ch=='&')
- {
- vl1=(vl1&vl2);
- vl.push(vl1);
- }
- else if(ch=='|')
- {
- vl1=(vl1|vl2);
- vl.push(vl1);
- }
- else if(ch=='-')
- {
- if(vl1==0 && vl2==0)
- vl.push(1);
- else
- vl.push(vl2);
- }
- }
- op.pop();
- }
- else
- {
- while(!op.empty() && pre(op.top())>=pre(s[i]))
- {
- char c=op.top();
- op.pop();
- int vl2=vl.top();
- vl.pop();
- if(c=='!')
- {
- vl2=(!vl2);
- vl.push(vl2);
- continue;
- }
- int vl1=vl.top();
- vl.pop();
- if(c=='&')
- {
- vl1=(vl1&vl2);
- vl.push(vl1);
- }
- else if(c=='|')
- {
- vl1=(vl1|vl2);
- vl.push(vl1);
- }
- else if(c=='-')
- {
- if(vl1==0 && vl2==0)
- vl.push(1);
- else
- vl.push(vl2);
- }
- }
- op.push(s[i]);
- }
- }
- while(!op.empty())
- {
- char ch=op.top();
- op.pop();
- int vl2=vl.top();
- vl.pop();
- if(ch=='!')
- {
- if(vl2==1)
- vl2=0;
- else
- vl2=1;
- vl.push(vl2);
- continue;
- }
- int vl1=vl.top();
- vl.pop();
- if(ch=='&')
- {
- vl1=(vl1&vl2);
- vl.push(vl1);
- }
- else if(ch=='|')
- {
- vl1=(vl1|vl2);
- vl.push(vl1);
- }
- else if(ch=='-')
- {
- if(vl1==0 && vl2==0)
- vl.push(1);
- else
- vl.push(vl2);
- }
- }
- cout<<vl.top()<<endl;
- }
- }
- int main()
- {
- string str;
- cin>>str;
- eqn(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement