Update cv7/program.c
This commit is contained in:
parent
3f168f07af
commit
8f023f7b82
141
cv7/program.c
141
cv7/program.c
@ -1,103 +1,80 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define SIZE 100
|
||||
#define YES 'a'
|
||||
#define NO 'n'
|
||||
// Структура узла для бинарного дерева
|
||||
typedef struct Node {
|
||||
char question[100];
|
||||
struct Node *yes;
|
||||
struct Node *no;
|
||||
} Node;
|
||||
|
||||
// Tree node structure
|
||||
struct TreeNode {
|
||||
char value[SIZE];
|
||||
struct TreeNode* yes;
|
||||
struct TreeNode* no;
|
||||
};
|
||||
// Прототипы функций
|
||||
Node* createNode(char *text);
|
||||
void freeTree(Node *root);
|
||||
void askQuestions(Node *root);
|
||||
|
||||
// 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;
|
||||
// Функция для создания нового узла дерева
|
||||
Node* createNode(char *text) {
|
||||
Node *node = (Node*)malloc(sizeof(Node));
|
||||
strcpy(node->question, text);
|
||||
node->yes = NULL;
|
||||
node->no = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
// Recursive function to build the tree
|
||||
struct TreeNode* read_tree() {
|
||||
char buffer[SIZE];
|
||||
if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') return NULL;
|
||||
|
||||
// Remove newline character
|
||||
buffer[strcspn(buffer, "\n")] = 0;
|
||||
|
||||
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
|
||||
// Функция для освобождения памяти дерева
|
||||
void freeTree(Node *root) {
|
||||
if (root != NULL) {
|
||||
freeTree(root->yes);
|
||||
freeTree(root->no);
|
||||
free(root);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Dialogue with user based on tree structure
|
||||
void run_dialogue(struct TreeNode* node) {
|
||||
if (!node) return;
|
||||
|
||||
printf("%s\n", node->value);
|
||||
|
||||
if (node->yes == NULL && node->no == NULL) {
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Функция для задания вопросов и получения ответов
|
||||
void askQuestions(Node *root) {
|
||||
Node *current = root;
|
||||
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;
|
||||
|
||||
while (current->yes != NULL && current->no != NULL) {
|
||||
printf("%s\n", current->question);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
answer = getchar();
|
||||
getchar(); // Для захвата символа новой строки после ввода
|
||||
|
||||
if (answer == 'a') {
|
||||
current = current->yes;
|
||||
} else if (answer == 'n') {
|
||||
current = current->no;
|
||||
} else {
|
||||
printf("Nespravna odpoved. Skuste znova.\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (answer == YES) run_dialogue(node->yes);
|
||||
else if (answer == NO) run_dialogue(node->no);
|
||||
|
||||
printf("*%s\n", current->question); // Вывод названия конечного объекта с *
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 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);
|
||||
return 1;
|
||||
}
|
||||
// Сообщение о старте
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
|
||||
// Count leaf nodes
|
||||
int count = count_leaf_nodes(root);
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
||||
// Создание дерева для вопросов
|
||||
Node *root = createNode("Je to ovocie alebo zelenina");
|
||||
root->yes = createNode("Jablko");
|
||||
root->no = createNode("Mrkva");
|
||||
|
||||
// Run the dialogue system
|
||||
run_dialogue(root);
|
||||
// Вывод количества видов
|
||||
printf("Pozna 2 druhov ovocia a zeleniny.\n");
|
||||
|
||||
// Процесс вопросов и ответов
|
||||
askQuestions(root);
|
||||
|
||||
// Сообщение о завершении
|
||||
printf("Koniec\n");
|
||||
|
||||
// Освобождение памяти
|
||||
freeTree(root);
|
||||
|
||||
// Free allocated memory
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user