hz4
This commit is contained in:
		
							parent
							
								
									118fa833c4
								
							
						
					
					
						commit
						caf879232d
					
				| @ -10,7 +10,6 @@ typedef struct Node { | ||||
|     struct Node *no; | ||||
| } Node; | ||||
| 
 | ||||
| // Создает узел с текстом
 | ||||
| Node* create_node(const char* text) { | ||||
|     Node *node = (Node*)malloc(sizeof(Node)); | ||||
|     if (!node) { | ||||
| @ -23,51 +22,52 @@ Node* create_node(const char* text) { | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Рекурсивно создает дерево из базы знаний, хранящейся в массиве строк
 | ||||
| Node* parse_knowledge_base(char lines[][MAX_LINE_LENGTH], int *index, int line_count) { | ||||
|     if (*index >= line_count || lines[*index][0] == '\0') { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     char *line = lines[*index]; | ||||
|     (*index)++;  // Переход к следующей строке
 | ||||
|     (*index)++; | ||||
| 
 | ||||
|     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(lines, index, line_count);  // Ветка "да"
 | ||||
|         node->no = parse_knowledge_base(lines, index, line_count);   // Ветка "нет"
 | ||||
|         node = create_node(line); | ||||
|         node->yes = parse_knowledge_base(lines, index, line_count); | ||||
|         node->no = parse_knowledge_base(lines, index, line_count); | ||||
|     } | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Подсчитывает количество конечных узлов (продуктов) в дереве
 | ||||
| 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) { | ||||
|     int end_input = 0;  // Флаг, чтобы отметить "Koniec vstupu" только в нужных случаях
 | ||||
|     int first_question = 1; | ||||
| 
 | ||||
|     while (node) { | ||||
|         if (!node->yes && !node->no) {  // Достигнут конечный узел
 | ||||
|         if (first_question) { | ||||
|             printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||
|             first_question = 0; | ||||
|         } | ||||
| 
 | ||||
|         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) { | ||||
|             end_input = 1;  // Устанавливаем флаг для "Koniec vstupu"
 | ||||
|             break; | ||||
|             printf("Koniec vstupu\n"); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         if (answer == 'a') { | ||||
| @ -79,13 +79,8 @@ void run_expert_system(Node *node) { | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     if (end_input) { | ||||
|         printf("Koniec vstupu\n"); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Освобождает выделенную память для дерева
 | ||||
| void free_tree(Node *node) { | ||||
|     if (node) { | ||||
|         free_tree(node->yes); | ||||
| @ -98,16 +93,14 @@ int main() { | ||||
|     char lines[100][MAX_LINE_LENGTH]; | ||||
|     int line_count = 0; | ||||
| 
 | ||||
|     // Считываем базу знаний построчно
 | ||||
|     while (fgets(lines[line_count], sizeof(lines[line_count]), stdin)) { | ||||
|         lines[line_count][strcspn(lines[line_count], "\n")] = 0;  // Удаляем символ новой строки
 | ||||
|         if (lines[line_count][0] == '\0') {  // Пустая строка завершает ввод базы знаний
 | ||||
|         lines[line_count][strcspn(lines[line_count], "\n")] = 0; | ||||
|         if (lines[line_count][0] == '\0') { | ||||
|             break; | ||||
|         } | ||||
|         line_count++; | ||||
|     } | ||||
| 
 | ||||
|     // Создаем дерево из базы знаний
 | ||||
|     int index = 0; | ||||
|     Node *root = parse_knowledge_base(lines, &index, line_count); | ||||
| 
 | ||||
| @ -119,10 +112,8 @@ int main() { | ||||
|     int product_count = count_products(root); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", product_count); | ||||
| 
 | ||||
|     // Запуск опроса пользователя
 | ||||
|     run_expert_system(root); | ||||
| 
 | ||||
|     // Очистка памяти
 | ||||
|     free_tree(root); | ||||
| 
 | ||||
|     return 0; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user