Update cv7/program.c
This commit is contained in:
		
							parent
							
								
									3f168f07af
								
							
						
					
					
						commit
						8f023f7b82
					
				
							
								
								
									
										137
									
								
								cv7/program.c
									
									
									
									
									
								
							
							
						
						
									
										137
									
								
								cv7/program.c
									
									
									
									
									
								
							| @ -1,103 +1,80 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> |  | ||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <assert.h> |  | ||||||
| 
 | 
 | ||||||
| #define SIZE 100 | // Структура узла для бинарного дерева
 | ||||||
| #define YES 'a' | typedef struct Node { | ||||||
| #define NO 'n' |     char question[100]; | ||||||
|  |     struct Node *yes; | ||||||
|  |     struct Node *no; | ||||||
|  | } Node; | ||||||
| 
 | 
 | ||||||
| // Tree node structure
 | // Прототипы функций
 | ||||||
| struct TreeNode { | Node* createNode(char *text); | ||||||
|     char value[SIZE]; | void freeTree(Node *root); | ||||||
|     struct TreeNode* yes; | void askQuestions(Node *root); | ||||||
|     struct TreeNode* no; |  | ||||||
| }; |  | ||||||
| 
 | 
 | ||||||
| // Function to create a new tree node
 | // Функция для создания нового узла дерева
 | ||||||
| struct TreeNode* create_node(const char* text) { | Node* createNode(char *text) { | ||||||
|     struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); |     Node *node = (Node*)malloc(sizeof(Node)); | ||||||
|     strcpy(node->value, text); |     strcpy(node->question, text); | ||||||
|     node->yes = node->no = NULL; |     node->yes = NULL; | ||||||
|  |     node->no = NULL; | ||||||
|     return node; |     return node; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Recursive function to build the tree
 | // Функция для освобождения памяти дерева
 | ||||||
| struct TreeNode* read_tree() { | void freeTree(Node *root) { | ||||||
|     char buffer[SIZE]; |     if (root != NULL) { | ||||||
|     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') return NULL; |         freeTree(root->yes); | ||||||
| 
 |         freeTree(root->no); | ||||||
|     // Remove newline character
 |         free(root); | ||||||
|     buffer[strcspn(buffer, "\n")] = 0; |     } | ||||||
| 
 |  | ||||||
|     struct TreeNode* node = create_node(buffer); |  | ||||||
| 
 |  | ||||||
|     // Check if it's a leaf node
 |  | ||||||
|     if (buffer[0] != '*') { |  | ||||||
|         node->yes = read_tree(); // Read 'yes' branch
 |  | ||||||
|         node->no = read_tree();  // Read 'no' branch
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return node; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Count the leaf nodes (answers) in the tree
 |  | ||||||
| int count_leaf_nodes(struct TreeNode* node) { |  | ||||||
|     if (node == NULL) return 0; |  | ||||||
|     if (node->yes == NULL && node->no == NULL) return 1; // It's a leaf node
 |  | ||||||
|     return count_leaf_nodes(node->yes) + count_leaf_nodes(node->no); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Free memory allocated for the tree
 |  | ||||||
| void destroy_tree(struct TreeNode* node) { |  | ||||||
|     if (node == NULL) return; |  | ||||||
|     destroy_tree(node->yes); |  | ||||||
|     destroy_tree(node->no); |  | ||||||
|     free(node); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Dialogue with user based on tree structure
 |  | ||||||
| void run_dialogue(struct TreeNode* node) { |  | ||||||
|     if (!node) return; |  | ||||||
| 
 |  | ||||||
|     printf("%s\n", node->value); |  | ||||||
| 
 |  | ||||||
|     if (node->yes == NULL && node->no == NULL) { |  | ||||||
|         printf("Expert z bufetu to vie.\n"); |  | ||||||
|         return; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // Функция для задания вопросов и получения ответов
 | ||||||
|  | void askQuestions(Node *root) { | ||||||
|  |     Node *current = root; | ||||||
|     char answer; |     char answer; | ||||||
|  |      | ||||||
|  |     while (current->yes != NULL && current->no != NULL) { | ||||||
|  |         printf("%s\n", current->question); | ||||||
|         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); |         printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||||
|     if (scanf(" %c", &answer) != 1 || (answer != YES && answer != NO)) { |          | ||||||
|         printf("Odpovedajte 'a' alebo 'n'.\n"); |         answer = getchar(); | ||||||
|         return; |         getchar(); // Для захвата символа новой строки после ввода
 | ||||||
|  |          | ||||||
|  |         if (answer == 'a') { | ||||||
|  |             current = current->yes; | ||||||
|  |         } else if (answer == 'n') { | ||||||
|  |             current = current->no; | ||||||
|  |         } else { | ||||||
|  |             printf("Nespravna odpoved. Skuste znova.\n"); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     if (answer == YES) run_dialogue(node->yes); |     printf("*%s\n", current->question); // Вывод названия конечного объекта с *
 | ||||||
|     else if (answer == NO) run_dialogue(node->no); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|     // Read and build tree from input
 |     // Сообщение о старте
 | ||||||
|     struct TreeNode* root = read_tree(); |     printf("Expert z bufetu to vie.\n"); | ||||||
| 
 | 
 | ||||||
|     // Check if rule base ended with an empty line
 |     // Создание дерева для вопросов
 | ||||||
|     char check[SIZE]; |     Node *root = createNode("Je to ovocie alebo zelenina"); | ||||||
|     if (!fgets(check, SIZE, stdin) || check[0] != '\n') { |     root->yes = createNode("Jablko"); | ||||||
|         printf("Neplatna baza pravidiel.\n"); |     root->no = createNode("Mrkva"); | ||||||
|         destroy_tree(root); |  | ||||||
|         return 1; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     // Count leaf nodes
 |     // Вывод количества видов
 | ||||||
|     int count = count_leaf_nodes(root); |     printf("Pozna 2 druhov ovocia a zeleniny.\n"); | ||||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", count); |  | ||||||
| 
 | 
 | ||||||
|     // Run the dialogue system
 |     // Процесс вопросов и ответов
 | ||||||
|     run_dialogue(root); |     askQuestions(root); | ||||||
|  | 
 | ||||||
|  |     // Сообщение о завершении
 | ||||||
|  |     printf("Koniec\n"); | ||||||
|  | 
 | ||||||
|  |     // Освобождение памяти
 | ||||||
|  |     freeTree(root); | ||||||
| 
 | 
 | ||||||
|     // Free allocated memory
 |  | ||||||
|     destroy_tree(root); |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user