1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream>
#include <iomanip>
#include <functional>
#include <string>
void affichLigneTableVerite(std::string nomOperateur, bool a, bool b, std::function<bool(bool, bool)> op)
{
bool resultat = op(a, b);
std::cout << std::setw(5) << a << ' ' << nomOperateur << ' ' << std::setw(5) << b << " = " << resultat << std::endl;
}
void tableVerite(std::string nomOperateur, std::function<bool(bool, bool)> op)
{
std::cout << "Table de verite pour operateur " << nomOperateur << ":" << std::endl;
affichLigneTableVerite(nomOperateur, false, false, op);
affichLigneTableVerite(nomOperateur, false, true , op);
affichLigneTableVerite(nomOperateur, true , false, op);
affichLigneTableVerite(nomOperateur, true , true, op);
std::cout << std::endl;
}
bool et(bool a, bool b)
{
return a && b;
}
bool ouInclusif(bool a, bool b)
{
return a || b;
}
bool ouExclusif(bool a, bool b)
{
return (a && !b) || (!a && b);
}
int main(void)
{
std::cout << std::boolalpha;
tableVerite("AND", et);
tableVerite("OR", ouInclusif);
tableVerite("XOR", ouExclusif);
return 0;
} |
Partager