ls
This commit is contained in:
parent
c7b2ed7060
commit
48b502fcf4
0
du5/README.md
Normal file
0
du5/README.md
Normal file
123
du5/program.c
Normal file
123
du5/program.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define LINE_SIZE 256
|
||||
|
||||
typedef struct Node {
|
||||
char *text;
|
||||
int is_answer;
|
||||
struct Node *yes;
|
||||
struct Node *no;
|
||||
} Node;
|
||||
|
||||
Node* make_node(const char *text, int is_answer) {
|
||||
Node *node = (Node*)malloc(sizeof(Node));
|
||||
node->text = (char*)malloc(strlen(text) + 1);
|
||||
strcpy(node->text, text);
|
||||
node->is_answer = is_answer;
|
||||
node->yes = NULL;
|
||||
node->no = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
int how_many_answers(Node *node) {
|
||||
if (node == NULL) return 0;
|
||||
int count = 0;
|
||||
if (node->is_answer) count = 1;
|
||||
count += how_many_answers(node->yes);
|
||||
count += how_many_answers(node->no);
|
||||
return count;
|
||||
}
|
||||
|
||||
Node* build_tree() {
|
||||
char line[LINE_SIZE];
|
||||
|
||||
if (fgets(line, sizeof(line), stdin) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
line[strcspn(line, "\n\r")] = '\0';
|
||||
|
||||
if (strlen(line) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int is_answer = 0;
|
||||
char *text = line;
|
||||
|
||||
if (line[0] == '*') {
|
||||
is_answer = 1;
|
||||
text = line + 1;
|
||||
}
|
||||
|
||||
Node *node = make_node(text, is_answer);
|
||||
|
||||
if (!is_answer) {
|
||||
node->yes = build_tree();
|
||||
node->no = build_tree();
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void chat_with_user(Node *root, int 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");
|
||||
|
||||
Node *current = root;
|
||||
char input[LINE_SIZE];
|
||||
|
||||
while (current != NULL && !current->is_answer) {
|
||||
printf("%s\n", current->text);
|
||||
|
||||
if (fgets(input, sizeof(input), stdin) == NULL) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
|
||||
input[strcspn(input, "\n\r")] = '\0';
|
||||
|
||||
if (strcmp(input, "a") == 0) {
|
||||
current = current->yes;
|
||||
} else if (strcmp(input, "n") == 0) {
|
||||
current = current->no;
|
||||
} else {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (current != NULL && current->is_answer) {
|
||||
printf("* %s\n", current->text);
|
||||
}
|
||||
|
||||
printf("Koniec\n");
|
||||
}
|
||||
|
||||
void cleanup_tree(Node *node) {
|
||||
if (node == NULL) return;
|
||||
cleanup_tree(node->yes);
|
||||
cleanup_tree(node->no);
|
||||
free(node->text);
|
||||
free(node);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Node *root = build_tree();
|
||||
|
||||
if (root == NULL) {
|
||||
printf("Nepodarilo sa nacitat bazu pravidiel\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int answer_count = how_many_answers(root);
|
||||
|
||||
chat_with_user(root, answer_count);
|
||||
|
||||
cleanup_tree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user