Přidat du5/program.c
This commit is contained in:
parent
acb2759384
commit
4c4436caaa
120
du5/program.c
Normal file
120
du5/program.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_NODES 1000
|
||||
#define MAX_LEN 256
|
||||
|
||||
typedef struct {
|
||||
char text[MAX_LEN];
|
||||
int is_answer;
|
||||
int yes_child;
|
||||
int no_child;
|
||||
} Node;
|
||||
|
||||
static Node nodes[MAX_NODES];
|
||||
static int node_count = 0;
|
||||
|
||||
static char lines[MAX_NODES][MAX_LEN];
|
||||
static int line_count = 0;
|
||||
static int line_pos = 0;
|
||||
|
||||
static int parse_node(void)
|
||||
{
|
||||
if (line_pos >= line_count) return -1;
|
||||
if (node_count >= MAX_NODES) return -1;
|
||||
|
||||
char *line = lines[line_pos++];
|
||||
int idx = node_count++;
|
||||
|
||||
if (line[0] == '*') {
|
||||
nodes[idx].is_answer = 1;
|
||||
nodes[idx].yes_child = -1;
|
||||
nodes[idx].no_child = -1;
|
||||
int start = 1;
|
||||
if (line[start] == ' ') start++;
|
||||
strncpy(nodes[idx].text, line + start, MAX_LEN - 1);
|
||||
nodes[idx].text[MAX_LEN - 1] = '\0';
|
||||
} else {
|
||||
nodes[idx].is_answer = 0;
|
||||
strncpy(nodes[idx].text, line, MAX_LEN - 1);
|
||||
nodes[idx].text[MAX_LEN - 1] = '\0';
|
||||
|
||||
nodes[idx].yes_child = parse_node();
|
||||
nodes[idx].no_child = parse_node();
|
||||
|
||||
if (nodes[idx].yes_child == -1 || nodes[idx].no_child == -1)
|
||||
return -1;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
static void trim_newline(char *s)
|
||||
{
|
||||
int len = (int)strlen(s);
|
||||
while (len > 0 && (s[len - 1] == '\n' || s[len - 1] == '\r'))
|
||||
s[--len] = '\0';
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char buf[MAX_LEN];
|
||||
while (fgets(buf, MAX_LEN, stdin)) {
|
||||
trim_newline(buf);
|
||||
if (buf[0] == '\0') break;
|
||||
if (line_count >= MAX_NODES) {
|
||||
printf("Chyba: Baza pravidiel je prilis velka.\n");
|
||||
return 1;
|
||||
}
|
||||
strncpy(lines[line_count++], buf, MAX_LEN - 1);
|
||||
}
|
||||
|
||||
if (line_count == 0) {
|
||||
printf("Chyba: Baza pravidiel je prazdna.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int root = parse_node();
|
||||
|
||||
if (root == -1 || 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 (nodes[i].is_answer) 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 (nodes[current].is_answer) {
|
||||
printf("* %s\n", nodes[current].text);
|
||||
printf("Koniec\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("%s\n", nodes[current].text);
|
||||
|
||||
if (!fgets(buf, MAX_LEN, stdin)) {
|
||||
printf("Chyba: Neplatny vstup.\n");
|
||||
return 1;
|
||||
}
|
||||
trim_newline(buf);
|
||||
|
||||
if (strcmp(buf, "a") == 0) {
|
||||
current = nodes[current].yes_child;
|
||||
} else if (strcmp(buf, "n") == 0) {
|
||||
current = nodes[current].no_child;
|
||||
} else {
|
||||
printf("Chyba: Neplatny vstup '%s'.\n", buf);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Chyba: Nenasla sa odpoved.\n");
|
||||
return 1;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user