Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int prece(char c)
- {
- if(c=='!')
- return 3;
- else if(c=='&'||c=='|')
- return 2;
- else if(c=='->'||c=='<->')
- return 1;
- else
- return -1;
- }
- int infixtopostfix(string s,int p, int q,int r)
- {
- for(p=0;p<2;p++)
- for(q=0;q<2;q++)
- for(r=0;r<2;r++)
- {
- cout<<p<<"\t"<<q<<"\t"<<r<<endl;
- stack<char>st;
- st.push('N');
- //int l=s.length();
- stack<char> ns;
- for(int i=0; i<s.size(); i++)
- {
- // if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))
- if(s[i]>='p' && s[i]<='r')
- {
- // ns.push(s[i]);
- if(s[i]=='p')
- ns.push(p);
- if(s[i]=='q')
- ns.push(q);
- if(s[i]=='r')
- ns.push(r);
- }
- else if(s[i]=='(')
- st.push(s[i]);
- else if(s[i]==')')
- {
- while(st.top()!='N' && st.top()!='(')
- {
- char c=st.top();
- st.pop();
- ns.push(c);
- }
- if(st.top()=='(')
- {
- char c=st.top();
- st.pop();
- }
- }
- else
- {
- while(st.top()!='N' && prece(s[i])<=prece(st.top()))
- {
- char c=st.top();
- st.pop();
- ns.push(c);
- }
- st.push(s[i]);
- }
- }
- while(st.top()!='N')
- {
- char c=st.top();
- st.pop();
- ns.push(c);
- }
- while(!ns.empty())
- {
- cout<<ns.top();
- char c = ns.top();
- ns.pop();
- if(c=='!')
- {
- char x = ns.top();
- ns.pop();
- //ns.push(negative(x));
- if(x==1)
- x=0;
- else
- x=1;
- //ns.push(z);
- }
- else if(c=='&')
- {
- char x=ns.top();
- ns.pop();
- char y=ns.top();
- ns.pop();
- int z=x&y;
- ns.push(z);
- }
- else if(c=='|')
- {
- char x=ns.top();
- ns.pop();
- char y=ns.top();
- ns.pop();
- char z=x|y;
- ns.push(z);
- }
- else if(c=='->')
- {
- char x=ns.top();
- ns.pop();
- char y=ns.top();
- ns.pop();
- if(x==1 && y==0)
- return 0;
- else
- return 1;
- }
- ns.pop();
- }
- return ns.top();
- }
- }
- int main()
- {
- char s[100];
- int p,q,r;
- cout<<"Enter a string:"<<endl;
- cin>>s;
- // for(int i=0; i<2; i++)
- // {
- // for(int j=0; j<2; j++)
- // {
- // for(int k=0; k<2; k++)
- // {
- int x= infixtopostfix(s,0,0,0);
- // cout<<i<<"\t"<<j<<"\t"<<k<<"\t"<<s<<endl;
- // }
- // }
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement