Update cv7/program.c
This commit is contained in:
parent
cd502360a7
commit
6e022baaca
@ -27,7 +27,7 @@ void free_tree(Node *root) {
|
|||||||
|
|
||||||
int count_answers(Node *root) {
|
int count_answers(Node *root) {
|
||||||
if (!root) return 0;
|
if (!root) return 0;
|
||||||
if (root->yes == NULL && root->no == NULL) return 1; // It's an answer
|
if (root->yes == NULL && root->no == NULL) return 1;
|
||||||
return count_answers(root->yes) + count_answers(root->no);
|
return count_answers(root->yes) + count_answers(root->no);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +38,11 @@ Node* parse_rules() {
|
|||||||
int stack_top = -1;
|
int stack_top = -1;
|
||||||
|
|
||||||
while (fgets(line, sizeof(line), stdin)) {
|
while (fgets(line, sizeof(line), stdin)) {
|
||||||
// Trim newline and spaces
|
// Trim newline
|
||||||
line[strcspn(line, "\n")] = 0;
|
line[strcspn(line, "\n")] = 0;
|
||||||
if (strlen(line) == 0) break; // End of rules
|
if (strlen(line) == 0) break;
|
||||||
|
|
||||||
if (line[0] == '*') { // This is an answer
|
if (line[0] == '*') {
|
||||||
Node *answer = create_node(line + 1);
|
Node *answer = create_node(line + 1);
|
||||||
if (stack_top >= 0) {
|
if (stack_top >= 0) {
|
||||||
if (stack[stack_top]->yes == NULL)
|
if (stack[stack_top]->yes == NULL)
|
||||||
@ -50,7 +50,7 @@ Node* parse_rules() {
|
|||||||
else
|
else
|
||||||
stack[stack_top]->no = answer;
|
stack[stack_top]->no = answer;
|
||||||
}
|
}
|
||||||
} else { // This is a question
|
} else {
|
||||||
Node *question = create_node(line);
|
Node *question = create_node(line);
|
||||||
if (stack_top >= 0) {
|
if (stack_top >= 0) {
|
||||||
if (stack[stack_top]->yes == NULL)
|
if (stack[stack_top]->yes == NULL)
|
||||||
@ -76,27 +76,19 @@ void ask_question(Node *node) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the current question
|
printf("%s\n", node->content);
|
||||||
printf("%s?\n", node->content);
|
if (node->yes && node->yes->content[0] != '*') {
|
||||||
if (node->yes) {
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
printf("* %s\n", node->yes->content);
|
|
||||||
}
|
}
|
||||||
if (node->no) {
|
|
||||||
printf("* %s\n", node->no->content);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prompt for input
|
|
||||||
printf("\n");
|
|
||||||
printf("n\n");
|
|
||||||
|
|
||||||
char response;
|
char response;
|
||||||
while (1) {
|
while (1) {
|
||||||
response = getchar();
|
response = getchar();
|
||||||
getchar(); // To capture newline
|
getchar(); // To capture newline
|
||||||
if (response == 'n') {
|
if (response == 'a' && node->yes) {
|
||||||
ask_question(node->yes);
|
ask_question(node->yes);
|
||||||
break;
|
break;
|
||||||
} else if (response == 'n') {
|
} else if (response == 'n' && node->no) {
|
||||||
ask_question(node->no);
|
ask_question(node->no);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
@ -107,7 +99,6 @@ void ask_question(Node *node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// General system information only once
|
|
||||||
printf("Expert z bufetu to vie.\n");
|
printf("Expert z bufetu to vie.\n");
|
||||||
|
|
||||||
Node *root = parse_rules();
|
Node *root = parse_rules();
|
||||||
@ -119,9 +110,11 @@ int main() {
|
|||||||
int answer_count = count_answers(root);
|
int answer_count = count_answers(root);
|
||||||
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
|
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
|
||||||
|
|
||||||
// Ask the first question
|
if (root) {
|
||||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
ask_question(root);
|
ask_question(root);
|
||||||
|
}
|
||||||
|
|
||||||
free_tree(root);
|
free_tree(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user