43
This commit is contained in:
		
							parent
							
								
									3e37cc7666
								
							
						
					
					
						commit
						721a31f324
					
				| @ -23,26 +23,46 @@ Node* create_node(const char* text) { | |||||||
|     return node; |     return node; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Создает заранее заданное дерево знаний
 | // Создает базу знаний, если файл не существует
 | ||||||
| Node* build_knowledge_tree() { | void create_default_knowledge_base() { | ||||||
|     // Создаем корневой узел
 |     FILE *file = fopen("knowledge_base.txt", "w"); | ||||||
|     Node *root = create_node("Rastie to na strome?"); |     if (!file) { | ||||||
|      |         perror("Failed to create the knowledge base file"); | ||||||
|     // Добавляем варианты ответов
 |         exit(1); | ||||||
|     root->yes = create_node("Jablko"); |     } | ||||||
|     root->no = create_node("Rastie to pod zemou?"); |     fprintf(file, "Rastie to na strome?\n"); | ||||||
|      |     fprintf(file, "*Jablko\n"); | ||||||
|     // Узлы для "Rastie to pod zemou?"
 |     fprintf(file, "Rastie to pod zemou?\n"); | ||||||
|     root->no->yes = create_node("Mrkva"); |     fprintf(file, "*Mrkva\n"); | ||||||
|     root->no->no = create_node("Šalát"); |     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) { | int count_products(Node *node) { | ||||||
|     if (!node) return 0; |     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); |     return count_products(node->yes) + count_products(node->no); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -85,8 +105,19 @@ void free_tree(Node *node) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|     // Создаем дерево знаний вручную
 |     FILE *file = fopen("knowledge_base.txt", "r"); | ||||||
|     Node *root = build_knowledge_tree(); |     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) { |     if (!root) { | ||||||
|         printf("Báza znalostí sa nedá načítať.\n"); |         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); |     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||||
| 
 | 
 | ||||||
|     run_expert_system(root); |     run_expert_system(root); | ||||||
| 
 |  | ||||||
|     free_tree(root); |     free_tree(root); | ||||||
|  | 
 | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user