Re: help with a logic algorithm

Liste des GroupesRevenir à cl c  
Sujet : Re: help with a logic algorithm
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.c
Date : 03. Apr 2024, 14:17:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uujhcv$3sr9p$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
Here is the algorithm I did. Kind of brute-force algorithm.
It requires two phases.
At phase 1 we collect the variables used by the expression.
This phase is not implemented here, but the result of this phase is represented by.
struct var vars[] = { {"a"}, {"b"} , {"c"} };
The expression can be configured at
bool expression(struct var vars[], int l);
The expression used in this sample is
a && b || c
So what we have is
if (a && b || c)
{
    //what are the possible values of a, b and c?
}
else
{
    //what are the possible values of a, b and c?
}
The phase two generates all possible combinations evaluating the expression.
if the result the expression is true, we store the possible states of true_branch, otherwise se store the possible states of else branch.
---code --
#include <stdio.h>
#include <stdbool.h>
enum E
{
     TRUE_FLAG = 1 << 0,
     FALSE_FLAG = 1 << 1
};
struct var
{
     const char * name; //name of the variable
     bool value; //value
     enum E true_branch;   //possible values at true branch
     enum E false_branch;  //possible values at else branch
};
//List of variables used by expression
struct var vars[] = { {"a"}, {"b"} , {"c"} };
bool expression(struct var vars[], int l)
{
     l;
     //a && b
     // return vars[0].value && vars[1].value;
     //a && b || c
     return vars[0].value && vars[1].value || vars[2].value;
}
void visit(int k, struct var  vars[], int l)
{
     if (k == l)
     {
         for (int i = 0; i < l; i++)
         {
             if (i > 0) printf(",");
             printf("%s:%s", vars[i].name, (vars[i].value ? "T" : "F"));
         }
         bool r = expression(vars, l);
         printf(" = %s\n", r ? "T" : "F");
         for (int i = 0; i < l; i++)
         {
             if (r)
             {
                 vars[i].true_branch |= (vars[i].value ? TRUE_FLAG : FALSE_FLAG);
             }
             else
             {
                 vars[i].false_branch |= (vars[i].value ? TRUE_FLAG : FALSE_FLAG);
             }
         }
         return;
     }
     vars[k].value = true;
     visit(k + 1, vars, l);
     vars[k].value = false;
     visit(k + 1, vars, l);
}
int main()
{
     int l = (sizeof(vars) / sizeof(vars[0]));
     visit(0, vars, l);
     printf("\nAt true branch..\n");
     for (int i = 0; i < l; i++)
     {
         printf("%s can be : %s %s\n",
             vars[i].name,
             (vars[i].true_branch & TRUE_FLAG) ? " T" : "",
             (vars[i].true_branch & FALSE_FLAG) ? " F" : "");
     }
     printf("\nAt else branch..\n");
     for (int i = 0; i < l; i++)
     {
         printf("%s can be : %s %s\n",
             vars[i].name,
             (vars[i].false_branch & TRUE_FLAG) ? " T" : "",
             (vars[i].false_branch & FALSE_FLAG) ? " F" : "");
     }
}
https://godbolt.org/z/eEhY7rPsz

Date Sujet#  Auteur
2 Apr 24 * help with a logic algorithm13Thiago Adams
3 Apr 24 +* Re: help with a logic algorithm3Thiago Adams
3 Apr 24 i+- Re: help with a logic algorithm1jak
3 Apr 24 i`- Re: help with a logic algorithm1Thiago Adams
3 Apr 24 +- Re: help with a logic algorithm1Janis Papanagnou
3 Apr 24 +* Re: help with a logic algorithm5Anton Shepelev
3 Apr 24 i+- Re: help with a logic algorithm1Thiago Adams
3 Apr 24 i`* Re: help with a logic algorithm3Thiago Adams
4 Apr 24 i `* Re: help with a logic algorithm2Paul
5 Apr 24 i  `- Re: help with a logic algorithm1Anton Shepelev
4 Apr 24 `* Re: help with a logic algorithm3jak
4 Apr 24  `* Re: help with a logic algorithm2Thiago Adams
7 Apr 24   `- Re: help with a logic algorithm1Anton Shepelev

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal