This commit is contained in:
Deinerovych 2024-11-08 14:24:26 +01:00
parent 2e0d7f7853
commit 79d2450671

View File

@ -41,24 +41,11 @@ Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_c
return node; return node;
} }
int validate_tree_structure(char lines[][MAX_LINE_LENGTH], int line_count) { int validate_tree(Node* node) {
int leaf_count = 0; if (!node) return 0;
int question_count = 0; if ((!node->yes && node->no) || (node->yes && !node->no)) return 0;
if (!node->yes && !node->no) return 1;
for (int i = 0; i < line_count; i++) { return validate_tree(node->yes) && validate_tree(node->no);
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; // Обнаружена некорректная структура (более двух ответов)
}
}
}
return question_count >= leaf_count;
} }
int count_products(Node *node) { int count_products(Node *node) {
@ -121,7 +108,7 @@ int main() {
line_count++; line_count++;
} }
if (line_count == 0 || !validate_tree_structure(lines, line_count)) { if (line_count == 0) {
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;
@ -130,9 +117,10 @@ 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) { if (!root || !validate_tree(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;
} }