Sujet : Re: help with a logic algorithm
De : nospam (at) *nospam* please.ty (jak)
Groupes : comp.lang.cDate : 04. Apr 2024, 17:06:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uumj6i$ocmh$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.18.2
Thiago Adams ha scritto:
I need an algorithm that finds the possible states of variables used in "ifs"
for instance, I need to find out possible states:
if (a && b){
// 'a' is true
// 'b' is true
}
else
{
// 'a' maybe be true or false
// 'b' maybe be true or false
}
More complex:
if (a && b || c){
// 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
}
else
{
// 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
}
I think this algorithm may already exists.. but not finding it.
Maybe something related with predicates.
I'm not sure I understand exactly what you want to obtain,
but if I understand I can propose, to you, a simple idea:
#include <stdio.h>
int main()
{
#define SFMT "a:%-5.5s b: %-5.5s c: %-5.5s\n"
#define BIT(v, t) (v & t)
#define TST(v) ((v) ? "true" : "false")
unsigned char a = 1 << 0,
b = 1 << 1,
c = 1 << 2;
for(unsigned char i = 0; i <= 7; i++)
{
if(BIT(i, a) && BIT(i, b) || BIT(i, c))
{
printf("Then: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
else
{
printf("Else: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
}
return 0;
}
output:
Else: a:false b: false c: false
Else: a:true b: false c: false
Else: a:false b: true c: false
Then: a:true b: true c: false
Then: a:false b: false c: true
Then: a:true b: false c: true
Then: a:false b: true c: true
Then: a:true b: true c: true