#include "knowledge-base.h" #include #define QUESTION(symbol, label, yes, no) \ [QUESTION_##symbol] = label, char *question_labels[QUESTION_COUNT] = { QUESTIONS }; #undef QUESTION #define QUESTION(symbol, label, yes, no) \ [QUESTION_##symbol] = { yes, no }, struct question_branch_labels question_branch_labels[QUESTION_COUNT] = { QUESTIONS }; #undef QUESTION #define DECISION(symbol, label, ...) [DECISION_##symbol] = label, char *decision_labels[DECISION_COUNT] = { DECISIONS }; #undef DECISION #define DECISION(symbol, label, ...) [DECISION_##symbol] = { __VA_ARGS__ }, struct decision_predicates decision_predicates[DECISION_COUNT] = { DECISIONS }; #undef DECISION #define QUESTION(symbol, label, yes, no) \ if (QUESTION_##symbol == question) \ return decision_predicates[decision].symbol; enum answer get_decision_predicate (enum decision decision, enum question question) { QUESTIONS return ANSWER_MALFORMED; } #undef QUESTION void print_knowledge_base (void) { puts ("QUESTIONS:"); for (int i = 0; i < QUESTION_COUNT; ++i) printf ("%2X", i + 1); putchar ('\n'); for (int i = 0; i < QUESTION_COUNT; ++i) { printf ("%0*b: %s\n", 2*QUESTION_COUNT, (1 << ANSWER_COUNT) - 1 << (QUESTION_COUNT - 1 - i)*ANSWER_COUNT, question_labels[i]); } putchar ('\n'); puts ("DECISIONS:"); for (int i = 0; i < QUESTION_COUNT; ++i) printf ("%2X", i + 1); putchar ('\n'); for (int i = 0; i < DECISION_COUNT; ++i) { for (int j = 0; j < QUESTION_COUNT; ++j) { printf ("%0*b", ANSWER_COUNT, get_decision_predicate (i, j)); } printf (": %s\n", decision_labels[i]); } }