refresh
This commit is contained in:
parent
c5cde60d85
commit
54bffb445c
130
du6/program.c
Normal file
130
du6/program.c
Normal file
@ -0,0 +1,130 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAXLINE 256
|
||||
|
||||
typedef struct TreeNode {
|
||||
char text[MAXLINE];
|
||||
struct TreeNode *yes_branch;
|
||||
struct TreeNode *no_branch;
|
||||
} TreeNode;
|
||||
|
||||
static TreeNode* read_tree_node() {
|
||||
char buffer[MAXLINE];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
char *r = fgets(buffer, sizeof(buffer), stdin);
|
||||
if (r == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (buffer[0] == '\n' || buffer[0] == '\r') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TreeNode *node = calloc(1, sizeof(TreeNode));
|
||||
if (node == NULL) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
strncpy(node->text, buffer, MAXLINE - 1);
|
||||
|
||||
if (node->text[0] == '*') {
|
||||
node->yes_branch = NULL;
|
||||
node->no_branch = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
node->yes_branch = read_tree_node();
|
||||
node->no_branch = read_tree_node();
|
||||
return node;
|
||||
}
|
||||
|
||||
static void free_tree(TreeNode *root) {
|
||||
if (root != NULL) {
|
||||
free_tree(root->yes_branch);
|
||||
free_tree(root->no_branch);
|
||||
free(root);
|
||||
}
|
||||
}
|
||||
|
||||
static int count_leafs(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int left_count = count_leafs(root->yes_branch);
|
||||
int right_count = count_leafs(root->no_branch);
|
||||
|
||||
int is_leaf = 0;
|
||||
if (root->yes_branch == NULL && root->no_branch == NULL) {
|
||||
is_leaf = 1;
|
||||
}
|
||||
|
||||
return left_count + right_count + is_leaf;
|
||||
}
|
||||
|
||||
static void skip_empty_line() {
|
||||
char temp[MAXLINE];
|
||||
long pos = ftell(stdin);
|
||||
|
||||
if (fgets(temp, sizeof(temp), stdin) == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(temp[0] == '\n' || temp[0] == '\r')) {
|
||||
fseek(stdin, pos, SEEK_SET);
|
||||
}
|
||||
}
|
||||
|
||||
static void run_dialog(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s", root->text);
|
||||
|
||||
if (root->yes_branch == NULL && root->no_branch == NULL) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char c = 0;
|
||||
if (scanf(" %c", &c) != 1) {
|
||||
printf("Odpovedajte 'a' alebo 'n'\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (c == 'a') {
|
||||
run_dialog(root->yes_branch);
|
||||
}
|
||||
else if (c == 'n') {
|
||||
run_dialog(root->no_branch);
|
||||
}
|
||||
else {
|
||||
printf("Odpovedajte 'a' alebo 'n'\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
TreeNode *root = read_tree_node();
|
||||
|
||||
skip_empty_line();
|
||||
|
||||
if (root == NULL) {
|
||||
printf("Chyba\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int total_types = count_leafs(root);
|
||||
|
||||
printf("MUDrC to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", total_types);
|
||||
|
||||
run_dialog(root);
|
||||
|
||||
free_tree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user