43
This commit is contained in:
		
							parent
							
								
									3e37cc7666
								
							
						
					
					
						commit
						721a31f324
					
				| @ -23,26 +23,46 @@ Node* create_node(const char* text) { | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Создает заранее заданное дерево знаний
 | ||||
| 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"); | ||||
| // Создает базу знаний, если файл не существует
 | ||||
| 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, "Rastie to na strome?\n"); | ||||
|     fprintf(file, "*Jablko\n"); | ||||
|     fprintf(file, "Rastie to pod zemou?\n"); | ||||
|     fprintf(file, "*Mrkva\n"); | ||||
|     fprintf(file, "*Šalát\n"); | ||||
|     fclose(file); | ||||
| } | ||||
| 
 | ||||
|     return root; | ||||
| // Считывает базу знаний из файла и создает дерево
 | ||||
| Node* parse_knowledge_base(FILE *file) { | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
| 
 | ||||
|     if (!fgets(line, sizeof(line), file) || line[0] == '\n') { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     line[strcspn(line, "\n")] = 0;  // Удаляем символ новой строки
 | ||||
| 
 | ||||
|     Node *node; | ||||
|     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; | ||||
| } | ||||
| 
 | ||||
| // Подсчитывает количество листовых узлов (продуктов)
 | ||||
| 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); | ||||
| } | ||||
| 
 | ||||
| @ -85,8 +105,19 @@ void free_tree(Node *node) { | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     // Создаем дерево знаний вручную
 | ||||
|     Node *root = build_knowledge_tree(); | ||||
|     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 = parse_knowledge_base(file); | ||||
|     fclose(file); | ||||
| 
 | ||||
|     if (!root) { | ||||
|         printf("Báza znalostí sa nedá načítať.\n"); | ||||
| @ -98,8 +129,8 @@ int main() { | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||
| 
 | ||||
|     run_expert_system(root); | ||||
| 
 | ||||
|     free_tree(root); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user