41
This commit is contained in:
		
							parent
							
								
									7758076eaf
								
							
						
					
					
						commit
						09115abc71
					
				| @ -24,24 +24,28 @@ Node* create_node(const char* text) { | ||||
| 
 | ||||
| Node* parse_knowledge_base(FILE *file) { | ||||
|     char line[MAX_LINE_LENGTH]; | ||||
|      | ||||
| 
 | ||||
|     // Считываем строку из файла
 | ||||
|     if (!fgets(line, sizeof(line), file) || line[0] == '\n') { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     // Удаляем символ новой строки
 | ||||
|     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; | ||||
| @ -70,12 +74,13 @@ void run_expert_system(Node *node, FILE *input) { | ||||
|         } else if (answer == 'n') { | ||||
|             node = node->no; | ||||
|         } else { | ||||
|             printf("Koniec\n"); | ||||
|             printf("Nespravny vstup.\nKoniec\n"); | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void free_tree(Node *node) { | ||||
|     if (node) { | ||||
|         free_tree(node->yes); | ||||
| @ -90,11 +95,11 @@ void create_default_knowledge_base() { | ||||
|         perror("Failed to create the knowledge base file"); | ||||
|         exit(1); | ||||
|     } | ||||
|     fprintf(file, "Rastie to na strome?\n"); | ||||
|     fprintf(file, "Je to ovocie alebo zelenina\n"); | ||||
|     fprintf(file, "*Jablko\n"); | ||||
|     fprintf(file, "Rastie to pod zemou?\n"); | ||||
|     fprintf(file, "*Mrkva\n"); | ||||
|     fprintf(file, "*Šalát\n"); | ||||
|     fprintf(file, "\n"); | ||||
|     fprintf(file, "a\n"); | ||||
|     fclose(file); | ||||
| } | ||||
| 
 | ||||
| @ -130,3 +135,4 @@ int main() { | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user