This commit is contained in:
Oleksandr Vyshniakov 2025-11-20 16:59:48 +01:00
parent b93407d906
commit b05532617b

View File

@ -10,25 +10,20 @@ typedef struct TreeNode {
struct TreeNode *no_branch; struct TreeNode *no_branch;
} TreeNode; } TreeNode;
static TreeNode* read_tree_node(int *used_lines) { static TreeNode* read_tree_node() {
char buffer[MAXLINE]; char buffer[MAXLINE];
memset(buffer, 0, sizeof(buffer));
char *r = fgets(buffer, sizeof(buffer), stdin); if (!fgets(buffer, sizeof(buffer), stdin)) {
if (r == NULL) {
return NULL; return NULL;
} }
(*used_lines)++;
if (buffer[0] == '\n' || buffer[0] == '\r') { if (buffer[0] == '\n' || buffer[0] == '\r') {
return NULL; return NULL;
} }
TreeNode *node = calloc(1, sizeof(TreeNode)); TreeNode *node = calloc(1, sizeof(TreeNode));
if (!node) { if (!node)
exit(1); exit(1);
}
strncpy(node->text, buffer, MAXLINE - 1); strncpy(node->text, buffer, MAXLINE - 1);
@ -38,129 +33,81 @@ static TreeNode* read_tree_node(int *used_lines) {
return node; return node;
} }
node->yes_branch = read_tree_node(used_lines); node->yes_branch = read_tree_node();
node->no_branch = read_tree_node(used_lines); node->no_branch = read_tree_node();
return node; return node;
} }
static void free_tree(TreeNode *root) { static void free_tree(TreeNode *root) {
if (root != NULL) { if (!root) return;
free_tree(root->yes_branch); free_tree(root->yes_branch);
free_tree(root->no_branch); free_tree(root->no_branch);
free(root); free(root);
} }
}
static int count_leafs(TreeNode *root) { static int count_leafs(TreeNode *root) {
if (root == NULL) { if (!root) return 0;
return 0;
}
int left = count_leafs(root->yes_branch); if (!root->yes_branch && !root->no_branch)
int right = count_leafs(root->no_branch); return 1;
int is_leaf = (root->yes_branch == NULL && root->no_branch == NULL) ? 1 : 0; return count_leafs(root->yes_branch) + count_leafs(root->no_branch);
return left + right + is_leaf;
} }
static int validate_tree(TreeNode *root) { static int validate_tree(TreeNode *root) {
if (root == NULL) { if (!root) return 0;
return 0;
}
int is_answer = (root->text[0] == '*'); int is_answer = (root->text[0] == '*');
if (is_answer) { if (is_answer) {
if (root->yes_branch != NULL || root->no_branch != NULL) { if (root->yes_branch || root->no_branch)
return 0; return 0;
}
return 1; return 1;
} }
if (root->yes_branch == NULL || root->no_branch == NULL) { if (!root->yes_branch || !root->no_branch)
return 0; return 0;
}
return validate_tree(root->yes_branch) && validate_tree(root->no_branch); return validate_tree(root->yes_branch) && validate_tree(root->no_branch);
} }
static void skip_empty_line() {
char temp[MAXLINE];
long pos = ftell(stdin);
if (fgets(temp, sizeof(temp), stdin) == NULL) {
return;
}
if (!(temp[0] == '\n' || temp[0] == '\r')) {
fseek(stdin, pos, SEEK_SET);
}
}
static void run_dialog(TreeNode *root) { static void run_dialog(TreeNode *root) {
if (root == NULL) { if (!root) return;
return;
}
printf("%s", root->text); printf("%s", root->text);
if (root->yes_branch == NULL && root->no_branch == NULL) { if (!root->yes_branch && !root->no_branch) {
printf("Koniec\n"); printf("Koniec\n");
return; return;
} }
char c; char c;
int r = scanf(" %c", &c); if (scanf(" %c", &c) != 1) {
printf("Koniec\n");
if (r != 1) {
printf("Koniec vstupu\n");
return; return;
} }
if (c == 'a') { if (c == 'a') {
run_dialog(root->yes_branch); run_dialog(root->yes_branch);
return; } else if (c == 'n') {
}
if (c == 'n') {
run_dialog(root->no_branch); run_dialog(root->no_branch);
return; } else {
}
printf("Nerozumiem\n"); printf("Nerozumiem\n");
} }
}
int main() { int main() {
int used_lines = 0;
TreeNode *root = read_tree_node(&used_lines);
skip_empty_line(); TreeNode *root = read_tree_node(); // читаем базу до пустой строки
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
if (root == NULL || !validate_tree(root)) { if (!root || !validate_tree(root)) {
printf("Chybna databaza\n"); printf("Chybna databaza\n");
free_tree(root); free_tree(root);
return 0; return 0;
} }
int total_lines = used_lines;
long pos = ftell(stdin);
char extra[MAXLINE];
if (fgets(extra, sizeof(extra), stdin) != NULL) {
int newline = (extra[0] == '\n' || extra[0] == '\r');
if (!newline) {
printf("Chybna databaza\n");
free_tree(root);
return 0;
}
} else {
fseek(stdin, pos, SEEK_SET);
}
int total = count_leafs(root); int total = count_leafs(root);
printf("Pozna %d druhov ovocia a zeleniny.\n", total); printf("Pozna %d druhov ovocia a zeleniny.\n", total);
@ -169,6 +116,5 @@ int main() {
run_dialog(root); run_dialog(root);
free_tree(root); free_tree(root);
return 0; return 0;
} }