This commit is contained in:
Deinerovych 2024-11-08 14:20:41 +01:00
parent cf0cb60c78
commit 2e0d7f7853
2 changed files with 19 additions and 11 deletions

BIN
cv7/.program.c.swp Normal file

Binary file not shown.

View File

@ -41,15 +41,24 @@ Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_c
return node; return node;
} }
int is_valid_tree(Node* node) { int validate_tree_structure(char lines[][MAX_LINE_LENGTH], int line_count) {
if (!node) return 0; int leaf_count = 0;
if ((!node->yes && node->no) || (node->yes && !node->no)) { int question_count = 0;
return 0; // Некорректное состояние: одна ветка отсутствует
for (int i = 0; i < line_count; i++) {
if (lines[i][0] == '*') {
leaf_count++;
} else {
question_count++;
if (i + 2 >= line_count || lines[i + 1][0] == '*' && lines[i + 2][0] == '*') {
continue;
} else if (i + 2 < line_count && lines[i + 2][0] != '*') {
return 0; // Обнаружена некорректная структура (более двух ответов)
}
}
} }
if (!node->yes && !node->no) {
return 1; // Листовой узел return question_count >= leaf_count;
}
return is_valid_tree(node->yes) && is_valid_tree(node->no);
} }
int count_products(Node *node) { int count_products(Node *node) {
@ -112,7 +121,7 @@ int main() {
line_count++; line_count++;
} }
if (line_count == 0) { if (line_count == 0 || !validate_tree_structure(lines, line_count)) {
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n"); printf("Chybna databaza\n");
return 0; return 0;
@ -121,10 +130,9 @@ int main() {
int index = 0; int index = 0;
Node *root = parse_knowledge_base(lines, &index, line_count); Node *root = parse_knowledge_base(lines, &index, line_count);
if (!root || !is_valid_tree(root)) { if (!root) {
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n"); printf("Chybna databaza\n");
free_tree(root);
return 0; return 0;
} }