41
This commit is contained in:
		
							parent
							
								
									09115abc71
								
							
						
					
					
						commit
						48311a443e
					
				| @ -22,33 +22,24 @@ Node* create_node(const char* text) { | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| Node* parse_knowledge_base(FILE *file) { | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
| Node* build_knowledge_tree() { | ||||
|     // Создаем корневой узел
 | ||||
|     Node *root = create_node("Rastie to na strome?"); | ||||
|      | ||||
|     // Создаем дочерние узлы для ответа "да" и "нет"
 | ||||
|     root->yes = create_node("Jablko"); | ||||
|     root->no = create_node("Rastie to pod zemou?"); | ||||
|      | ||||
|     // Создаем узлы для поддерева "Rastie to pod zemou?"
 | ||||
|     root->no->yes = create_node("Mrkva"); | ||||
|     root->no->no = create_node("Šalát"); | ||||
| 
 | ||||
|     // Считываем строку из файла
 | ||||
|     if (!fgets(line, sizeof(line), file) || line[0] == '\n') { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     // Удаляем символ новой строки
 | ||||
|     line[strcspn(line, "\n")] = 0; | ||||
| 
 | ||||
|     // Создаем узел, проверяем, является ли это ответом (начинается с '*')
 | ||||
|     Node *node = NULL; | ||||
|     if (line[0] == '*') { | ||||
|         node = create_node(line + 1);  // Создаем узел-ответ
 | ||||
|     } else { | ||||
|         node = create_node(line);      // Создаем узел-вопрос
 | ||||
|         node->yes = parse_knowledge_base(file);  // Переход для ответа "да"
 | ||||
|         node->no = parse_knowledge_base(file);   // Переход для ответа "нет"
 | ||||
|     } | ||||
|     return node; | ||||
|     return root; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| int count_products(Node *node) { | ||||
|     if (!node) return 0; | ||||
|     if (!node->yes && !node->no) return 1; | ||||
|     if (!node->yes && !node->no) return 1;  // Листовой узел - это продукт
 | ||||
|     return count_products(node->yes) + count_products(node->no); | ||||
| } | ||||
| 
 | ||||
| @ -80,7 +71,6 @@ void run_expert_system(Node *node, FILE *input) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void free_tree(Node *node) { | ||||
|     if (node) { | ||||
|         free_tree(node->yes); | ||||
| @ -89,35 +79,11 @@ void free_tree(Node *node) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void create_default_knowledge_base() { | ||||
|     FILE *file = fopen("knowledge_base.txt", "w"); | ||||
|     if (!file) { | ||||
|         perror("Failed to create the knowledge base file"); | ||||
|         exit(1); | ||||
|     } | ||||
|     fprintf(file, "Je to ovocie alebo zelenina\n"); | ||||
|     fprintf(file, "*Jablko\n"); | ||||
|     fprintf(file, "*Mrkva\n"); | ||||
|     fprintf(file, "\n"); | ||||
|     fprintf(file, "a\n"); | ||||
|     fclose(file); | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     FILE *file = fopen("knowledge_base.txt", "r"); | ||||
|     if (!file) { | ||||
|         create_default_knowledge_base(); | ||||
|         file = fopen("knowledge_base.txt", "r"); | ||||
|         if (!file) { | ||||
|             perror("Failed to open the knowledge base file"); | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
|     Node *root = build_knowledge_tree(); | ||||
| 
 | ||||
|     Node *root = parse_knowledge_base(file); | ||||
|     if (!root) { | ||||
|         printf("Báza znalostí sa nedá načítať.\n"); | ||||
|         fclose(file); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
| @ -125,14 +91,9 @@ int main() { | ||||
|     int product_count = count_products(root); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||
| 
 | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|     while (fgets(line, sizeof(line), file) && line[0] != '\n'); | ||||
| 
 | ||||
|     run_expert_system(root, file); | ||||
|     run_expert_system(root, stdin);  // Используем стандартный ввод для ответов
 | ||||
| 
 | ||||
|     free_tree(root); | ||||
|     fclose(file); | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user