3
This commit is contained in:
		
							parent
							
								
									ba7dc26405
								
							
						
					
					
						commit
						79c9d86bea
					
				| @ -10,11 +10,10 @@ typedef struct Node { | ||||
|     struct Node *no; | ||||
| } Node; | ||||
| 
 | ||||
| // Функция для создания нового узла
 | ||||
| Node* create_node(const char* text) { | ||||
|     Node *node = (Node*)malloc(sizeof(Node)); | ||||
|     if (!node) { | ||||
|         fprintf(stderr, "Ошибка выделения памяти.\n"); | ||||
|         fprintf(stderr, "Memory allocation error.\n"); | ||||
|         exit(1); | ||||
|     } | ||||
|     strncpy(node->text, text, MAX_LINE_LENGTH); | ||||
| @ -23,7 +22,6 @@ Node* create_node(const char* text) { | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Рекурсивная функция для построения дерева
 | ||||
| Node* parse_knowledge_base(FILE *file) { | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|      | ||||
| @ -31,14 +29,11 @@ Node* parse_knowledge_base(FILE *file) { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     // Удаление символа новой строки
 | ||||
|     line[strcspn(line, "\n")] = 0; | ||||
| 
 | ||||
|     // Если строка начинается с '*', это ответный узел
 | ||||
|     if (line[0] == '*') { | ||||
|         return create_node(line + 1);  // Пропустить '*'
 | ||||
|         return create_node(line + 1); | ||||
|     } else { | ||||
|         // Иначе это вопросный узел
 | ||||
|         Node *node = create_node(line); | ||||
|         node->yes = parse_knowledge_base(file); | ||||
|         node->no = parse_knowledge_base(file); | ||||
| @ -46,22 +41,20 @@ Node* parse_knowledge_base(FILE *file) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Функция для подсчета товаров
 | ||||
| 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); | ||||
| } | ||||
| 
 | ||||
| // Функция для взаимодействия с пользователем
 | ||||
| void run_expert_system(Node *node) { | ||||
|     while (node) { | ||||
|         if (!node->yes && !node->no) {  // Если это ответный узел
 | ||||
|             printf("Expert z bufetu to vie: %s.\n", node->text); | ||||
|         if (!node->yes && !node->no) { | ||||
|             printf("The expert knows: %s.\n", node->text); | ||||
|             return; | ||||
|         } | ||||
|         printf("%s\n", node->text); | ||||
|         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost: "); | ||||
|         printf("Answer 'a' for the first option or 'n' for the second option: "); | ||||
|         char answer; | ||||
|         scanf(" %c", &answer); | ||||
|         if (answer == 'a') { | ||||
| @ -69,34 +62,34 @@ void run_expert_system(Node *node) { | ||||
|         } else if (answer == 'n') { | ||||
|             node = node->no; | ||||
|         } else { | ||||
|             printf("Neplatná odpoveď. Koniec.\n"); | ||||
|             printf("Invalid answer. Exiting.\n"); | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Основная функция
 | ||||
| int main() { | ||||
|     printf("Attempting to open the knowledge base file...\n"); | ||||
|     FILE *file = fopen("knowledge_base.txt", "r"); | ||||
|     if (!file) { | ||||
|         fprintf(stderr, "Не удалось открыть файл базы знаний.\n"); | ||||
|         perror("Failed to open the knowledge base file"); | ||||
|         return 1; | ||||
|     } | ||||
|     printf("File successfully opened.\n"); | ||||
| 
 | ||||
|     Node *root = parse_knowledge_base(file); | ||||
|     fclose(file); | ||||
| 
 | ||||
|     if (!root) { | ||||
|         printf("База знаний не может быть загружена.\n"); | ||||
|         printf("The knowledge base could not be loaded.\n"); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     int product_count = count_products(root); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||
|     printf("The system knows about %d types of fruits and vegetables.\n", product_count); | ||||
|      | ||||
|     run_expert_system(root); | ||||
| 
 | ||||
|     // Освобождение памяти
 | ||||
|     free(root); | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user