This commit is contained in:
Weber 2024-11-14 17:25:22 +00:00
parent 9290bc1b1e
commit a22fdbe135

View File

@ -2,85 +2,151 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
typedef struct Uzol { typedef struct Node {
char *text; char *text;
struct Uzol *ano; int is_answer;
struct Uzol *nie; struct Node *yes;
} Uzol; struct Node *no;
} Node;
int pocet_tovarov = 0; char *read_line(void) {
size_t size = 100;
char *buffer = malloc(size);
if (!buffer) return NULL;
Uzol* vytvor_uzol(const char *text) { int c;
Uzol *uzol = (Uzol *)malloc(sizeof(Uzol)); size_t len = 0;
uzol->text = strdup(text); while ((c = getchar()) != EOF && c != '\n') {
uzol->ano = NULL; if (len == size - 1) {
uzol->nie = NULL; size *= 2;
return uzol; char *new_buffer = realloc(buffer, size);
} if (!new_buffer) {
free(buffer);
Uzol* nacitaj_strom() { return NULL;
char riadok[256]; }
if (fgets(riadok, sizeof(riadok), stdin) == NULL || riadok[0] == '\n') { buffer = new_buffer;
}
buffer[len++] = c;
}
if (len == 0 && c == EOF) {
free(buffer);
return NULL; return NULL;
} }
riadok[strcspn(riadok, "\n")] = '\0'; buffer[len] = '\0';
if (riadok[0] == '*') { return buffer;
pocet_tovarov++;
return vytvor_uzol(riadok + 1);
}
Uzol *uzol = vytvor_uzol(riadok);
uzol->ano = nacitaj_strom();
uzol->nie = nacitaj_strom();
return uzol;
} }
void vypis_otazku(Uzol *uzol) { int read_knowledge_base(char ***lines_ptr, int *num_lines) {
if (uzol == NULL) { int capacity = 100;
return; char **lines = malloc(capacity * sizeof(char *));
} if (!lines) return -1;
printf("%s\n", uzol->text); int count = 0;
if (uzol->ano == NULL && uzol->nie == NULL) {
return;
}
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
char odpoved;
while (1) { while (1) {
odpoved = getchar(); // čítame odpoveď char *line = read_line();
while (getchar() != '\n'); // vyčistenie bufferu if (!line) break;
if (odpoved == 'a' || odpoved == 'n') {
break; // Odpoveď je platná, ukončíme cyklus
} else {
printf("Nerozumiem\n");
}
}
if (odpoved == 'a') { if (count == capacity) {
if (uzol->ano != NULL) { capacity *= 2;
vypis_otazku(uzol->ano); char **new_lines = realloc(lines, capacity * sizeof(char *));
} if (!new_lines) {
} else if (odpoved == 'n') { for (int i = 0; i < count; i++) free(lines[i]);
if (uzol->nie != NULL) { free(lines);
vypis_otazku(uzol->nie); return -1;
}
lines = new_lines;
} }
lines[count++] = line;
} }
} *lines_ptr = lines;
*num_lines = count;
void spusti_system(Uzol *strom) {
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", pocet_tovarov);
vypis_otazku(strom);
printf("Koniec\n");
}
int main() {
Uzol *strom = nacitaj_strom();
if (strom == NULL) {
printf("Chyba: Nepodarilo sa otvorit subor s pravidlami.\n");
return 1;
}
spusti_system(strom);
return 0; return 0;
} }
Node *build_tree(char **lines, int *index, int num_lines, int *count, int *error) {
if (*index >= num_lines) {
*error = 1;
return NULL;
}
Node *node = malloc(sizeof(Node));
if (!node) {
*error = 1;
return NULL;
}
node->text = lines[(*index)++];
node->yes = node->no = NULL;
node->is_answer = (node->text[0] == '*');
if (node->is_answer) {
(*count)++;
} else {
node->yes = build_tree(lines, index, num_lines, count, error);
node->no = build_tree(lines, index, num_lines, count, error);
}
return node;
}
void run_knowledge_system(Node *root, int count) {
printf("Expert z bufetu to vie.\n");
if (!root) {
printf("Chybna databaza\n");
return;
}
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
Node *node = root;
while (node) {
printf("%s\n", node->text);
if (node->is_answer) {
printf("Koniec\n");
return;
}
char input[10];
if (!fgets(input, sizeof(input), stdin)) {
printf("Koniec vstupu\n");
return;
}
input[strcspn(input, "\n")] = 0;
if (strcmp(input, "a") == 0) {
node = node->yes;
} else if (strcmp(input, "n") == 0) {
node = node->no;
} else {
printf("Nerozumiem\n");
return;
}
}
}
void free_tree(Node *node) {
if (!node) return;
if (!node->is_answer) {
free_tree(node->yes);
free_tree(node->no);
}
free(node);
}
int main(void) {
char **lines;
int num_lines;
if (read_knowledge_base(&lines, &num_lines) != 0 || num_lines == 0) {
printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n");
return 0;
}
int index = 0, count = 0, error = 0;
Node *root = build_tree(lines, &index, num_lines, &count, &error);
if (error || index != num_lines || count == 0) {
printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n");
for (int i = 0; i < num_lines; i++) free(lines[i]);