Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int precidence(char pre)
- {
- if(pre=='!')
- return 2;
- else if(pre=='&'|| pre=='|')
- return 1;
- else
- return -1;
- }
- void operatiobn(string stk)
- {
- char ch,ch1;
- int val1,val2,val;
- for(int i=0; i<2; i++)
- {
- for(int j=0; j<2; j++)
- {
- for(int k=0; k<2; k++)
- {
- cout<<i<<"\t\t"<<j<<"\t\t"<<k<<endl;
- stack<int>stack1;
- stack<char>stack2;
- for(int l=0; l<100; l++)
- {
- if(stk[l]>='p'&& stk[l]<='r')
- {
- if(stk[l]=='p')
- stack1.push(i);
- if(stk[l]=='q')
- stack1.push(j);
- if(stk[l]=='r')
- stack1.push(k);
- }
- else if(stk[l]=='!'||stk[l]=='&'||stk[l]=='|'|| stk[l]=='('||stk[l]==')')
- {
- if(stk[l]=='(')
- stack2.push(stk[l]);
- else if(stk[l]==')')
- {
- while(!stack2.empty())
- {
- ch=stack2.top();
- stack2.pop();
- val1=stack1.top();
- stack1.pop();
- val2=stack1.top();
- stack1.pop();
- switch(ch)
- {
- case'!':
- if(ch=='!')
- {
- val1=!val1;
- stack1.push(val);
- }
- case '&':
- if(ch=='&')
- {
- val=val1&val2;
- stack1.push(val);
- }
- case '|':
- if(ch=='|')
- {
- val=val1|val2;
- stack1.push(val);
- }
- }
- }
- stack2.pop();
- }
- }
- else
- {
- while(!stack2.empty() && precidence(stack2.top())>=precidence(stk[l]))
- {
- ch1=stack2.top();
- stack2.pop();
- val1=stack1.top();
- stack1.pop();
- val2=stack1.top();
- stack1.pop();
- switch(ch)
- {
- case'!':
- if(ch=='!')
- {
- val1=!val1;
- stack1.push(val);
- }
- case '&':
- if(ch=='&')
- {
- val=val1&val2;
- stack1.push(val);
- }
- case '|':
- if(ch=='|')
- {
- val=val1|val2;
- stack1.push(val);
- }
- }
- }
- stack2.push(stk[l]);
- }
- }
- cout<<stack1.top()<<endl;
- }
- }
- }
- }
- int main()
- {
- string stk;
- cout<<"enter the string:";
- cin>>stk;
- cout<<"p\t\tq\t\tr\t\t"<<stk<<endl;
- operatiobn(stk);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement