Update cv7/program.c
This commit is contained in:
		
							parent
							
								
									282c50bfd0
								
							
						
					
					
						commit
						3f168f07af
					
				
							
								
								
									
										136
									
								
								cv7/program.c
									
									
									
									
									
								
							
							
						
						
									
										136
									
								
								cv7/program.c
									
									
									
									
									
								
							| @ -1,95 +1,103 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include <string.h> | #include <string.h> | ||||||
|  | #include <assert.h> | ||||||
| 
 | 
 | ||||||
| #define SIZE 256 | #define SIZE 100 | ||||||
|  | #define YES 'a' | ||||||
|  | #define NO 'n' | ||||||
| 
 | 
 | ||||||
| // Шструктура узла бинарного дерева
 | // Tree node structure
 | ||||||
| struct strom { | struct TreeNode { | ||||||
|     char otazka[SIZE]; |     char value[SIZE]; | ||||||
|     struct strom* ano; |     struct TreeNode* yes; | ||||||
|     struct strom* nie; |     struct TreeNode* no; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| // Функция для чтения базы правил из стандартного ввода
 | // Function to create a new tree node
 | ||||||
| struct strom* nacitaj_strom() { | struct TreeNode* create_node(const char* text) { | ||||||
|  |     struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); | ||||||
|  |     strcpy(node->value, text); | ||||||
|  |     node->yes = node->no = NULL; | ||||||
|  |     return node; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Recursive function to build the tree
 | ||||||
|  | struct TreeNode* read_tree() { | ||||||
|     char buffer[SIZE]; |     char buffer[SIZE]; | ||||||
|     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') { |     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') return NULL; | ||||||
|         return NULL;  // Конец базы данных или пустая строка
 | 
 | ||||||
|  |     // Remove newline character
 | ||||||
|  |     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
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     struct strom* uzol = malloc(sizeof(struct strom)); |     return node; | ||||||
|     if (!uzol) { |  | ||||||
|         printf("Chyba: nedostatok pamäte.\n"); |  | ||||||
|         exit(1); |  | ||||||
|     } |  | ||||||
|     strcpy(uzol->otazka, buffer); |  | ||||||
| 
 |  | ||||||
|     if (buffer[0] == '*') { |  | ||||||
|         uzol->ano = uzol->nie = NULL;  // Это лист (ответ)
 |  | ||||||
|     } else { |  | ||||||
|         uzol->ano = nacitaj_strom();  // Чтение ветви для "да"
 |  | ||||||
|         uzol->nie = nacitaj_strom();  // Чтение ветви для "нет"
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return uzol; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Функция для освобождения памяти дерева
 | // Count the leaf nodes (answers) in the tree
 | ||||||
| void znic_strom(struct strom* uzol) { | int count_leaf_nodes(struct TreeNode* node) { | ||||||
|     if (uzol) { |     if (node == NULL) return 0; | ||||||
|         znic_strom(uzol->ano); |     if (node->yes == NULL && node->no == NULL) return 1; // It's a leaf node
 | ||||||
|         znic_strom(uzol->nie); |     return count_leaf_nodes(node->yes) + count_leaf_nodes(node->no); | ||||||
|         free(uzol); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Функция для взаимодействия с пользователем (прохождение дерева)
 | // Free memory allocated for the tree
 | ||||||
| void spusti_expert_system(struct strom* uzol) { | void destroy_tree(struct TreeNode* node) { | ||||||
|     if (!uzol) return; |     if (node == NULL) return; | ||||||
|  |     destroy_tree(node->yes); | ||||||
|  |     destroy_tree(node->no); | ||||||
|  |     free(node); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|     printf("%s", uzol->otazka); | // Dialogue with user based on tree structure
 | ||||||
|     if (!uzol->ano && !uzol->nie) { | void run_dialogue(struct TreeNode* node) { | ||||||
|         printf("Koniec\n");  // Окончательный ответ
 |     if (!node) return; | ||||||
|  | 
 | ||||||
|  |     printf("%s\n", node->value); | ||||||
|  | 
 | ||||||
|  |     if (node->yes == NULL && node->no == NULL) { | ||||||
|  |         printf("Expert z bufetu to vie.\n"); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     char odpoved; |     char answer; | ||||||
|     if (scanf(" %c", &odpoved) != 1 || (odpoved != 'a' && odpoved != 'n')) { |     printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||||
|         printf("Nerozumiem\n"); |     if (scanf(" %c", &answer) != 1 || (answer != YES && answer != NO)) { | ||||||
|  |         printf("Odpovedajte 'a' alebo 'n'.\n"); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (odpoved == 'a') { |     if (answer == YES) run_dialogue(node->yes); | ||||||
|         spusti_expert_system(uzol->ano); |     else if (answer == NO) run_dialogue(node->no); | ||||||
|     } else { |  | ||||||
|         spusti_expert_system(uzol->nie); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Функция для подсчета листьев дерева (окончательных ответов)
 |  | ||||||
| int pocet_listov(struct strom* uzol) { |  | ||||||
|     if (!uzol) return 0; |  | ||||||
|     if (!uzol->ano && !uzol->nie) return 1; |  | ||||||
|     return pocet_listov(uzol->ano) + pocet_listov(uzol->nie); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Главная функция
 |  | ||||||
| int main() { | int main() { | ||||||
|     struct strom* databaza_znalosti = nacitaj_strom(); |     // Read and build tree from input
 | ||||||
| 
 |     struct TreeNode* root = read_tree(); | ||||||
|     if (!databaza_znalosti) { |      | ||||||
|         printf("Chyba: databáza je prázdna alebo nesprávne formátovaná.\n"); |     // Check if rule base ended with an empty line
 | ||||||
|  |     char check[SIZE]; | ||||||
|  |     if (!fgets(check, SIZE, stdin) || check[0] != '\n') { | ||||||
|  |         printf("Neplatna baza pravidiel.\n"); | ||||||
|  |         destroy_tree(root); | ||||||
|         return 1; |         return 1; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     int pocet_tovarov = pocet_listov(databaza_znalosti); |     // Count leaf nodes
 | ||||||
|     printf("Expert z bufetu to vie.\n"); |     int count = count_leaf_nodes(root); | ||||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", pocet_tovarov); |     printf("Pozna %d druhov ovocia a zeleniny.\n", count); | ||||||
|     printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); |  | ||||||
| 
 | 
 | ||||||
|     spusti_expert_system(databaza_znalosti); |     // Run the dialogue system
 | ||||||
|     znic_strom(databaza_znalosti); |     run_dialogue(root); | ||||||
| 
 | 
 | ||||||
|  |     // Free allocated memory
 | ||||||
|  |     destroy_tree(root); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user