Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int preced(char ch) {
- if(ch == '>' || ch=='@') {
- return 1;
- }
- else if(ch == '|') {
- return 2;
- }else if(ch == '^') {
- return 3;
- }else if(ch == '!') {
- return 4;
- }else {
- return 0;
- }
- }
- string postfix = "";
- string::iterator it;
- string inToPost(string infix ) {
- stack<char> stk;
- stk.push('#');
- for(it = infix.begin(); it!=infix.end(); it++) {
- if(isalnum(char(*it)))
- postfix += *it;
- else if(*it == '(')
- stk.push('(');
- else if(*it == '^')
- stk.push('^');
- else if(*it == ')') {
- while(stk.top() != '#' && stk.top() != '(') {
- postfix += stk.top();
- stk.pop();
- }
- stk.pop();
- }else {
- if(preced(*it) > preced(stk.top()))
- stk.push(*it);
- else {
- while(stk.top() != '#' && preced(*it) <= preced(stk.top())) {
- postfix += stk.top();
- stk.pop();
- }
- stk.push(*it);
- }
- }
- }
- while(stk.top() != '#') {
- postfix += stk.top();
- stk.pop();
- }
- return postfix;
- }
- int notoperation(int x)
- {
- if(x==1)return 0;
- if(x==0)return 1;
- }
- int evaluate(char ch,int x,int y)
- {
- if(ch=='^') return x&y;
- if(ch=='|') return x|y;
- if(ch=='>') return (x==1 && y==0)?0:1;
- if(ch=='@') return ((x==1 && y==0)||(x==0&&y==1))?0:1;
- }
- int main() {
- string infix = "p^s>t@(q|!r)";
- cout << "Postfix Form Is: " << inToPost(infix) << endl;
- stack<char>eval;
- int value[1000];
- for(it=postfix.end()-1;it>=postfix.begin();it--)
- {
- if(*it>='a' && *it<='z'){
- if(eval.top()=='!') {value[*it]=notoperation(value[*it]),cout<<value[*it]<<endl;
- }
- else eval.push(*it);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement