du5
This commit is contained in:
parent
31e4831594
commit
b2208b5cad
@ -20,7 +20,7 @@ Tree* readTree() {
|
||||
|
||||
if (line[0] == '\0') return NULL;
|
||||
|
||||
Tree *node = malloc(sizeof(Tree));
|
||||
Tree *node = (Tree*)malloc(sizeof(Tree));
|
||||
node->yes = NULL;
|
||||
node->no = NULL;
|
||||
|
||||
@ -41,18 +41,10 @@ Tree* readTree() {
|
||||
return node;
|
||||
}
|
||||
|
||||
int EmptyLine() {
|
||||
char line[SIZE];
|
||||
|
||||
if (fgets(line, SIZE, stdin) == NULL) return 0;
|
||||
|
||||
return (line[0] == '\n' || line[0] == '\r');
|
||||
}
|
||||
|
||||
int Leaves(Tree *node) {
|
||||
int countLeaves(Tree *node) {
|
||||
if (!node) return 0;
|
||||
if (node->isAnswer) return 1;
|
||||
return Leaves(node->yes) + Leaves(node->no);
|
||||
return countLeaves(node->yes) + countLeaves(node->no);
|
||||
}
|
||||
|
||||
void freeTree(Tree *node) {
|
||||
@ -68,20 +60,19 @@ void start(Tree *node) {
|
||||
while (node && !node->isAnswer) {
|
||||
printf("%s\n", node->text);
|
||||
|
||||
if (fgets(input, SIZE, stdin) == NULL) {
|
||||
printf("Koniec vstupu\n");
|
||||
return;
|
||||
}
|
||||
if (fgets(input, SIZE, stdin) == NULL) return;
|
||||
|
||||
int i = 0;
|
||||
while (input[i] == ' ' || input[i] == '\t' || input[i] == '\r') i++;
|
||||
while (input[i] == ' ' || input[i] == '\t') i++;
|
||||
|
||||
if (input[i] == 'a') node = node->yes;
|
||||
else node = node->no;
|
||||
if (input[i] == 'a')
|
||||
node = node->yes;
|
||||
else
|
||||
node = node->no;
|
||||
}
|
||||
|
||||
if (node && node->isAnswer) {
|
||||
printf("*%s\n", node->text);
|
||||
printf("* %s\n", node->text);
|
||||
printf("Koniec\n");
|
||||
}
|
||||
}
|
||||
@ -96,13 +87,7 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!EmptyLine()) {
|
||||
printf("Chyba nacitania\n");
|
||||
freeTree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", Leaves(root));
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", countLeaves(root));
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
start(root);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user