final
This commit is contained in:
		
							parent
							
								
									f4148bde44
								
							
						
					
					
						commit
						4f6ad9c280
					
				
							
								
								
									
										5
									
								
								cv7/knowledge_base.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								cv7/knowledge_base.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| Je to ovocie alebo zelenina | ||||
| *Jablko | ||||
| *Mrkva | ||||
| 
 | ||||
| a | ||||
							
								
								
									
										
											BIN
										
									
								
								cv7/program
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								cv7/program
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -29,30 +29,29 @@ Node* parse_knowledge_base(FILE *file) { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     line[strcspn(line, "\n")] = 0; // Удалить символ новой строки
 | ||||
|     line[strcspn(line, "\n")] = 0; | ||||
| 
 | ||||
|     Node *node = NULL; | ||||
|     if (line[0] == '*') { | ||||
|         node = create_node(line + 1); // Создаём конечный узел (ответ)
 | ||||
|         node = create_node(line + 1); | ||||
|     } else { | ||||
|         node = create_node(line); // Создаём узел с вопросом
 | ||||
|         node->yes = parse_knowledge_base(file); // Рекурсивное создание поддерева для "да"
 | ||||
|         node->no = parse_knowledge_base(file);  // Рекурсивное создание поддерева для "нет"
 | ||||
|         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; // Если это ответ, возвращаем 1
 | ||||
|     if (!node->yes && !node->no) return 1; | ||||
|     return count_products(node->yes) + count_products(node->no); | ||||
| } | ||||
| 
 | ||||
| void run_expert_system(Node *node, FILE *input) { | ||||
|     while (node) { | ||||
|         // Проверка, если это листовой узел (ответ)
 | ||||
|         if (!node->yes && !node->no) { | ||||
|             printf("*%s\n", node->text); // Выводим ответ с звездочкой
 | ||||
|             printf("*%s\n", node->text); | ||||
|             printf("Koniec\n"); | ||||
|             return; | ||||
|         } | ||||
| @ -62,17 +61,16 @@ void run_expert_system(Node *node, FILE *input) { | ||||
| 
 | ||||
|         char answer; | ||||
|         if (fscanf(input, " %c", &answer) != 1) { | ||||
|             printf("Koniec\n"); // Некорректный ввод
 | ||||
|             printf("Koniec\n"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // Переход по дереву на основе ответа
 | ||||
|         if (answer == 'a') { | ||||
|             node = node->yes; | ||||
|         } else if (answer == 'n') { | ||||
|             node = node->no; | ||||
|         } else { | ||||
|             printf("Koniec\n"); // Некорректный ввод
 | ||||
|             printf("Koniec\n"); | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
| @ -92,22 +90,22 @@ void create_default_knowledge_base() { | ||||
|         perror("Failed to create the knowledge base file"); | ||||
|         exit(1); | ||||
|     } | ||||
|     fprintf(file, "Je to ovocie alebo zelenina?\n"); | ||||
|     fprintf(file, "Je to ovocie alebo zelenina\n"); | ||||
|     fprintf(file, "*Jablko\n"); | ||||
|     fprintf(file, "*Mrkva\n"); | ||||
|     fprintf(file, "\n"); // Пустая строка для разделения базы знаний и ответов
 | ||||
|     fprintf(file, "a\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();  // Создаем файл по умолчанию
 | ||||
|         create_default_knowledge_base(); | ||||
|         file = fopen("knowledge_base.txt", "r"); | ||||
|         if (!file) { | ||||
|             perror("Failed to open the knowledge base file"); | ||||
|             return 1; // Завершение с кодом ошибки, если файл так и не открылся
 | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| @ -115,21 +113,20 @@ int main() { | ||||
|     if (!root) { | ||||
|         printf("Báza znalostí sa nedá načítať.\n"); | ||||
|         fclose(file); | ||||
|         return 1; // Завершение с кодом ошибки, если база знаний не загружена
 | ||||
|         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); | ||||
| 
 | ||||
|     // Пропуск пустой строки после базы знаний
 | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|     while (fgets(line, sizeof(line), file) && line[0] != '\n'); | ||||
| 
 | ||||
|     run_expert_system(root, file); // Запуск системы с использованием оставшихся ответов
 | ||||
|     run_expert_system(root, file); | ||||
| 
 | ||||
|     free_tree(root); | ||||
|     fclose(file); | ||||
|     return 0; // Успешное завершение программы
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user