Evaluate a boolean expression represented as string | C ++ program to evaluate value of an expression . ; Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evaluateBoolExpr ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( s [ i + 1 ] == ' A ' ) { if ( s [ i + 2 ] == '0' s [ i ] == '0' ) s [ i + 2 ] = '0' ; else s [ i + 2 ] = '1' ; } else if ( s [ i + 1 ] == ' B ' ) { if ( s [ i + 2 ] == '1' s [ i ] == '1' ) s [ i + 2 ] = '1' ; else s [ i + 2 ] = '0' ; } else { if ( s [ i + 2 ] == s [ i ] ) s [ i + 2 ] = '0' ; else s [ i + 2 ] = '1' ; } } return s [ n - 1 ] - '0' ; } int main ( ) { string s = "1C1B1B0A0" ; cout << evaluateBoolExpr ( s ) ; return 0 ; }