Advertisement
vencinachev

BoolCalculate

May 3rd, 2021
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. string postfixC(string infix)
  7. {
  8.     string output = "";
  9.     stack<char> operators;
  10.     for (int i = 0; i < infix.length(); i++)
  11.     {
  12.         if (infix[i] == 't' || infix[i] == 'f')
  13.         {
  14.             output += infix[i];
  15.         }
  16.         else if (infix[i] == '(')
  17.         {
  18.             operators.push(infix[i]);
  19.         }
  20.         else if (infix[i] == ')')
  21.         {
  22.             while (operators.top() != '(')
  23.             {
  24.  
  25.                 output += operators.top();
  26.                 operators.pop();
  27.             }
  28.             operators.pop();
  29.         }
  30.         else if (infix[i] == '!')
  31.         {
  32.             operators.push(infix[i]);
  33.         }
  34.         else if (infix[i] == '*')
  35.         {
  36.             while (!operators.empty() && (operators.top() == '*' || operators.top() == '!'))
  37.             {
  38.                     output += operators.top();
  39.                     operators.pop();
  40.             }
  41.             operators.push(infix[i]);
  42.         }
  43.         else if (infix[i] == '+')
  44.         {
  45.             while (!operators.empty() && (operators.top() == '*' || operators.top() == '!' || operators.top() == '+'))
  46.             {
  47.                     output += operators.top();
  48.                     operators.pop();
  49.             }
  50.             operators.push(infix[i]);
  51.         }
  52.     }
  53.  
  54.     while (!operators.empty())
  55.     {
  56.         output += operators.top();
  57.         operators.pop();
  58.     }
  59.     return output;
  60. }
  61.  
  62. bool calculatePostfix(string postfix)
  63. {
  64.     stack<char> st;
  65.     for (int i = 0; i < postfix.length(); i++)
  66.     {
  67.         if (postfix[i] == 't' || postfix[i] == 'f')
  68.         {
  69.             st.push(postfix[i]);
  70.         }
  71.         else if (postfix[i] == '*')
  72.         {
  73.             bool operandB = (st.top() == 't') ? true : false;
  74.             st.pop();
  75.             bool operandA = (st.top() == 't') ? true : false;
  76.             st.pop();
  77.             if (operandA && operandB)
  78.             {
  79.                 st.push('t');
  80.             }
  81.             else
  82.             {
  83.                 st.push('f');
  84.             }
  85.         }
  86.         else if (postfix[i] == '+')
  87.         {
  88.             bool operandB = (st.top() == 't') ? true : false;
  89.             st.pop();
  90.             bool operandA = (st.top() == 't') ? true : false;
  91.             st.pop();
  92.             if (operandA || operandB)
  93.             {
  94.                 st.push('t');
  95.             }
  96.             else
  97.             {
  98.                 st.push('f');
  99.             }
  100.         }
  101.         else if (postfix[i] == '!')
  102.         {
  103.             bool operand = (st.top() == 't') ? true : false;
  104.             st.pop();
  105.             if (!operand)
  106.             {
  107.                 st.push('t');
  108.             }
  109.             else
  110.             {
  111.                 st.push('f');
  112.             }
  113.         }
  114.     }
  115.     return (st.top() == 't');
  116. }
  117.  
  118. int main()
  119. {
  120.     string expr;
  121.     cout << "Enter expression: ";
  122.     getline(cin, expr);
  123.     if (calculatePostfix(postfixC(expr)))
  124.     {
  125.         cout << 't' << endl;
  126.     }
  127.     else
  128.     {
  129.         cout << 'f' << endl;
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement