Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int preced(char ch) {
  5.     if(ch == '>' || ch=='@') {
  6.       return 1;
  7.    }
  8.    else if(ch == '|') {
  9.       return 2;
  10.    }else if(ch == '^') {
  11.       return 3;
  12.    }else if(ch == '!') {
  13.       return 4;
  14.    }else {
  15.       return 0;
  16.    }
  17. }
  18. string postfix = "";
  19. string::iterator it;
  20. string inToPost(string infix ) {
  21.    stack<char> stk;
  22.    stk.push('#');
  23.  
  24.  
  25.  
  26.    for(it = infix.begin(); it!=infix.end(); it++) {
  27.       if(isalnum(char(*it)))
  28.          postfix += *it;
  29.       else if(*it == '(')
  30.          stk.push('(');
  31.       else if(*it == '^')
  32.          stk.push('^');
  33.       else if(*it == ')') {
  34.          while(stk.top() != '#' && stk.top() != '(') {
  35.             postfix += stk.top();
  36.             stk.pop();
  37.          }
  38.          stk.pop();
  39.       }else {
  40.          if(preced(*it) > preced(stk.top()))
  41.             stk.push(*it);
  42.          else {
  43.             while(stk.top() != '#' && preced(*it) <= preced(stk.top())) {
  44.                postfix += stk.top();
  45.                stk.pop();
  46.             }
  47.             stk.push(*it);
  48.          }
  49.       }
  50.    }
  51.  
  52.    while(stk.top() != '#') {
  53.       postfix += stk.top();
  54.       stk.pop();
  55.    }
  56.  
  57.    return postfix;
  58. }
  59. int notoperation(int x)
  60. {
  61.     if(x==1)return 0;
  62.     if(x==0)return 1;
  63. }
  64.  
  65. int evaluate(char ch,int x,int y)
  66. {
  67.     if(ch=='^') return x&y;
  68.     if(ch=='|') return x|y;
  69.     if(ch=='>')  return (x==1 && y==0)?0:1;
  70.     if(ch=='@')  return ((x==1 && y==0)||(x==0&&y==1))?0:1;
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int main() {
  79.    string infix = "p^s>t@(q|!r)";
  80.    cout << "Postfix Form Is: " << inToPost(infix) << endl;
  81.    stack<char>eval;
  82.    int value[1000];
  83.  
  84.    for(it=postfix.end()-1;it>=postfix.begin();it--)
  85.    {
  86.         if(*it>='a' && *it<='z'){
  87.             if(eval.top()=='!') {value[*it]=notoperation(value[*it]),cout<<value[*it]<<endl;
  88.  
  89.         }
  90.         else eval.push(*it);
  91.  
  92.    }
  93.  
  94.  
  95.    }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement