129 lines
3.4 KiB
C
129 lines
3.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
typedef struct Node {
|
|
int is_answer;
|
|
char *text;
|
|
struct Node *yes;
|
|
struct Node *no;
|
|
} Node;
|
|
|
|
static char *my_strdup(const char *s) {
|
|
if (!s) return NULL;
|
|
size_t l = strlen(s) + 1;
|
|
char *p = malloc(l);
|
|
if (!p) return NULL;
|
|
memcpy(p, s, l);
|
|
return p;
|
|
}
|
|
|
|
ssize_t getline(char **__lineptr, size_t *__n, FILE *__stream);
|
|
|
|
static char *trim_newline(char *s) {
|
|
if (!s) return s;
|
|
size_t len = strlen(s);
|
|
while (len > 0 && (s[len-1] == '\n' || s[len-1] == '\r')) {
|
|
s[--len] = '\0';
|
|
}
|
|
return s;
|
|
}
|
|
|
|
static char *ltrim(char *s) {
|
|
while (*s && isspace((unsigned char)*s)) s++;
|
|
return s;
|
|
}
|
|
|
|
Node *parse_node(char **lines, int n, int *idx, int *err) {
|
|
if (*idx >= n) { *err = 1; return NULL; }
|
|
char *line = lines[*idx];
|
|
(*idx)++;
|
|
if (line[0] == '*') {
|
|
char *txt = line + 1;
|
|
txt = ltrim(txt);
|
|
Node *node = malloc(sizeof(Node));
|
|
if (!node) { *err = 1; return NULL; }
|
|
node->is_answer = 1;
|
|
node->text = my_strdup(txt);
|
|
node->yes = node->no = NULL;
|
|
return node;
|
|
} else {
|
|
Node *node = malloc(sizeof(Node));
|
|
if (!node) { *err = 1; return NULL; }
|
|
node->is_answer = 0;
|
|
node->text = my_strdup(line);
|
|
node->yes = parse_node(lines, n, idx, err);
|
|
if (*err) { free(node->text); free(node); return NULL; }
|
|
node->no = parse_node(lines, n, idx, err);
|
|
if (*err) {
|
|
free(node->text); free(node); return NULL;
|
|
}
|
|
return node;
|
|
}
|
|
}
|
|
|
|
int count_answers(Node *root) {
|
|
if (!root) return 0;
|
|
if (root->is_answer) return 1;
|
|
return count_answers(root->yes) + count_answers(root->no);
|
|
}
|
|
|
|
void free_tree(Node *root) {
|
|
if (!root) return;
|
|
free_tree(root->yes);
|
|
free_tree(root->no);
|
|
free(root->text);
|
|
free(root);
|
|
}
|
|
|
|
int main(void) {
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
ssize_t len;
|
|
char **lines = NULL;
|
|
int lines_count = 0;
|
|
|
|
while ((len = getline(&line, &cap, stdin)) != -1) {
|
|
trim_newline(line);
|
|
if (line[0] == '\0') break;
|
|
char *copy = my_strdup(line);
|
|
if (!copy) { fprintf(stderr, "Chyba pri alokácii pamäte\n"); return 1; }
|
|
lines = realloc(lines, sizeof(char*) * (lines_count + 1));
|
|
if (!lines) { fprintf(stderr, "Chyba pri alokácii pamäte\n"); return 1; }
|
|
lines[lines_count++] = copy;
|
|
}
|
|
free(line);
|
|
|
|
if (lines_count == 0) {
|
|
printf("Chyba pri načítaní bázy pravidiel\n");
|
|
return 0;
|
|
}
|
|
|
|
int idx = 0;
|
|
int err = 0;
|
|
Node *root = parse_node(lines, lines_count, &idx, &err);
|
|
if (err || root == NULL || idx != lines_count) {
|
|
printf("Chyba pri načítaní bázy pravidiel\n");
|
|
for (int i = 0; i < lines_count; ++i) free(lines[i]);
|
|
free(lines);
|
|
if (root) free_tree(root);
|
|
return 0;
|
|
}
|
|
|
|
int leaves = count_answers(root);
|
|
printf("Expert z bufetu to vie.\n");
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", leaves);
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
if (root && !root->is_answer) {
|
|
printf("%s\n", root->text);
|
|
}
|
|
printf("Koniec vstupu\n");
|
|
|
|
free_tree(root);
|
|
for (int i = 0; i < lines_count; ++i) free(lines[i]);
|
|
free(lines);
|
|
return 0;
|
|
} |