cv7
This commit is contained in:
parent
ae9d9a9a08
commit
0b7175626f
@ -2,6 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define SIZE 100
|
||||
|
||||
struct tree {
|
||||
@ -10,26 +11,26 @@ struct tree {
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
struct tree* read_tree(int* counter){
|
||||
char buffer[SIZE];
|
||||
memset(buffer, 0, SIZE);
|
||||
char* r = fgets(buffer, SIZE, stdin);
|
||||
if(buffer[0] == '\n'){
|
||||
return NULL;
|
||||
struct tree* read_tree(int* counter) {
|
||||
char buffer[SIZE];
|
||||
memset(buffer, 0, SIZE);
|
||||
char* r = fgets(buffer, SIZE, stdin);
|
||||
if (buffer[0] == '\n') {
|
||||
return NULL;
|
||||
}
|
||||
assert(r);
|
||||
struct tree* tree = calloc(1, sizeof(struct tree));
|
||||
assert(r);
|
||||
struct tree* tree = calloc(1, sizeof(struct tree));
|
||||
tree->value = malloc(strlen(buffer) + 1);
|
||||
strcpy(tree->value,r);
|
||||
assert(tree->value);
|
||||
strcpy(tree->value, buffer);
|
||||
assert(tree->value);
|
||||
if (buffer[0] == '*') {
|
||||
(*counter)++;
|
||||
return tree;
|
||||
}
|
||||
tree->left = read_tree(counter);
|
||||
tree->right = read_tree(counter);
|
||||
return tree;
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* tree) {
|
||||
if (tree == NULL) return;
|
||||
@ -39,39 +40,37 @@ void destroy_tree(struct tree* tree) {
|
||||
free(tree);
|
||||
}
|
||||
|
||||
void print_tree(struct tree* tree,int offset){
|
||||
char buffer[SIZE];
|
||||
memset(buffer, 0, SIZE);
|
||||
for (int i = 0; i < offset && tree->value[0] != '*'; i++){
|
||||
void print_tree(struct tree* tree, int offset) {
|
||||
if (tree == NULL) return;
|
||||
for (int i = 0; i < offset && tree->value[0] != '*'; i++) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("%s",tree->value);
|
||||
if(tree->value[0] == '*'){
|
||||
printf("Koniec\n");
|
||||
exit(0);
|
||||
printf("%s", tree->value);
|
||||
if (tree->value[0] == '*') {
|
||||
printf("Koniec vstupu\n");
|
||||
return;
|
||||
}
|
||||
getchar();
|
||||
char r = getchar();
|
||||
if (r == 'a') {
|
||||
print_tree(tree->left, offset + 3);
|
||||
} else if (r == 'n') {
|
||||
print_tree(tree->right, offset + 3);
|
||||
} else if (r == '\n'){
|
||||
} else if (r == '\n') {
|
||||
printf("Koniec vstupu\n");
|
||||
}else {
|
||||
} else {
|
||||
printf("Nerozumiem\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int main() {
|
||||
int counter = 0;
|
||||
int offset = 0;
|
||||
struct tree* root = read_tree(&counter);
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n",counter);
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", counter);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
print_tree(root,offset);
|
||||
print_tree(root, offset);
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user