Update cv7/program.c
This commit is contained in:
		
							parent
							
								
									282c50bfd0
								
							
						
					
					
						commit
						3f168f07af
					
				
							
								
								
									
										134
									
								
								cv7/program.c
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								cv7/program.c
									
									
									
									
									
								
							| @ -1,95 +1,103 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <assert.h> | ||||
| 
 | ||||
| #define SIZE 256 | ||||
| #define SIZE 100 | ||||
| #define YES 'a' | ||||
| #define NO 'n' | ||||
| 
 | ||||
| // Шструктура узла бинарного дерева
 | ||||
| struct strom { | ||||
|     char otazka[SIZE]; | ||||
|     struct strom* ano; | ||||
|     struct strom* nie; | ||||
| // Tree node structure
 | ||||
| struct TreeNode { | ||||
|     char value[SIZE]; | ||||
|     struct TreeNode* yes; | ||||
|     struct TreeNode* no; | ||||
| }; | ||||
| 
 | ||||
| // Функция для чтения базы правил из стандартного ввода
 | ||||
| struct strom* nacitaj_strom() { | ||||
| // Function to create a new tree node
 | ||||
| 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]; | ||||
|     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') { | ||||
|         return NULL;  // Конец базы данных или пустая строка
 | ||||
|     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') 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)); | ||||
|     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; | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Функция для освобождения памяти дерева
 | ||||
| void znic_strom(struct strom* uzol) { | ||||
|     if (uzol) { | ||||
|         znic_strom(uzol->ano); | ||||
|         znic_strom(uzol->nie); | ||||
|         free(uzol); | ||||
|     } | ||||
| // 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); | ||||
| } | ||||
| 
 | ||||
| // Функция для взаимодействия с пользователем (прохождение дерева)
 | ||||
| void spusti_expert_system(struct strom* uzol) { | ||||
|     if (!uzol) return; | ||||
| // 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); | ||||
| } | ||||
| 
 | ||||
|     printf("%s", uzol->otazka); | ||||
|     if (!uzol->ano && !uzol->nie) { | ||||
|         printf("Koniec\n");  // Окончательный ответ
 | ||||
| // 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; | ||||
|     } | ||||
| 
 | ||||
|     char odpoved; | ||||
|     if (scanf(" %c", &odpoved) != 1 || (odpoved != 'a' && odpoved != 'n')) { | ||||
|         printf("Nerozumiem\n"); | ||||
|     char answer; | ||||
|     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"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     if (odpoved == 'a') { | ||||
|         spusti_expert_system(uzol->ano); | ||||
|     } else { | ||||
|         spusti_expert_system(uzol->nie); | ||||
|     } | ||||
|     if (answer == YES) run_dialogue(node->yes); | ||||
|     else if (answer == NO) run_dialogue(node->no); | ||||
| } | ||||
| 
 | ||||
| // Функция для подсчета листьев дерева (окончательных ответов)
 | ||||
| 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() { | ||||
|     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; | ||||
|     } | ||||
| 
 | ||||
|     int pocet_tovarov = pocet_listov(databaza_znalosti); | ||||
|     printf("Expert z bufetu to vie.\n"); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", pocet_tovarov); | ||||
|     printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||
|     // Count leaf nodes
 | ||||
|     int count = count_leaf_nodes(root); | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", count); | ||||
| 
 | ||||
|     spusti_expert_system(databaza_znalosti); | ||||
|     znic_strom(databaza_znalosti); | ||||
|     // Run the dialogue system
 | ||||
|     run_dialogue(root); | ||||
| 
 | ||||
|     // Free allocated memory
 | ||||
|     destroy_tree(root); | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user