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