usaa24/cv7/program.c

104 lines
2.6 KiB
C
Raw Normal View History

2024-11-11 13:45:35 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-11-14 20:53:26 +00:00
#include <assert.h>
2024-11-11 13:45:35 +00:00
2024-11-14 20:53:26 +00:00
#define SIZE 100
#define YES 'a'
#define NO 'n'
2024-11-14 13:04:50 +00:00
2024-11-14 20:53:26 +00:00
// Tree node structure
struct TreeNode {
char value[SIZE];
struct TreeNode* yes;
struct TreeNode* no;
2024-11-14 13:04:50 +00:00
};
2024-11-14 20:53:26 +00:00
// Function to create a new tree node
struct TreeNode* create_node(const char* text) {
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
strcpy(node->value, text);
node->yes = node->no = NULL;
return node;
}
// Recursive function to build the tree
struct TreeNode* read_tree() {
2024-11-14 13:04:50 +00:00
char buffer[SIZE];
2024-11-14 20:53:26 +00:00
if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') return NULL;
2024-11-11 13:45:35 +00:00
2024-11-14 20:53:26 +00:00
// Remove newline character
buffer[strcspn(buffer, "\n")] = 0;
2024-11-11 13:45:35 +00:00
2024-11-14 20:53:26 +00:00
struct TreeNode* node = create_node(buffer);
// Check if it's a leaf node
if (buffer[0] != '*') {
node->yes = read_tree(); // Read 'yes' branch
node->no = read_tree(); // Read 'no' branch
2024-11-11 13:45:35 +00:00
}
2024-11-14 20:53:26 +00:00
return node;
2024-11-14 13:04:50 +00:00
}
2024-11-13 22:44:04 +00:00
2024-11-14 20:53:26 +00:00
// Count the leaf nodes (answers) in the tree
int count_leaf_nodes(struct TreeNode* node) {
if (node == NULL) return 0;
if (node->yes == NULL && node->no == NULL) return 1; // It's a leaf node
return count_leaf_nodes(node->yes) + count_leaf_nodes(node->no);
2024-11-11 13:45:35 +00:00
}
2024-11-14 20:53:26 +00:00
// Free memory allocated for the tree
void destroy_tree(struct TreeNode* node) {
if (node == NULL) return;
destroy_tree(node->yes);
destroy_tree(node->no);
free(node);
}
2024-11-13 22:44:04 +00:00
2024-11-14 20:53:26 +00:00
// Dialogue with user based on tree structure
void run_dialogue(struct TreeNode* node) {
if (!node) return;
printf("%s\n", node->value);
2024-11-11 13:45:35 +00:00
2024-11-14 20:53:26 +00:00
if (node->yes == NULL && node->no == NULL) {
printf("Expert z bufetu to vie.\n");
2024-11-13 22:48:03 +00:00
return;
2024-11-14 13:15:31 +00:00
}
2024-11-13 22:44:04 +00:00
2024-11-14 20:53:26 +00:00
char answer;
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
if (scanf(" %c", &answer) != 1 || (answer != YES && answer != NO)) {
printf("Odpovedajte 'a' alebo 'n'.\n");
return;
2024-11-11 13:45:35 +00:00
}
2024-11-14 20:53:26 +00:00
if (answer == YES) run_dialogue(node->yes);
else if (answer == NO) run_dialogue(node->no);
2024-11-13 22:44:04 +00:00
}
2024-11-13 21:28:02 +00:00
2024-11-11 13:45:35 +00:00
int main() {
2024-11-14 20:53:26 +00:00
// Read and build tree from input
struct TreeNode* root = read_tree();
// Check if rule base ended with an empty line
char check[SIZE];
if (!fgets(check, SIZE, stdin) || check[0] != '\n') {
printf("Neplatna baza pravidiel.\n");
destroy_tree(root);
2024-11-13 22:44:04 +00:00
return 1;
2024-11-13 21:17:06 +00:00
}
2024-11-11 13:45:35 +00:00
2024-11-14 20:53:26 +00:00
// Count leaf nodes
int count = count_leaf_nodes(root);
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
2024-11-14 13:04:50 +00:00
2024-11-14 20:53:26 +00:00
// Run the dialogue system
run_dialogue(root);
2024-11-13 22:44:04 +00:00
2024-11-14 20:53:26 +00:00
// Free allocated memory
destroy_tree(root);
2024-11-13 22:44:04 +00:00
return 0;
}