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
using System ; using System . Collections . Generic ; public class GFG { static char logicalExpressionEvaluation ( String str ) { Stack < char > arr = new Stack < char > ( ) ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' [ ' ) { List < char > s = new List < char > ( ) ; while ( arr . Peek ( ) != ' ] ' ) { s . Add ( arr . Peek ( ) ) ; arr . Pop ( ) ; } arr . Pop ( ) ; if ( s . Count == 3 ) { arr . Push ( s [ 2 ] == '1' ? '0' : '1' ) ; } else if ( s . Count == 5 ) { int a = s [ 0 ] - 48 , b = s [ 4 ] - 48 , c ; if ( s [ 2 ] == ' & ' ) { c = a & b ; } else { c = a | b ; } arr . Push ( ( char ) ( c + 48 ) ) ; } } else { arr . Push ( str [ i ] ) ; } } return arr . Peek ( ) ; } public static void Main ( String [ ] args ) { String str = " [ [0 , & ,1 ] , | , [ ! ,1 ] ] " ; Console . WriteLine ( logicalExpressionEvaluation ( str ) ) ; } }