Aktualizovat du5/program.c
This commit is contained in:
parent
633812654d
commit
7703b877a8
193
du5/program.c
193
du5/program.c
@ -1,96 +1,125 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 128
|
||||
#define SIZE_LINES 1024
|
||||
#define SIZE 256
|
||||
|
||||
int main()
|
||||
{
|
||||
char line[SIZE];
|
||||
char option1[SIZE];
|
||||
char option2[SIZE];
|
||||
char question[SIZE];
|
||||
char question2[SIZE];
|
||||
char answer = '0';
|
||||
char answer2 = '0'; //'0' = ako keby 'NULL'/ absencia hodnoty
|
||||
int fruitCount = 0;
|
||||
bool isFirstQuestionRead = false;
|
||||
bool isFirstOptionLoaded = false;
|
||||
bool err = false;
|
||||
|
||||
option1[0] = '\0';
|
||||
option2[0] = '\0';
|
||||
question[0] = '\0';
|
||||
question2[0] = '\0';
|
||||
line[0] = '\0';
|
||||
typedef struct {
|
||||
char text[SIZE];
|
||||
bool isAnswer;
|
||||
int yes; //ekvivalent 'left' (alebo 'a' v pripade nasej ulohy)
|
||||
int no; //ekvivalent 'right' (alebo 'n' v pripade nasej ulohy)
|
||||
} Node;
|
||||
|
||||
while (fgets(line, SIZE, stdin) != NULL)
|
||||
Node tree[SIZE];
|
||||
char lines[SIZE_LINES][SIZE];
|
||||
|
||||
//pomocne premeny
|
||||
int lineCount = 0;
|
||||
int indexLine = 0;
|
||||
int nodeCount = 0;
|
||||
int answerCount = 0;
|
||||
int invalidIndex = -1;
|
||||
|
||||
// vytvorii binarny strom na zakl. vstupu
|
||||
//VRATI -> Bud cislo > 0 ALEBO stav '-1' (Neplatny index korenu) ALEBO -2
|
||||
int buildATree() {
|
||||
if (indexLine >= lineCount)
|
||||
{
|
||||
if (line[0] == '*')
|
||||
{
|
||||
fruitCount += 1;
|
||||
|
||||
//precita a ulozi odpoved na zaklade kontrolnej premeny
|
||||
if (!isFirstOptionLoaded)
|
||||
{
|
||||
sscanf(line, "%s [^\n]", &option1);
|
||||
isFirstOptionLoaded = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(line, "%s [^\n]", &option2);
|
||||
}
|
||||
}
|
||||
else if (line[1] == '\n')
|
||||
{
|
||||
if (answer == '0')
|
||||
{
|
||||
answer = line[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
answer2 = line[0];
|
||||
}
|
||||
|
||||
}
|
||||
else if (!isFirstQuestionRead)
|
||||
{
|
||||
sscanf(line, "%127[^\n]]", question);
|
||||
isFirstQuestionRead = true;
|
||||
// strcat(question, "?");
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(line, "%127[^\n]]", question2);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (answer == '0') { err = true; }
|
||||
char *line = lines[indexLine + 1];
|
||||
//pomocny index
|
||||
int current = nodeCount++;
|
||||
|
||||
if (line[0] == '*') {
|
||||
tree[current].isAnswer = true;
|
||||
strcpy(tree[current].text, line + 1);
|
||||
tree[current].yes = invalidIndex;
|
||||
tree[current].no = invalidIndex;
|
||||
answerCount += 1;
|
||||
return current;
|
||||
}
|
||||
|
||||
tree[current].isAnswer = false;
|
||||
strcpy(tree[current].text, line);
|
||||
|
||||
//rekurzivne vytvorii dalsie casti na zaklade toho, ci odpoved sa rovna anu
|
||||
tree[current].yes = buildATree(); // 'a'
|
||||
tree[current].no = buildATree(); // 'n'
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
void goThroughTheTree(int rootIndex)
|
||||
{
|
||||
//pomocne premeny
|
||||
int current = rootIndex;
|
||||
char input;
|
||||
|
||||
while (current != -1) {
|
||||
if (tree[current].isAnswer) {
|
||||
printf("*%s\n", tree[current].text);
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s\n", tree[current].text);
|
||||
scanf(" %c", &input);
|
||||
|
||||
if (input == 'a')
|
||||
{
|
||||
current = tree[current].yes;
|
||||
}
|
||||
else if (input == 'n')
|
||||
{
|
||||
current = tree[current].no;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Nerozumiem.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char buffer[SIZE];
|
||||
|
||||
// Load input
|
||||
while (fgets(buffer, SIZE, stdin))
|
||||
{
|
||||
|
||||
if (strcmp(buffer, "\n") == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < SIZE, i++)
|
||||
{
|
||||
if (buffer[i] == '\n')
|
||||
{
|
||||
buffer[i] = '\0';
|
||||
}
|
||||
}
|
||||
// buffer[strcspn(buffer, "\n")] = '\0';
|
||||
strcpy(lines[lineCount + 1], buffer);
|
||||
}
|
||||
|
||||
int root = buildTree();
|
||||
|
||||
|
||||
if (root == -1 || indexLine != lineCount) {
|
||||
printf("Koniec vstupu.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", fruitCount);
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", answerCount);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
printf("%s\n", question);
|
||||
if (question2[0] != '\0') {printf("%s\n", question2); }
|
||||
if (answer == 'a')
|
||||
{
|
||||
printf("%s\n", option1);
|
||||
}
|
||||
else if (answer == 'n')
|
||||
{
|
||||
printf("%s\n", option2);
|
||||
}
|
||||
else if (answer == '0')
|
||||
{
|
||||
printf("Koniec vstupu\n");
|
||||
}
|
||||
else //v pripade, ze nespravny vstup bol zadany uzivatelom
|
||||
{
|
||||
printf("Nerozumiem\n");
|
||||
err = true;
|
||||
}
|
||||
if (!err) { printf("Koniec\n"); }
|
||||
|
||||
goThroughTheTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user