diff options
| author | 2026-11-07 05:06:01 +0000 | |
|---|---|---|
| committer | 2026-11-07 05:38:49 +0000 | |
| commit | 04f8b646c1c8a2435931bea9d1acffdde138f81e (patch) | |
| tree | 892354a7b485c3ac53e51a7d3e3388254ac877b7 /src/knowledge-base.c | |
| download | coursework-04f8b646c1c8a2435931bea9d1acffdde138f81e.tar.gz | |
Diffstat (limited to 'src/knowledge-base.c')
| -rw-r--r-- | src/knowledge-base.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/knowledge-base.c b/src/knowledge-base.c new file mode 100644 index 0000000..58a9d07 --- /dev/null +++ b/src/knowledge-base.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | #include "knowledge-base.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | #define QUESTION(symbol, label, yes, no) \ | ||
| 5 | [QUESTION_##symbol] = label, | ||
| 6 | char *question_labels[QUESTION_COUNT] = { | ||
| 7 | QUESTIONS | ||
| 8 | }; | ||
| 9 | #undef QUESTION | ||
| 10 | |||
| 11 | #define QUESTION(symbol, label, yes, no) \ | ||
| 12 | [QUESTION_##symbol] = { yes, no }, | ||
| 13 | struct question_branch_labels question_branch_labels[QUESTION_COUNT] = { | ||
| 14 | QUESTIONS | ||
| 15 | }; | ||
| 16 | #undef QUESTION | ||
| 17 | |||
| 18 | #define DECISION(symbol, label, ...) [DECISION_##symbol] = label, | ||
| 19 | char *decision_labels[DECISION_COUNT] = { | ||
| 20 | DECISIONS | ||
| 21 | }; | ||
| 22 | #undef DECISION | ||
| 23 | |||
| 24 | #define DECISION(symbol, label, ...) [DECISION_##symbol] = { __VA_ARGS__ }, | ||
| 25 | struct decision_predicates decision_predicates[DECISION_COUNT] = { | ||
| 26 | DECISIONS | ||
| 27 | }; | ||
| 28 | #undef DECISION | ||
| 29 | |||
| 30 | #define QUESTION(symbol, label, yes, no) \ | ||
| 31 | if (QUESTION_##symbol == question) \ | ||
| 32 | return decision_predicates[decision].symbol; | ||
| 33 | enum answer | ||
| 34 | get_decision_predicate (enum decision decision, enum question question) | ||
| 35 | { | ||
| 36 | QUESTIONS | ||
| 37 | |||
| 38 | return ANSWER_MALFORMED; | ||
| 39 | } | ||
| 40 | #undef QUESTION | ||
| 41 | |||
| 42 | void | ||
| 43 | print_knowledge_base (void) | ||
| 44 | { | ||
| 45 | puts ("QUESTIONS:"); | ||
| 46 | for (int i = 0; i < QUESTION_COUNT; ++i) | ||
| 47 | printf ("%2X", i + 1); | ||
| 48 | putchar ('\n'); | ||
| 49 | for (int i = 0; i < QUESTION_COUNT; ++i) { | ||
| 50 | printf ("%0*b: %s\n", 2*QUESTION_COUNT, | ||
| 51 | (1 << ANSWER_COUNT) - 1 << (QUESTION_COUNT - 1 - i)*ANSWER_COUNT, question_labels[i]); | ||
| 52 | } putchar ('\n'); | ||
| 53 | |||
| 54 | puts ("DECISIONS:"); | ||
| 55 | for (int i = 0; i < QUESTION_COUNT; ++i) | ||
| 56 | printf ("%2X", i + 1); | ||
| 57 | putchar ('\n'); | ||
| 58 | for (int i = 0; i < DECISION_COUNT; ++i) { | ||
| 59 | for (int j = 0; j < QUESTION_COUNT; ++j) { | ||
| 60 | printf ("%0*b", ANSWER_COUNT, get_decision_predicate (i, j)); | ||
| 61 | } | ||
| 62 | printf (": %s\n", decision_labels[i]); | ||
| 63 | } | ||
| 64 | } | ||
