Update cv7/program.c

This commit is contained in:
Marat Izmailov 2024-11-13 22:44:04 +00:00
parent e3952af035
commit 1c188d677a

View File

@ -3,118 +3,96 @@
#include <string.h> #include <string.h>
typedef struct Node { typedef struct Node {
char *content; char text[100];
struct Node *yes; struct Node* yes;
struct Node *no; struct Node* no;
} Node; } Node;
Node* create_node(const char *content) { // Function to create a new node
Node *new_node = (Node*)malloc(sizeof(Node)); Node* createNode(char* text) {
new_node->content = strdup(content); Node* node = (Node*)malloc(sizeof(Node));
new_node->yes = NULL; strcpy(node->text, text);
new_node->no = NULL; node->yes = NULL;
return new_node; node->no = NULL;
return node;
} }
void free_tree(Node *root) { // Recursive function to read and build the tree
if (root) { Node* readTree(FILE* file, int* count) {
free(root->content); char line[100];
free_tree(root->yes);
free_tree(root->no);
free(root);
}
}
int count_answers(Node *root) { if (!fgets(line, sizeof(line), file) || strcmp(line, "\n") == 0) {
if (!root) return 0; return NULL;
if (root->yes == NULL && root->no == NULL) return 1;
return count_answers(root->yes) + count_answers(root->no);
}
Node* parse_rules() {
char line[256];
Node *root = NULL, *current = NULL;
Node *stack[100];
int stack_top = -1;
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\n")] = 0;
if (strlen(line) == 0) break;
if (line[0] == '*') {
Node *answer = create_node(line + 1);
if (stack_top >= 0) {
if (stack[stack_top]->yes == NULL)
stack[stack_top]->yes = answer;
else
stack[stack_top]->no = answer;
}
} else {
Node *question = create_node(line);
if (stack_top >= 0) {
if (stack[stack_top]->yes == NULL)
stack[stack_top]->yes = question;
else
stack[stack_top]->no = question;
}
stack[++stack_top] = question;
}
} }
if (stack_top >= 0) { // Remove newline character from line
root = stack[0]; line[strcspn(line, "\n")] = 0;
// Check if the line is an answer (starts with '*')
if (line[0] == '*') {
(*count)++;
return createNode(line + 1); // Skip the '*' character
} }
return root; // If it's a question, create a node and recursively read its children
Node* node = createNode(line);
node->yes = readTree(file, count);
node->no = readTree(file, count);
return node;
} }
void ask_question(Node *node) { // Function to interact with the user and navigate the tree
if (!node) return; void queryUser(Node* node) {
if (node == NULL) return;
if (node->yes == NULL && node->no == NULL) { if (node->yes == NULL && node->no == NULL) {
printf("*%s\n", node->content); // Removed extra space after "*" printf("Expert z bufetu to vie. Je to: %s\n", node->text);
printf("Koniec\n");
return; return;
} }
printf("%s\n", node->content); // Print question and get user's answer
printf("%s\n", node->text);
char response;
while (1) {
response = getchar();
getchar(); // Capture newline
if (response == 'a' && node->yes) {
ask_question(node->yes);
break;
} else if (response == 'n' && node->no) {
ask_question(node->no);
break;
} else {
printf("Nerozumiem\n");
}
}
}
int main() {
printf("Expert z bufetu to vie.\n");
printf("Pozna 2 druhov ovocia a zeleniny.\n");
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
char input[50]; char answer;
int understood = 0; // To track if the response was understood scanf(" %c", &answer);
while (1) { if (answer == 'a') {
printf("Je to ovocie alebo zelenina\n"); queryUser(node->yes);
fgets(input, sizeof(input), stdin); } else if (answer == 'n') {
queryUser(node->no);
} else {
printf("Nespravny vstup.\n");
}
}
if (strcmp(input, "a\n") == 0 || strcmp(input, "n\n") == 0) { // Function to free the tree
understood = 1; // Valid response received void freeTree(Node* node) {
break; if (node == NULL) return;
} else { freeTree(node->yes);
printf("Nerozumiem\n"); freeTree(node->no);
if (understood) break; // Stop if an invalid input has already been handled free(node);
} }
int main() {
FILE* file = fopen("baza.txt", "r");
if (!file) {
printf("Chyba pri nacitani suboru.\n");
return 1;
} }
return 0; // Ensure correct return value int count = 0;
Node* root = readTree(file, &count);
fclose(file);
if (root == NULL) {
printf("Chyba pri nacitani baze pravidiel.\n");
return 1;
}
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
queryUser(root);
freeTree(root);
return 0;
} }