Přidat du6/program.c
This commit is contained in:
parent
dd939e2256
commit
1c72fb3b11
122
du6/program.c
Normal file
122
du6/program.c
Normal file
@ -0,0 +1,122 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#define SIZE 256
|
||||
|
||||
struct tree {
|
||||
char value[SIZE];
|
||||
int id;
|
||||
struct tree* left;
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
int read_line(char* buf) {
|
||||
if (!fgets(buf, SIZE, stdin))
|
||||
return 0;
|
||||
|
||||
int len = strlen(buf);
|
||||
while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
|
||||
buf[--len] = '\0';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct tree* read_tree(int* counter) {
|
||||
char buffer[SIZE];
|
||||
|
||||
if (!read_line(buffer))
|
||||
return NULL;
|
||||
|
||||
if (buffer[0] == '\0')
|
||||
return NULL;
|
||||
|
||||
struct tree* node = calloc(1, sizeof(struct tree));
|
||||
assert(node);
|
||||
strcpy(node->value, buffer);
|
||||
node->id = (*counter)++;
|
||||
|
||||
// відповідь (*...), то це лист → не читаємо дітей
|
||||
if (buffer[0] == '*') {
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
node->left = read_tree(counter);
|
||||
node->right = read_tree(counter);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* node) {
|
||||
if (!node) return; // якщо вузла немає → нічого робити
|
||||
|
||||
destroy_tree(node->left);
|
||||
destroy_tree(node->right);
|
||||
free(node);
|
||||
}
|
||||
|
||||
|
||||
int count_leaves(struct tree* node) {
|
||||
if (!node) return 0; // порожнє дерево
|
||||
|
||||
if (!node->left && !node->right) // якщо це лист
|
||||
return 1;
|
||||
|
||||
return count_leaves(node->left)+count_leaves(node->right);
|
||||
}
|
||||
|
||||
|
||||
void run_system(struct tree* node) {
|
||||
while (node) {
|
||||
printf("%s\n", node->value); // виводимо питання або відповідь
|
||||
|
||||
|
||||
if (!node->left && !node->right) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
char c;
|
||||
if (scanf(" %c", &c) != 1) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (c == 'a') // так → йдемо в ліву гілку
|
||||
node = node->left;
|
||||
else if (c == 'n') // ні → вправо
|
||||
node = node->right;
|
||||
else {
|
||||
printf("Nespravny vstup.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int counter=0; //нумерація вузлів
|
||||
struct tree* root =read_tree(&counter);
|
||||
char check[SIZE];
|
||||
|
||||
if (!fgets(check, SIZE, stdin) ||
|
||||
!(check[0]=='\n' || check[0]=='\r')) {
|
||||
printf("Chyba v baze pravidiel.\n");
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int leaves = count_leaves(root);
|
||||
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", leaves);
|
||||
|
||||
run_system(root);
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user