du5
This commit is contained in:
parent
2c732eb6be
commit
6d96a58aa6
@ -11,21 +11,22 @@ typedef struct Tree {
|
|||||||
struct Tree *no;
|
struct Tree *no;
|
||||||
} Tree;
|
} Tree;
|
||||||
|
|
||||||
// читання дерева
|
// читання дерева у preorder
|
||||||
Tree* readTree() {
|
Tree* readTree() {
|
||||||
char line[SIZE];
|
char line[SIZE];
|
||||||
|
|
||||||
if (fgets(line, SIZE, stdin) == NULL) return NULL;
|
if (fgets(line, SIZE, stdin) == NULL) return NULL;
|
||||||
|
|
||||||
// якщо пустий рядок → кінець бази
|
// кінець дерева = порожній рядок
|
||||||
|
if (strcmp(line, "\n") == 0) return NULL;
|
||||||
|
|
||||||
|
// прибрати \n і \r
|
||||||
line[strcspn(line, "\r\n")] = 0;
|
line[strcspn(line, "\r\n")] = 0;
|
||||||
if (line[0] == '\0') return NULL;
|
|
||||||
|
|
||||||
Tree *node = malloc(sizeof(Tree));
|
Tree *node = malloc(sizeof(Tree));
|
||||||
node->yes = NULL;
|
node->yes = NULL;
|
||||||
node->no = NULL;
|
node->no = NULL;
|
||||||
|
|
||||||
|
|
||||||
if (line[0] == '*') {
|
if (line[0] == '*') {
|
||||||
node->isAnswer = 1;
|
node->isAnswer = 1;
|
||||||
|
|
||||||
@ -36,37 +37,32 @@ Tree* readTree() {
|
|||||||
node->isAnswer = 0;
|
node->isAnswer = 0;
|
||||||
strcpy(node->text, line);
|
strcpy(node->text, line);
|
||||||
|
|
||||||
node->yes = readTree(); // a
|
node->yes = readTree();
|
||||||
node->no = readTree(); // n
|
node->no = readTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// перевірка що після дерева є пустий рядок
|
// перевірка пустого рядка після дерева
|
||||||
int EmptyLine() {
|
int EmptyLine() {
|
||||||
char line[SIZE];
|
char line[SIZE];
|
||||||
|
|
||||||
if (fgets(line, SIZE, stdin) == NULL) return 0;
|
if (fgets(line, SIZE, stdin) == NULL) return 0;
|
||||||
|
|
||||||
if (strcmp(line, "\n") == 0) return 1;
|
return strcmp(line, "\n") == 0;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// рахуємо відповіді (листи)
|
// підрахунок листів
|
||||||
int Leaves(Tree *node) {
|
int Leaves(Tree *node) {
|
||||||
if (node == NULL) return 0;
|
if (!node) return 0;
|
||||||
|
|
||||||
if (node->isAnswer) return 1;
|
if (node->isAnswer) return 1;
|
||||||
|
|
||||||
return Leaves(node->yes) + Leaves(node->no);
|
return Leaves(node->yes) + Leaves(node->no);
|
||||||
}
|
}
|
||||||
|
|
||||||
// звільнення памʼяті
|
// звільнення пам’яті
|
||||||
void freeTree(Tree *node) {
|
void freeTree(Tree *node) {
|
||||||
if (node == NULL) return;
|
if (!node) return;
|
||||||
|
|
||||||
freeTree(node->yes);
|
freeTree(node->yes);
|
||||||
freeTree(node->no);
|
freeTree(node->no);
|
||||||
free(node);
|
free(node);
|
||||||
@ -74,31 +70,33 @@ void freeTree(Tree *node) {
|
|||||||
|
|
||||||
// запуск системи
|
// запуск системи
|
||||||
void start(Tree *node) {
|
void start(Tree *node) {
|
||||||
char input[10];
|
char input[SIZE];
|
||||||
|
|
||||||
while (node != NULL) {
|
|
||||||
if (node->isAnswer) {
|
|
||||||
printf("* %s\n", node->text);
|
|
||||||
printf("Koniec\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
while (node && !node->isAnswer) {
|
||||||
printf("%s\n", node->text);
|
printf("%s\n", node->text);
|
||||||
|
|
||||||
if (fgets(input, 10, stdin) == NULL) {
|
if (fgets(input, SIZE, stdin) == NULL) {
|
||||||
printf("Koniec\n");
|
printf("Nespravny vstup\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input[0] == 'a') {
|
int i = 0;
|
||||||
|
while (input[i] == ' ' || input[i] == '\t') i++;
|
||||||
|
|
||||||
|
if (input[i] == 'a') {
|
||||||
node = node->yes;
|
node = node->yes;
|
||||||
} else if (input[0] == 'n') {
|
} else if (input[i] == 'n') {
|
||||||
node = node->no;
|
node = node->no;
|
||||||
} else {
|
} else {
|
||||||
printf("Nerozumiem\n");
|
printf("Nespravny vstup\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (node && node->isAnswer) {
|
||||||
|
printf("*%s\n", node->text);
|
||||||
|
printf("Koniec\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -106,13 +104,16 @@ int main() {
|
|||||||
|
|
||||||
Tree *root = readTree();
|
Tree *root = readTree();
|
||||||
|
|
||||||
// перевірка чи дерево існує
|
if (!root) {
|
||||||
if (root == NULL) {
|
|
||||||
printf("Chyba nacitania\n");
|
printf("Chyba nacitania\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!EmptyLine()) {
|
||||||
|
printf("Chyba nacitania\n");
|
||||||
|
freeTree(root);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int count = Leaves(root);
|
int count = Leaves(root);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user