#include int main(void) { static char text[1000][256]; static int is_answer[1000]; static int yes_child[1000]; static int no_child[1000]; static char lines[1000][256]; static int stack_par[2000]; static int stack_wh[2000]; int node_count = 0; int line_count = 0; int stack_top = 0; int root = -1; char buf[256]; while (fgets(buf, 256, stdin)) { int len = 0; while (buf[len]) len++; while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0'; if (len == 0) break; int i = 0; while (buf[i]) { lines[line_count][i] = buf[i]; i++; } lines[line_count][i] = '\0'; line_count++; } if (line_count == 0) { printf("Chyba: Baza pravidiel je prazdna.\n"); return 1; } stack_par[0] = -1; stack_wh[0] = 0; stack_top = 1; int line_pos = 0; while (stack_top > 0 && line_pos < line_count) { stack_top--; int par = stack_par[stack_top]; int wh = stack_wh[stack_top]; char *line = lines[line_pos++]; int idx = node_count++; if (par == -1) root = idx; else if (wh == 0) yes_child[par] = idx; else no_child[par] = idx; if (line[0] == '*') { is_answer[idx] = 1; yes_child[idx] = -1; no_child[idx] = -1; int start = (line[1] == ' ') ? 2 : 1; int i = 0; while (line[start + i]) { text[idx][i] = line[start + i]; i++; } text[idx][i] = '\0'; } else { is_answer[idx] = 0; yes_child[idx] = -1; no_child[idx] = -1; int i = 0; while (line[i]) { text[idx][i] = line[i]; i++; } text[idx][i] = '\0'; stack_par[stack_top] = idx; stack_wh[stack_top] = 1; stack_top++; stack_par[stack_top] = idx; stack_wh[stack_top] = 0; stack_top++; } } if (root == -1 || stack_top != 0 || line_pos != line_count) { printf("Chyba: Nepodarilo sa spravne nacitat bazu pravidiel.\n"); return 1; } int answer_count = 0; for (int i = 0; i < node_count; i++) if (is_answer[i]) answer_count++; printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); int current = root; while (current != -1) { if (is_answer[current]) { printf("* %s\n", text[current]); printf("Koniec\n"); return 0; } printf("%s\n", text[current]); if (!fgets(buf, 256, stdin)) { printf("Chyba: Neplatny vstup.\n"); return 1; } int len = 0; while (buf[len]) len++; while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0'; if (buf[0] == 'a' && buf[1] == '\0') current = yes_child[current]; else if (buf[0] == 'n' && buf[1] == '\0') current = no_child[current]; else { printf("Chyba: Neplatny vstup '%s'.\n", buf); return 1; } } printf("Chyba: Nenasla sa odpoved.\n"); return 1; }