Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include <algorithm>
- using namespace std;
- float factor(string s);
- float calc(string s);
- string replaceStr(string str, string a, string b);
- int rec=0, pos=0;
- int main(){
- float result;
- string s;
- cout << "Enter boolean expression: " << endl;
- getline(cin, s);
- s.insert(s.size(), " ");
- s = replaceStr(s, "NOT TRUE", "0");
- s = replaceStr(s, "NOT FALSE", "1");
- s = replaceStr(s, "TRUE", "1");
- s = replaceStr(s, "FALSE", "0");
- s = replaceStr(s, "OR", "|");
- s = replaceStr(s, "AND", "&");
- s = replaceStr(s, " ", "");
- result = calc(s);
- cout << "Result: " << result << endl;
- cout << "Depth of Recursion: " << rec;
- return 0;
- }
- string replaceStr(string str, string a, string b){
- int start = 0;
- while((start = str.find(a, start)) != string::npos){
- str.replace(start, a.length(), b);
- start += b.length();
- }
- return str;
- }
- float calc(string s){
- float x = factor(s);
- char c = s.at(pos++);
- if(c == '|')
- return x || calc(s);
- else{
- pos--;
- return x;
- }
- }
- float factor(string s){
- float x;
- char c = s.at(pos++);
- if(c == '('){
- rec++;
- x = calc(s);
- pos++;
- }
- else{
- x = c - '0';
- }
- c = s.at(pos++);
- if(c == '&')
- return x && factor(s);
- else{
- pos--;
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement