usaa20/cv8/program.c

125 lines
2.7 KiB
C
Raw Normal View History

2020-11-21 15:26:27 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
struct tree {
2020-11-22 11:54:57 +00:00
char value[50];
2020-11-21 15:26:27 +00:00
struct tree* left;
struct tree* right;
};
struct tree* read_tree(){
2020-11-22 11:54:57 +00:00
char buffer[50];
memset(buffer,0,50);
char* r = fgets(buffer,50,stdin);
2020-11-21 15:26:27 +00:00
int x = strlen(buffer);
buffer[x-1]='\0';
2020-11-21 17:34:29 +00:00
2020-11-21 15:26:27 +00:00
assert(r);
2020-11-21 17:34:29 +00:00
2020-11-21 15:26:27 +00:00
struct tree* node = calloc(1,sizeof(struct tree));
2020-11-21 15:34:20 +00:00
if(buffer[0] == '\0'){
return node;
}
2020-11-22 11:54:57 +00:00
memcpy(node->value, buffer,50);
2020-11-21 15:26:27 +00:00
if(buffer[0] != '*'){
2020-11-21 16:00:44 +00:00
if(node->left == NULL){
2020-11-21 15:59:00 +00:00
node->left = read_tree();
2020-11-21 16:00:44 +00:00
}
if(node->right == NULL){
2020-11-21 15:59:00 +00:00
node->right = read_tree();
2020-11-21 16:00:44 +00:00
}
2020-11-21 15:26:27 +00:00
}
2020-11-21 16:00:44 +00:00
//if(node->left != NULL && node->right != NULL){
2020-11-21 15:26:27 +00:00
return node;
2020-11-21 17:34:29 +00:00
// }
2020-11-21 15:26:27 +00:00
}
2020-11-22 11:54:57 +00:00
struct tree* search(struct tree* this){
char question[30] = "Je to ovocie alebo zelenina";
if(strcmp(this->value,question) == 0){
printf("%s\n", this->value);
this = search(this->left);
2020-11-21 15:26:27 +00:00
}
2020-11-22 11:54:57 +00:00
char c = getchar();
char buffer[50];
buffer[strlen(buffer)-1]='\0';
2020-11-21 15:26:27 +00:00
2020-11-22 11:54:57 +00:00
memcpy(buffer, this->value, 50);
//printf("%s\n", buffer);
if(buffer[0] != '*'){
if(c == 'a'){
printf("%s\n", this->value);
this->left = search(this->left);
}
else if(c == 'n'){
printf("%s\n", this->value);
this->right = search(this->right);
}
2020-11-21 17:34:29 +00:00
2020-11-21 15:26:27 +00:00
}
2020-11-22 11:54:57 +00:00
else if(buffer[0] == '*'){
2020-11-22 10:58:56 +00:00
printf("%s\n", this->value);
2020-11-22 11:54:57 +00:00
printf("Koniec\n");
exit(0);
}
}
2020-11-21 15:26:27 +00:00
void destroy_tree(struct tree* root){
if(root == NULL) return;
destroy_tree(root->left);
destroy_tree(root->right);
free(root);
root = NULL;
2020-11-21 17:34:29 +00:00
}
int count_leaves(struct tree* node){
int count = 0;
if(node->left == NULL && node->right == NULL){
count = 1;
}
else{
if(node->left != NULL){
count += count_leaves(node->left);
}
if(node->right != NULL){
count += count_leaves(node->right);
}
}
return count;
}
int count_all(struct tree* node){
2020-11-21 15:26:27 +00:00
if(node == NULL){
return 0;
}
2020-11-21 17:34:29 +00:00
int count = 1;
if(node->left != NULL){
count += count_all(node->left);
2020-11-21 15:26:27 +00:00
}
2020-11-21 17:34:29 +00:00
if(node->right != NULL){
count += count_all(node->right);
2020-11-21 15:26:27 +00:00
}
2020-11-21 17:34:29 +00:00
return count;
2020-11-21 15:26:27 +00:00
}
int main(){
struct tree* tree = NULL;
tree = read_tree();
2020-11-21 17:34:29 +00:00
int count = count_leaves(tree);
2020-11-22 10:58:56 +00:00
2020-11-22 11:56:13 +00:00
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
2020-11-21 15:32:04 +00:00
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
2020-11-22 11:54:57 +00:00
2020-11-21 17:35:27 +00:00
tree = search(tree);
2020-11-21 16:04:17 +00:00
2020-11-21 15:26:27 +00:00
return 0;
}