Solve the Logical Expression given by string | C ++ program to solve the logical expression . ; Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char logicalExpressionEvaluation ( string str ) { stack < char > arr ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' [ ' ) { vector < char > s ; while ( arr . top ( ) != ' ] ' ) { s . push_back ( arr . top ( ) ) ; arr . pop ( ) ; } arr . pop ( ) ; if ( s . size ( ) == 3 ) { s [ 2 ] == '1' ? arr . push ( '0' ) : arr . push ( '1' ) ; } else if ( s . size ( ) == 5 ) { int a = s [ 0 ] - 48 , b = s [ 4 ] - 48 , c ; s [ 2 ] == ' & ' ? c = a && b : c = a || b ; arr . push ( ( char ) c + 48 ) ; } } else { arr . push ( str [ i ] ) ; } } return arr . top ( ) ; } int main ( ) { string str = " [ [0 , & ,1 ] , | , [ ! ,1 ] ] " ; cout << logicalExpressionEvaluation ( str ) << endl ; return 0 ; }