151 lines
3.5 KiB
C
151 lines
3.5 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#define SIZE_LINES 1024
|
|
#define SIZE 256
|
|
|
|
typedef struct {
|
|
char text[SIZE];
|
|
bool isAnswer;
|
|
int yes; //ekvivalent 'left' (alebo 'a')
|
|
int no; //ekvivalent 'right' (alebo 'n')
|
|
} Node;
|
|
|
|
Node tree[SIZE];
|
|
char lines[SIZE_LINES][SIZE];
|
|
|
|
//pomocne premeny
|
|
int lineCount = 0;
|
|
int indexLine = 0;
|
|
int nodeCount = 0;
|
|
int answerCount = 0;
|
|
|
|
//pomocna konstanta
|
|
const int invalidIndex = -1;
|
|
|
|
// vytvorii binarny strom na zakl. vstupu
|
|
//** VRATI -> Bud cislo > 0 (Platny Koren) ALEBO stav '-1' (Neplatny index korenu, cize koniec stromu)
|
|
int buildATree()
|
|
{
|
|
if (indexLine >= lineCount)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
char *line = lines[indexLine];
|
|
indexLine += 1;
|
|
//pomocny index
|
|
int current = nodeCount;
|
|
nodeCount += 1;
|
|
|
|
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 'nodes' (uzly po slovensky) na na zaklade vycitanych odpovedi z standard inputu
|
|
tree[current].yes = buildATree(); // 'a'
|
|
tree[current].no = buildATree(); // 'n'
|
|
|
|
return current;
|
|
}
|
|
|
|
void goThroughTheTree(int rootIndex)
|
|
{
|
|
//pomocne premeny
|
|
int current = rootIndex;
|
|
char input = '0'; // '0' = ako keby nic / NULL
|
|
|
|
while (current != -1)
|
|
{
|
|
if (tree[current].isAnswer)
|
|
{
|
|
printf("*%s\n", tree[current].text);
|
|
printf("Koniec\n");
|
|
return;
|
|
}
|
|
|
|
printf("%s\n", tree[current].text);
|
|
|
|
//nacitanie odpovedi (jeden char)
|
|
if (scanf(" %c", &input) != 1)
|
|
{
|
|
printf("Koniec vstupu\n");
|
|
return;
|
|
}
|
|
|
|
if (input == 'a')
|
|
{
|
|
current = tree[current].yes;
|
|
}
|
|
else if (input == 'n')
|
|
{
|
|
current = tree[current].no;
|
|
}
|
|
else if (input != 'n' || input != 'a')
|
|
{
|
|
printf("Nerozumiem\n");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
char buffer[SIZE];
|
|
bool isErr = false; //pomocna error premena
|
|
|
|
//null-terminovanie
|
|
buffer[0] = '\0';
|
|
|
|
// citanie vstupu
|
|
while (fgets(buffer, SIZE, stdin))
|
|
{
|
|
//prestane citat vstup, ak je NEPLATNY (rovna sa Newline symbolu, resp. prazdny riadok)
|
|
//ALEBO ak sa nenacital riadok vobec (buffer je uplne prazdny, (resp prvy prvok je null terminator))
|
|
if (strcmp(buffer, "\n") == 0 || buffer[0] == '\0')
|
|
{
|
|
isErr = true;
|
|
break;
|
|
}
|
|
//nahradi NewLine symbol za null terminator (Newline sa prida naspat potom na konci programu)
|
|
buffer[strcspn(buffer, "\n")] = '\0';
|
|
|
|
strcpy(lines[lineCount], buffer);
|
|
lineCount += 1;
|
|
}
|
|
|
|
//zisk. korena
|
|
int root = buildATree();
|
|
|
|
if (tree[root].yes == -1 && tree[root].no == -1)
|
|
{
|
|
printf("Koniec vstupu.\n");
|
|
return 0;
|
|
}
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
if (!isErr) //vypise vysledky/statistiky, AK nebola chyba na zaciatku citania vstupu
|
|
{
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", answerCount);
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
}
|
|
else
|
|
{
|
|
printf("Chybna␣databaza");
|
|
}
|
|
|
|
goThroughTheTree(root);
|
|
|
|
return 0;
|
|
} |