second commit
This commit is contained in:
parent
b8fa664a33
commit
07e5f2eb4a
123
cv8/program.c
123
cv8/program.c
@ -4,78 +4,91 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#define SIZE 100
|
#define SIZE 100
|
||||||
|
|
||||||
typedef struct tree {
|
struct node {
|
||||||
// Otázka aleo odpoveď
|
// Otázka aleo odpoveď
|
||||||
char value[SIZE];
|
char data[SIZE];
|
||||||
// Odpoveď áno
|
// Odpoveď áno
|
||||||
struct tree* left;
|
struct node* left;
|
||||||
// Odpoveď nie
|
// Odpoveď nie
|
||||||
struct tree* right;
|
struct node* right;
|
||||||
} tree;
|
};
|
||||||
|
|
||||||
void print_tree(struct tree* node){
|
|
||||||
if( node == NULL){
|
|
||||||
printf("empty");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
printf("hodnota: %s\n",node->value);
|
|
||||||
printf("left ");
|
|
||||||
printf("%s\n",node->left->value);
|
|
||||||
printf("right ");
|
|
||||||
printf("%s\n",node->right->value);
|
|
||||||
|
|
||||||
|
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 tree* read_tree(tree* node){
|
|
||||||
char buffer[SIZE];
|
struct node* insert(struct node* root, char data[SIZE]){
|
||||||
memset(buffer,0, SIZE);
|
if(root == NULL){
|
||||||
char* r = fgets(buffer, SIZE, stdin);
|
root = getNewNode(data);
|
||||||
if( r == NULL ){
|
return root;
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
memcpy(node->value, buffer, SIZE);
|
|
||||||
r = fgets(buffer, SIZE, stdin);
|
if(root->left == NULL){
|
||||||
if( r == NULL ){
|
root->left = insert(root->left,data);
|
||||||
node->left = NULL;
|
}else{
|
||||||
return node;
|
root->right = insert(root->right,data);
|
||||||
}
|
}
|
||||||
node->left = calloc(1, sizeof(struct tree));
|
return root;
|
||||||
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 node* node){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
/*void remove_tree(struct tree* node){
|
|
||||||
if ( node != NULL ){
|
if ( node != NULL ){
|
||||||
|
|
||||||
if( node->left != NULL){
|
|
||||||
remove_tree(node->left);
|
remove_tree(node->left);
|
||||||
free(node);
|
|
||||||
}
|
|
||||||
if( node->right != NULL){
|
|
||||||
remove_tree(node->right);
|
remove_tree(node->right);
|
||||||
free(node);
|
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(){
|
int main(){
|
||||||
tree* node = calloc(1, sizeof(struct tree));
|
struct node* tree = NULL;
|
||||||
read_tree(node);
|
char buff[SIZE];
|
||||||
//remove_tree(node);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user