100 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <string.h>
 | |
| 
 | |
| #define MAX_LINE_LENGTH 100
 | |
| 
 | |
| typedef struct Node {
 | |
|     char text[MAX_LINE_LENGTH];
 | |
|     struct Node *yes;
 | |
|     struct Node *no;
 | |
| } Node;
 | |
| 
 | |
| Node* create_node(const char* text) {
 | |
|     Node *node = (Node*)malloc(sizeof(Node));
 | |
|     if (!node) {
 | |
|         fprintf(stderr, "Memory allocation error.\n");
 | |
|         exit(1);
 | |
|     }
 | |
|     strncpy(node->text, text, MAX_LINE_LENGTH);
 | |
|     node->yes = NULL;
 | |
|     node->no = NULL;
 | |
|     return node;
 | |
| }
 | |
| 
 | |
| Node* build_knowledge_tree() {
 | |
|     // Создаем корневой узел
 | |
|     Node *root = create_node("Rastie to na strome?");
 | |
|     
 | |
|     // Создаем дочерние узлы для ответа "да" и "нет"
 | |
|     root->yes = create_node("Jablko");
 | |
|     root->no = create_node("Rastie to pod zemou?");
 | |
|     
 | |
|     // Создаем узлы для поддерева "Rastie to pod zemou?"
 | |
|     root->no->yes = create_node("Mrkva");
 | |
|     root->no->no = create_node("Šalát");
 | |
| 
 | |
|     return root;
 | |
| }
 | |
| 
 | |
| int count_products(Node *node) {
 | |
|     if (!node) return 0;
 | |
|     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("Koniec\n");
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
 | |
|         printf("%s\n", node->text);
 | |
| 
 | |
|         char answer;
 | |
|         if (fscanf(input, " %c", &answer) != 1) {
 | |
|             printf("Koniec\n");
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         if (answer == 'a') {
 | |
|             node = node->yes;
 | |
|         } else if (answer == 'n') {
 | |
|             node = node->no;
 | |
|         } else {
 | |
|             printf("Nespravny vstup.\nKoniec\n");
 | |
|             return;
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| void free_tree(Node *node) {
 | |
|     if (node) {
 | |
|         free_tree(node->yes);
 | |
|         free_tree(node->no);
 | |
|         free(node);
 | |
|     }
 | |
| }
 | |
| 
 | |
| int main() {
 | |
|     Node *root = build_knowledge_tree();
 | |
| 
 | |
|     if (!root) {
 | |
|         printf("Báza znalostí sa nedá načítať.\n");
 | |
|         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);
 | |
| 
 | |
|     run_expert_system(root, stdin);  // Используем стандартный ввод для ответов
 | |
| 
 | |
|     free_tree(root);
 | |
|     return 0;
 | |
| }
 | |
| 
 |