second commit
This commit is contained in:
parent
b8fa664a33
commit
07e5f2eb4a
133
cv8/program.c
133
cv8/program.c
@ -4,78 +4,91 @@
|
||||
#include <assert.h>
|
||||
#define SIZE 100
|
||||
|
||||
typedef struct tree {
|
||||
struct node {
|
||||
// Otázka aleo odpoveď
|
||||
char value[SIZE];
|
||||
char data[SIZE];
|
||||
// Odpoveď áno
|
||||
struct tree* left;
|
||||
struct node* left;
|
||||
// Odpoveď nie
|
||||
struct tree* right;
|
||||
} tree;
|
||||
struct node* right;
|
||||
};
|
||||
|
||||
void print_tree(struct tree* node){
|
||||
if( node == NULL){
|
||||
printf("empty");
|
||||
return;
|
||||
struct node* getNewNode(char data[SIZE]){
|
||||
struct node* newNode = (struct node*)calloc(1, sizeof(struct node));
|
||||
memcpy(newNode->data, data, SIZE);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
|
||||
struct node* insert(struct node* root, char data[SIZE]){
|
||||
if(root == NULL){
|
||||
root = getNewNode(data);
|
||||
return root;
|
||||
}
|
||||
printf("hodnota: %s\n",node->value);
|
||||
printf("left ");
|
||||
printf("%s\n",node->left->value);
|
||||
printf("right ");
|
||||
printf("%s\n",node->right->value);
|
||||
|
||||
if(root->left == NULL){
|
||||
root->left = insert(root->left,data);
|
||||
}else{
|
||||
root->right = insert(root->right,data);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
struct tree* read_tree(tree* node){
|
||||
char buffer[SIZE];
|
||||
memset(buffer,0, SIZE);
|
||||
char* r = fgets(buffer, SIZE, stdin);
|
||||
if( r == NULL ){
|
||||
return node;
|
||||
}
|
||||
memcpy(node->value, buffer, SIZE);
|
||||
r = fgets(buffer, SIZE, stdin);
|
||||
if( r == NULL ){
|
||||
node->left = NULL;
|
||||
return node;
|
||||
}
|
||||
node->left = calloc(1, sizeof(struct tree));
|
||||
memcpy(node->left->value, buffer, SIZE);
|
||||
r = fgets(buffer, SIZE, stdin);
|
||||
if( r == NULL ){
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
node->right = calloc(1, sizeof(struct tree));
|
||||
memcpy(node->right->value, buffer, SIZE);
|
||||
read_tree(node);
|
||||
print_tree(node);
|
||||
free(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
void guess(tree* node){
|
||||
|
||||
|
||||
|
||||
}
|
||||
/*void remove_tree(struct tree* node){
|
||||
void remove_tree(struct node* node){
|
||||
if ( node != NULL ){
|
||||
remove_tree(node->left);
|
||||
remove_tree(node->right);
|
||||
free(node);
|
||||
|
||||
if( node->left != NULL){
|
||||
remove_tree(node->left);
|
||||
free(node);
|
||||
}
|
||||
if( node->right != NULL){
|
||||
remove_tree(node->right);
|
||||
free(node);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
void printt_tree(struct node* tree,int offset){
|
||||
for (int i = 0; i < offset; i++){
|
||||
printf(" ");
|
||||
}
|
||||
printf("%s",tree->data);
|
||||
if (tree->left){
|
||||
printt_tree(tree->left,offset+3);
|
||||
printt_tree(tree->right,offset+3);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
tree* node = calloc(1, sizeof(struct tree));
|
||||
read_tree(node);
|
||||
//remove_tree(node);
|
||||
struct node* tree = NULL;
|
||||
char buff[SIZE];
|
||||
char* r;
|
||||
int counter=0;
|
||||
int lists=0;
|
||||
while(1){
|
||||
r = fgets(buff, SIZE, stdin);
|
||||
if(r == NULL){
|
||||
break;
|
||||
}
|
||||
if(strncmp(buff, "*", 1)) lists++;
|
||||
tree = insert(tree, buff);
|
||||
counter++;
|
||||
}
|
||||
puts("Expert z bufetu to vie.");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", lists);
|
||||
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
|
||||
for(int i=0; i< counter; i++){
|
||||
printf("%s", tree->data);
|
||||
r = fgets(buff, SIZE, stdin);
|
||||
if(strncmp(buff, "a", 1) && tree->right != NULL){
|
||||
tree = tree->right;
|
||||
continue;
|
||||
}else if(strncmp(buff, "n", 1) && tree->left != NULL){
|
||||
tree = tree->left;
|
||||
continue;
|
||||
}else {
|
||||
puts("Koniec");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//printt_tree(tree, 1);
|
||||
remove_tree(tree);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user