usaa24/cv7/program.c

110 lines
2.4 KiB
C
Raw Permalink Normal View History

2024-11-11 13:45:35 +00:00
#include <stdio.h>
2024-11-14 21:05:03 +00:00
#include <stdlib.h>
2024-11-11 13:45:35 +00:00
#include <string.h>
2024-11-14 23:56:29 +00:00
#define MAX_SIZE 256
2024-11-14 21:05:03 +00:00
2024-11-14 23:56:29 +00:00
struct node {
char value[MAX_SIZE];
struct node *left;
struct node *right;
int id;
2024-11-14 21:05:03 +00:00
};
2024-11-14 23:56:29 +00:00
struct node* read_tree(int *id_counter) {
char line[MAX_SIZE];
if (fgets(line, MAX_SIZE, stdin) == NULL || line[0] == '\n') {
return NULL;
2024-11-14 21:05:03 +00:00
}
2024-11-14 23:56:29 +00:00
struct node *new_node = (struct node*)malloc(sizeof(struct node));
if (new_node == NULL) {
return NULL;
2024-11-14 21:05:03 +00:00
}
2024-11-14 23:56:29 +00:00
strcpy(new_node->value, line);
new_node->id = (*id_counter)++;
if (line[0] != '*') {
new_node->left = read_tree(id_counter);
new_node->right = read_tree(id_counter);
2024-11-14 21:05:03 +00:00
}
2024-11-14 23:56:29 +00:00
return new_node;
2024-11-14 20:53:26 +00:00
}
2024-11-14 23:56:29 +00:00
void free_tree(struct node *root) {
if (root == NULL) return;
free_tree(root->left);
free_tree(root->right);
free(root);
2024-11-11 13:45:35 +00:00
}
2024-11-14 23:56:29 +00:00
int count_items(struct node *root) {
if (root == NULL) return 0;
if (root->left == NULL && root->right == NULL) return 1;
return count_items(root->left) + count_items(root->right);
}
2024-11-14 21:05:03 +00:00
2024-11-14 23:56:29 +00:00
void start_system(struct node *current_node) {
if (current_node == NULL) return;
int blank_lines = 0;
int is_first_prompt = 1;
while (current_node != NULL) {
if (current_node->left == NULL && current_node->right == NULL) {
printf("%s", current_node->value);
printf("Koniec\n");
return;
}
if (is_first_prompt) {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
is_first_prompt = 0;
}
2024-11-13 21:28:02 +00:00
2024-11-14 23:56:29 +00:00
printf("%s", current_node->value);
char response;
int result = scanf(" %c", &response);
if (result == EOF || (response == '\n' && ++blank_lines >= 2)) {
printf("Koniec vstupu\n");
return;
}
if (response == 'a') {
blank_lines = 0;
current_node = current_node->left;
} else if (response == 'n') {
blank_lines = 0;
current_node = current_node->right;
2024-11-14 21:13:00 +00:00
} else {
2024-11-14 23:56:29 +00:00
printf("Nerozumiem\n");
return;
2024-11-14 21:13:00 +00:00
}
2024-11-14 21:05:03 +00:00
}
}
2024-11-14 20:57:04 +00:00
2024-11-14 21:05:03 +00:00
int main() {
2024-11-14 23:56:29 +00:00
int counter = 0;
struct node *root = read_tree(&counter);
printf("Expert z bufetu to vie.\n");
2024-11-14 20:57:04 +00:00
2024-11-14 23:56:29 +00:00
if (root == NULL) {
printf("Chybna databaza\n");
return 0;
2024-11-14 21:05:03 +00:00
}
2024-11-11 13:45:35 +00:00
2024-11-14 23:56:29 +00:00
int item_count = count_items(root);
printf("Pozna %d druhov ovocia a zeleniny.\n", item_count);
2024-11-14 13:04:50 +00:00
2024-11-14 23:56:29 +00:00
start_system(root);
free_tree(root);
2024-11-13 22:44:04 +00:00
return 0;
2024-11-14 21:13:00 +00:00
}