123
This commit is contained in:
parent
72dadabd6e
commit
c65260b1fe
@ -11,7 +11,6 @@ typedef struct Node {
|
||||
struct Node *no;
|
||||
} Node;
|
||||
|
||||
// portable strdup replacement
|
||||
static char *my_strdup(const char *s) {
|
||||
if (!s) return NULL;
|
||||
size_t l = strlen(s) + 1;
|
||||
@ -21,7 +20,6 @@ static char *my_strdup(const char *s) {
|
||||
return p;
|
||||
}
|
||||
|
||||
// getline prototype (POSIX); declaring to avoid implicit declaration warnings
|
||||
ssize_t getline(char **__lineptr, size_t *__n, FILE *__stream);
|
||||
|
||||
static char *trim_newline(char *s) {
|
||||
@ -43,7 +41,6 @@ Node *parse_node(char **lines, int n, int *idx, int *err) {
|
||||
char *line = lines[*idx];
|
||||
(*idx)++;
|
||||
if (line[0] == '*') {
|
||||
// answer node
|
||||
char *txt = line + 1;
|
||||
txt = ltrim(txt);
|
||||
Node *node = malloc(sizeof(Node));
|
||||
@ -53,15 +50,14 @@ Node *parse_node(char **lines, int n, int *idx, int *err) {
|
||||
node->yes = node->no = NULL;
|
||||
return node;
|
||||
} else {
|
||||
// question node
|
||||
Node *node = malloc(sizeof(Node));
|
||||
Node *node = malloc(sizeof(Node));
|
||||
if (!node) { *err = 1; return NULL; }
|
||||
node->is_answer = 0;
|
||||
node->text = my_strdup(line);
|
||||
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 subtree? skip deep frees for brevity
|
||||
if (*err) {
|
||||
free(node->text); free(node); return NULL;
|
||||
}
|
||||
return node;
|
||||
@ -83,7 +79,6 @@ void free_tree(Node *root) {
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Read lines until an empty line (or EOF)
|
||||
char *line = NULL;
|
||||
size_t cap = 0;
|
||||
ssize_t len;
|
||||
@ -92,10 +87,8 @@ int main(void) {
|
||||
|
||||
while ((len = getline(&line, &cap, stdin)) != -1) {
|
||||
trim_newline(line);
|
||||
// stop at blank line
|
||||
if (line[0] == '\0') break;
|
||||
// store a copy
|
||||
char *copy = my_strdup(line);
|
||||
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; }
|
||||
@ -120,26 +113,22 @@ int main(void) {
|
||||
}
|
||||
|
||||
int leaves = count_answers(root);
|
||||
// print number of goods (answers)
|
||||
printf("%d\n", leaves);
|
||||
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");
|
||||
|
||||
// interactive session: use 'a' for áno (yes) and 'n' for nie (no)
|
||||
Node *cur = root;
|
||||
char buf[128];
|
||||
while (cur) {
|
||||
if (cur->is_answer) {
|
||||
// print answer and exit
|
||||
printf("Odpoveď: %s\n", cur->text);
|
||||
printf("* %s\n", cur->text);
|
||||
break;
|
||||
}
|
||||
// question
|
||||
printf("%s\n", cur->text);
|
||||
if (!fgets(buf, sizeof(buf), stdin)) {
|
||||
// EOF or error
|
||||
printf("Nesprávny vstup\n");
|
||||
break;
|
||||
}
|
||||
// skip whitespace and take first non-space char
|
||||
char *p = buf;
|
||||
while (*p && isspace((unsigned char)*p)) p++;
|
||||
char c = *p;
|
||||
@ -152,6 +141,7 @@ int main(void) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("Koniec\n");
|
||||
|
||||
free_tree(root);
|
||||
for (int i = 0; i < lines_count; ++i) free(lines[i]);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user