36
This commit is contained in:
		
							parent
							
								
									43af0bd18e
								
							
						
					
					
						commit
						b63a8fce5f
					
				| @ -1,7 +1,6 @@ | ||||
| Je to ovocie alebo zelenina | ||||
| Je to ovocie alebo zelenina? | ||||
| *Jablko | ||||
| *Mrkva | ||||
| 
 | ||||
| a | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -25,47 +25,48 @@ Node* create_node(const char* text) { | ||||
| Node* parse_knowledge_base(FILE *file) { | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|      | ||||
|     if (!fgets(line, sizeof(line), file)) { | ||||
|     if (!fgets(line, sizeof(line), file) || line[0] == '\n') { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     line[strcspn(line, "\n")] = 0; // Remove newline character
 | ||||
|     line[strcspn(line, "\n")] = 0; // Удалить символ новой строки
 | ||||
| 
 | ||||
|     Node *node = NULL; | ||||
|     if (line[0] == '*') { | ||||
|         return create_node(line + 1); // Create answer node, skip '*'
 | ||||
|         node = create_node(line + 1); // Создаём конечный узел (ответ)
 | ||||
|     } else { | ||||
|         Node *node = create_node(line); | ||||
|         node->yes = parse_knowledge_base(file); // Recursively read "yes" answer
 | ||||
|         node->no = parse_knowledge_base(file);  // Recursively read "no" answer
 | ||||
|         return node; | ||||
|         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 is a leaf (answer)
 | ||||
|     if (!node->yes && !node->no) return 1; // Если это ответ, возвращаем 1
 | ||||
|     return count_products(node->yes) + count_products(node->no); | ||||
| } | ||||
| 
 | ||||
| void run_expert_system(Node *node) { | ||||
| void run_expert_system(Node *node, FILE *input) { | ||||
|     while (node) { | ||||
|         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||
|         printf("%s\n", node->text); | ||||
| 
 | ||||
|         // Проверка, если это листовой узел
 | ||||
|         // Проверка, если это листовой узел (ответ)
 | ||||
|         if (!node->yes && !node->no) { | ||||
|             printf("*%s\n", node->text); // Выводим ответ с звездочкой
 | ||||
|             printf("Koniec\n"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||
|         printf("%s\n", node->text); | ||||
| 
 | ||||
|         char answer; | ||||
|         if (scanf(" %c", &answer) != 1) { | ||||
|         if (fscanf(input, " %c", &answer) != 1) { | ||||
|             printf("Koniec\n"); // Некорректный ввод
 | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // Переход к следующему узлу
 | ||||
|         // Переход по дереву на основе ответа
 | ||||
|         if (answer == 'a') { | ||||
|             node = node->yes; | ||||
|         } else if (answer == 'n') { | ||||
| @ -74,16 +75,10 @@ void run_expert_system(Node *node) { | ||||
|             printf("Koniec\n"); // Некорректный ввод
 | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // Дополнительная проверка для вывода конечного узла
 | ||||
|         if (node && !node->yes && !node->no) { | ||||
|             printf("*%s\n", node->text); | ||||
|             printf("Koniec\n"); | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void free_tree(Node *node) { | ||||
|     if (node) { | ||||
|         free_tree(node->yes); | ||||
| @ -92,44 +87,32 @@ 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"); | ||||
|     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 newly created knowledge base file"); | ||||
|             return 1; | ||||
|         } | ||||
|         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"); | ||||
|         fclose(file); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     printf("Expert z bufetu to vie.\n"); | ||||
|     int product_count = count_products(root); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||
|      | ||||
|     run_expert_system(root); | ||||
| 
 | ||||
|     // Пропуск пустой строки, которая разделяет базу знаний и ответы
 | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|     while (fgets(line, sizeof(line), file) && line[0] != '\n'); | ||||
| 
 | ||||
|     run_expert_system(root, file); | ||||
| 
 | ||||
|     free_tree(root); | ||||
|     fclose(file); | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user