Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. float factor(string s);
  7. float calc(string s);
  8. string replaceStr(string str, string a, string b);
  9. int rec=0, pos=0;
  10.  
  11. int main(){
  12.     float result;
  13.     string s;
  14.     cout << "Enter boolean expression: " << endl;
  15.     getline(cin, s);
  16.     s.insert(s.size(), " ");
  17.     s = replaceStr(s, "NOT TRUE", "0");
  18.     s = replaceStr(s, "NOT FALSE", "1");
  19.     s = replaceStr(s, "TRUE", "1");
  20.     s = replaceStr(s, "FALSE", "0");
  21.     s = replaceStr(s, "OR", "|");
  22.     s = replaceStr(s, "AND", "&");
  23.     s = replaceStr(s, " ", "");
  24.     result = calc(s);
  25.     cout << "Result: " << result << endl;
  26.     cout << "Depth of Recursion: " << rec;
  27.     return 0;
  28. }
  29.  
  30. string replaceStr(string str, string a, string b){
  31.     int start = 0;
  32.     while((start = str.find(a, start)) != string::npos){
  33.         str.replace(start, a.length(), b);
  34.         start += b.length();
  35.     }
  36.     return str;
  37. }
  38.  
  39. float calc(string s){
  40.     float x = factor(s);
  41.     char c = s.at(pos++);
  42.     if(c == '|')
  43.         return x || calc(s);
  44.     else{
  45.         pos--;
  46.         return x;
  47.     }
  48. }
  49.  
  50. float factor(string s){
  51.     float x;
  52.     char c = s.at(pos++);
  53.     if(c == '('){
  54.         rec++;
  55.         x = calc(s);
  56.         pos++;
  57.     }
  58.     else{
  59.         x = c - '0';
  60.     }
  61.     c = s.at(pos++);
  62.     if(c == '&')
  63.         return x && factor(s);
  64.     else{
  65.         pos--;
  66.         return x;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement