usaa21/cv8/program.c

95 lines
2.0 KiB
C
Raw Normal View History

2021-11-26 02:15:06 +00:00
//Valer Jakubco
2021-11-25 01:36:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define SIZE 100
2021-11-26 01:37:17 +00:00
2021-11-25 21:58:01 +00:00
struct node {
2021-11-25 01:36:11 +00:00
// Otázka aleo odpoveď
2021-11-25 21:58:01 +00:00
char data[SIZE];
2021-11-26 01:53:02 +00:00
struct node* parent;
2021-11-25 01:36:11 +00:00
// Odpoveď áno
2021-11-25 21:58:01 +00:00
struct node* left;
2021-11-25 01:36:11 +00:00
// Odpoveď nie
2021-11-25 21:58:01 +00:00
struct node* right;
};
2021-11-25 01:36:11 +00:00
2021-11-26 01:53:02 +00:00
struct node* read_tree(int* count, int* leafs){
char buffer[SIZE];
memset(buffer,0,SIZE);
char* r = fgets(buffer,SIZE,stdin);
assert(r);
struct node* node = calloc(1,sizeof(struct node));
memcpy(node->data,buffer,SIZE);
if(r[0]!='*'){
*count = *count +1;
node->left=read_tree(count, leafs);
node->right=read_tree(count, leafs);
}else {
*leafs = *leafs+1;
}
return node;
2021-11-25 01:36:11 +00:00
}
2021-11-26 01:53:02 +00:00
void print_tree(struct node* tree,int offset){
for (int i = 0; i < offset; i++){
printf(" ");
2021-11-25 21:58:01 +00:00
}
2021-11-26 01:53:02 +00:00
printf("%s",tree->data);
if (tree->left){
print_tree(tree->left,offset +3);
print_tree(tree->right,offset +3);
2021-11-25 21:58:01 +00:00
}
2021-11-25 01:36:11 +00:00
}
2021-11-25 21:58:01 +00:00
void remove_tree(struct node* node){
2021-11-25 01:36:11 +00:00
if ( node != NULL ){
2021-11-25 21:58:01 +00:00
remove_tree(node->left);
remove_tree(node->right);
free(node);
}
}
2021-11-26 02:09:22 +00:00
2021-11-26 01:53:02 +00:00
void ask(struct node* tree){
printf("%s", tree->data);
if(tree->data[0] == '*'){
puts("Koniec");
2021-11-26 02:00:27 +00:00
return;
2021-11-26 01:37:17 +00:00
}
2021-11-26 02:00:27 +00:00
char buff[SIZE];
char* r = fgets(buff, SIZE, stdin);
2021-11-26 02:03:26 +00:00
if(r == NULL){
puts("Koniec vstupu");
return;
}
2021-11-26 02:00:27 +00:00
r = fgets(buff, SIZE, stdin);
if(r[0] == 'a' && tree->left != NULL){
2021-11-26 01:53:02 +00:00
tree = tree->left;
ask(tree);
2021-11-26 02:00:27 +00:00
}else if(r[0] == 'n' && tree->right != NULL) {
2021-11-26 01:53:02 +00:00
tree = tree->right;
ask(tree);
}else{
2021-11-26 02:01:36 +00:00
puts("Nerozumiem");
2021-11-26 01:37:17 +00:00
return;
}
}
2021-11-25 01:36:11 +00:00
int main(){
2021-11-26 01:53:02 +00:00
struct node* tree;
int count = 0;
int leafs = 0;
tree = read_tree(&count, &leafs);
//print_tree(tree, 20);
2021-11-26 01:55:19 +00:00
puts("Expert z bufetu to vie.");
printf("Pozna %d druhov ovocia a zeleniny.\n",leafs);
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
2021-11-26 01:53:02 +00:00
ask(tree);
remove_tree(tree);
2021-11-26 01:37:17 +00:00
2021-11-25 01:36:11 +00:00
return 0;
}