Initializacia
This commit is contained in:
		
							parent
							
								
									745926af82
								
							
						
					
					
						commit
						dd1da65481
					
				@ -10,31 +10,32 @@ struct tree {
 | 
			
		||||
    struct tree* right;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Function to read a single node from input
 | 
			
		||||
struct tree* read_tree() {
 | 
			
		||||
    char buffer[SIZE];
 | 
			
		||||
    memset(buffer, 0, SIZE);
 | 
			
		||||
    char* r = fgets(buffer, SIZE, stdin);
 | 
			
		||||
    if (!r || buffer[0] == '\n') {
 | 
			
		||||
    if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    buffer[strcspn(buffer, "\n")] = 0;
 | 
			
		||||
    buffer[strcspn(buffer, "\n")] = 0; // Remove newline character
 | 
			
		||||
    struct tree* node = calloc(1, sizeof(struct tree));
 | 
			
		||||
    strncpy(node->value, buffer, SIZE - 1);
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to load the tree structure from input
 | 
			
		||||
struct tree* load_tree() {
 | 
			
		||||
    struct tree* node = read_tree();
 | 
			
		||||
    if (!node) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    if (node->value[0] != '*') {
 | 
			
		||||
    if (node->value[0] != '*') { // Not an answer, so load children
 | 
			
		||||
        node->left = load_tree();
 | 
			
		||||
        node->right = load_tree();
 | 
			
		||||
    }
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to run the tree and interact with the user
 | 
			
		||||
void run_tree(struct tree* tree) {
 | 
			
		||||
    if (!tree) {
 | 
			
		||||
        return;
 | 
			
		||||
@ -47,6 +48,7 @@ void run_tree(struct tree* tree) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("%s\n", tree->value);
 | 
			
		||||
    printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
 | 
			
		||||
 | 
			
		||||
    char response;
 | 
			
		||||
    if (scanf(" %c", &response) != 1) {
 | 
			
		||||
@ -64,6 +66,7 @@ void run_tree(struct tree* tree) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to free the memory allocated for the tree
 | 
			
		||||
void destroy_tree(struct tree* tree) {
 | 
			
		||||
    if (tree) {
 | 
			
		||||
        destroy_tree(tree->left);
 | 
			
		||||
@ -72,10 +75,11 @@ void destroy_tree(struct tree* tree) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to count the number of unique items (answers) in the tree
 | 
			
		||||
void count_items(struct tree* tree, int* count) {
 | 
			
		||||
    if (tree) {
 | 
			
		||||
        if (tree->left == NULL && tree->right == NULL) {
 | 
			
		||||
            (*count)++;
 | 
			
		||||
            (*count)++; // It's a leaf node (an answer)
 | 
			
		||||
        } else {
 | 
			
		||||
            count_items(tree->left, count);
 | 
			
		||||
            count_items(tree->right, count);
 | 
			
		||||
@ -84,8 +88,9 @@ void count_items(struct tree* tree, int* count) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    struct tree* root = load_tree();
 | 
			
		||||
    printf("Expert z bufetu to vie.\n");
 | 
			
		||||
 | 
			
		||||
    struct tree* root = load_tree();
 | 
			
		||||
    if (!root) {
 | 
			
		||||
        printf("Chybna databaza\n");
 | 
			
		||||
        return 0;
 | 
			
		||||
@ -94,10 +99,8 @@ int main() {
 | 
			
		||||
    int count = 0;
 | 
			
		||||
    count_items(root, &count);
 | 
			
		||||
    printf("Pozna %d druhov ovocia a zeleniny.\n", count);
 | 
			
		||||
    printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
 | 
			
		||||
 | 
			
		||||
    run_tree(root);
 | 
			
		||||
 | 
			
		||||
    destroy_tree(root);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user