Aktualizovat du5/program.c

This commit is contained in:
Daniel Dembický 2026-04-17 06:52:33 +00:00
parent 4c4436caaa
commit 6e4ec334bc

View File

@ -1,71 +1,30 @@
#include <stdio.h> #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) int main(void)
{ {
char buf[MAX_LEN]; static char text[1000][256];
while (fgets(buf, MAX_LEN, stdin)) { static int is_answer[1000];
trim_newline(buf); static int yes_child[1000];
if (buf[0] == '\0') break; static int no_child[1000];
if (line_count >= MAX_NODES) { static char lines[1000][256];
printf("Chyba: Baza pravidiel je prilis velka.\n"); static int stack_par[2000];
return 1; static int stack_wh[2000];
}
strncpy(lines[line_count++], buf, MAX_LEN - 1); 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) { if (line_count == 0) {
@ -73,16 +32,51 @@ int main(void)
return 1; return 1;
} }
int root = parse_node(); stack_par[0] = -1;
stack_wh[0] = 0;
stack_top = 1;
if (root == -1 || line_pos != line_count) { 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"); printf("Chyba: Nepodarilo sa spravne nacitat bazu pravidiel.\n");
return 1; return 1;
} }
int answer_count = 0; int answer_count = 0;
for (int i = 0; i < node_count; i++) for (int i = 0; i < node_count; i++)
if (nodes[i].is_answer) answer_count++; if (is_answer[i]) answer_count++;
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
@ -90,26 +84,25 @@ int main(void)
int current = root; int current = root;
while (current != -1) { while (current != -1) {
if (is_answer[current]) {
if (nodes[current].is_answer) { printf("* %s\n", text[current]);
printf("* %s\n", nodes[current].text);
printf("Koniec\n"); printf("Koniec\n");
return 0; return 0;
} }
printf("%s\n", nodes[current].text); printf("%s\n", text[current]);
if (!fgets(buf, MAX_LEN, stdin)) { if (!fgets(buf, 256, stdin)) {
printf("Chyba: Neplatny vstup.\n"); printf("Chyba: Neplatny vstup.\n");
return 1; return 1;
} }
trim_newline(buf); int len = 0;
while (buf[len]) len++;
while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0';
if (strcmp(buf, "a") == 0) { if (buf[0] == 'a' && buf[1] == '\0') current = yes_child[current];
current = nodes[current].yes_child; else if (buf[0] == 'n' && buf[1] == '\0') current = no_child[current];
} else if (strcmp(buf, "n") == 0) { else {
current = nodes[current].no_child;
} else {
printf("Chyba: Neplatny vstup '%s'.\n", buf); printf("Chyba: Neplatny vstup '%s'.\n", buf);
return 1; return 1;
} }