I have been struggling for days to solve this problem:
I have to write a recursive code that calculates the truth value of a statement (I can also use loops in the function);
- Connectors: "&" (and) and "|" (or)
- values: 1 and 0
- The function must return either "1" or "0"
For instance -
For the statement 1 the function must return 1
For the statement 0 the function must return 0
For the statement (1&1) the function must return 1
For the statement (0|1) the function must return 1
So basically 0 & 0\1 = 0 , 1 | 1\0 = 1
And for more complex statements
(1&(1|0)) ; (1|0) is 1, so it is (1&1) which is 1
((1|0)&(0&1)) ; (1|0) = 1, (0&1) = 0 --> (1&0)=0
(The statement is defined as a string)
int st_value (char statement[], int length, int i) /* this can be changed*/
{
if (length == 1)
return statement[0];
if (statement[i]=='(' && statement[length-1]==')')
return st_val(statement, length--, i++);
else if (statement[i]=='(')
return st_val(statement, length, i++);
if (statement[i]=='0' || statement[i]=='1')
{
if (a[i+1] == '|')
return st_val(statement, length, i+2);
.....
}
if (statement[i]=='&')
.....
}
If I have to follow this, the code would be way too long and would have many holes, like when some part of the code returns 0...