Update cv7/program.c
This commit is contained in:
		
							parent
							
								
									62eb405625
								
							
						
					
					
						commit
						36d426b4e2
					
				
							
								
								
									
										146
									
								
								cv7/program.c
									
									
									
									
									
								
							
							
						
						
									
										146
									
								
								cv7/program.c
									
									
									
									
									
								
							| @ -2,99 +2,89 @@ | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| typedef struct Node { | ||||
|     char text[100]; | ||||
|     struct Node* yes; | ||||
|     struct Node* no; | ||||
| } Node; | ||||
| #define SIZE 256 | ||||
| 
 | ||||
| // Function to create a new node
 | ||||
| Node* createNode(char* text) { | ||||
|     Node* node = (Node*)malloc(sizeof(Node)); | ||||
|     if (!node) { | ||||
|         fprintf(stderr, "Chyba pri alokacii pamate.\n"); | ||||
|         exit(1); | ||||
|     } | ||||
|     strcpy(node->text, text); | ||||
|     node->yes = NULL; | ||||
|     node->no = NULL; | ||||
|     return node; | ||||
| } | ||||
| // Štruktúra uzla binárneho stromu
 | ||||
| struct strom { | ||||
|     char otazka[SIZE]; | ||||
|     struct strom* ano; | ||||
|     struct strom* nie; | ||||
| }; | ||||
| 
 | ||||
| // Recursive function to read and build the tree
 | ||||
| Node* readTree(int* count) { | ||||
|     char line[100]; | ||||
| 
 | ||||
|     if (!fgets(line, sizeof(line), stdin) || strcmp(line, "\n") == 0) { | ||||
|         return NULL; | ||||
| // Funkcia na načítanie stromu zo štandardného vstupu
 | ||||
| struct strom* nacitaj_strom() { | ||||
|     char buffer[SIZE]; | ||||
|     if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') { | ||||
|         return NULL;  // Koniec databázy alebo prázdny riadok
 | ||||
|     } | ||||
| 
 | ||||
|     // Remove newline character from line
 | ||||
|     line[strcspn(line, "\n")] = 0; | ||||
|     struct strom* uzol = malloc(sizeof(struct strom)); | ||||
|     strcpy(uzol->otazka, buffer); | ||||
| 
 | ||||
|     // Check if the line is an answer (starts with '*')
 | ||||
|     if (line[0] == '*') { | ||||
|         (*count)++; | ||||
|         return createNode(line + 1);  // Skip the '*' character
 | ||||
|     } | ||||
| 
 | ||||
|     // If it's a question, create a node and recursively read its children
 | ||||
|     Node* node = createNode(line); | ||||
|     node->yes = readTree(count); | ||||
|     node->no = readTree(count); | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| // Function to interact with the user and navigate the tree
 | ||||
| void queryUser(Node* node) { | ||||
|     if (node == NULL) return; | ||||
| 
 | ||||
|     if (node->yes == NULL && node->no == NULL) { | ||||
|         printf("Expert z bufetu to vie.\n"); | ||||
|         printf("Je to: %s\n", node->text); | ||||
|         printf("Koniec\n"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     // Print question and get user's answer
 | ||||
|     printf("%s\n", node->text); | ||||
|     printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); | ||||
| 
 | ||||
|     char answer; | ||||
|     if (scanf(" %c", &answer) != 1) { | ||||
|         printf("Nespravny vstup.\n"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     if (answer == 'a') { | ||||
|         queryUser(node->yes); | ||||
|     } else if (answer == 'n') { | ||||
|         queryUser(node->no); | ||||
|     if (buffer[0] == '*') { | ||||
|         uzol->ano = uzol->nie = NULL;  // Tento uzol je list (odpoveď)
 | ||||
|     } else { | ||||
|         printf("Nespravny vstup.\n"); | ||||
|         uzol->ano = nacitaj_strom();  // Rekurzívne načítanie vetvy pre "áno"
 | ||||
|         uzol->nie = nacitaj_strom();  // Rekurzívne načítanie vetvy pre "nie"
 | ||||
|     } | ||||
| 
 | ||||
|     return uzol; | ||||
| } | ||||
| 
 | ||||
| // Funkcia na uvoľnenie pamäte stromu
 | ||||
| void znic_strom(struct strom* uzol) { | ||||
|     if (uzol) { | ||||
|         znic_strom(uzol->ano); | ||||
|         znic_strom(uzol->nie); | ||||
|         free(uzol); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Function to free the tree
 | ||||
| void freeTree(Node* node) { | ||||
|     if (node == NULL) return; | ||||
|     freeTree(node->yes); | ||||
|     freeTree(node->no); | ||||
|     free(node); | ||||
| // Funkcia na interakciu s používateľom (prechádzanie stromu)
 | ||||
| void spusti_expert_system(struct strom* uzol) { | ||||
|     if (!uzol) return; | ||||
| 
 | ||||
|     printf("%s", uzol->otazka); | ||||
|     if (!uzol->ano && !uzol->nie) { | ||||
|         printf("Koniec\n");  // Konečná odpoveď
 | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     char odpoved; | ||||
|     printf(" (a/n): "); | ||||
|     if (scanf(" %c", &odpoved) != 1 || (odpoved != 'a' && odpoved != 'n')) { | ||||
|         printf("Očakávala sa odpoveď 'a' alebo 'n'.\n"); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     if (odpoved == 'a') { | ||||
|         spusti_expert_system(uzol->ano); | ||||
|     } else { | ||||
|         spusti_expert_system(uzol->nie); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     int count = 0; | ||||
|     Node* root = readTree(&count); | ||||
| // Funkcia na spočítanie listov stromu (konečných odpovedí)
 | ||||
| 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); | ||||
| } | ||||
| 
 | ||||
|     if (root == NULL) { | ||||
|         printf("Chyba pri nacitani baze pravidiel.\n"); | ||||
| // Hlavná funkcia
 | ||||
| int main() { | ||||
|     struct strom* databaza_znalosti = nacitaj_strom(); | ||||
| 
 | ||||
|     if (!databaza_znalosti) { | ||||
|         printf("Chyba: databáza je prázdna alebo nesprávne formátovaná.\n"); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     printf("Pozna %d druhov ovocia a zeleniny.\n", count); | ||||
|     queryUser(root); | ||||
|     freeTree(root); | ||||
|     int pocet_tovarov = pocet_listov(databaza_znalosti); | ||||
|     printf("Databáza obsahuje %d tovarov.\n", pocet_tovarov); | ||||
| 
 | ||||
|     spusti_expert_system(databaza_znalosti); | ||||
|     znic_strom(databaza_znalosti); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user