diff --git a/du5/program.c b/du5/program.c index 330891c..ed46bd8 100644 --- a/du5/program.c +++ b/du5/program.c @@ -1,5 +1,6 @@ #include #include +#include #define SIZE_LINES 1024 #define SIZE 256 @@ -7,8 +8,8 @@ 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) + int yes; //ekvivalent 'left' (alebo 'a') + int no; //ekvivalent 'right' (alebo 'n') } Node; Node tree[SIZE]; @@ -22,14 +23,15 @@ int answerCount = 0; int invalidIndex = -1; // vytvorii binarny strom na zakl. vstupu -//VRATI -> Bud cislo > 0 ALEBO stav '-1' (Neplatny index korenu) ALEBO -2 +//** 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 + 1]; + indexLine += 1; + char *line = lines[indexLine]; //pomocny index int current = nodeCount++; @@ -45,7 +47,7 @@ int buildATree() { tree[current].isAnswer = false; strcpy(tree[current].text, line); - //rekurzivne vytvorii dalsie casti na zaklade toho, ci odpoved sa rovna anu + //rekurzivne vytvorii dalsie 'nodes' na na zaklade vycitanych odpovedi z standard inputu tree[current].yes = buildATree(); // 'a' tree[current].no = buildATree(); // 'n' @@ -58,8 +60,11 @@ void goThroughTheTree(int rootIndex) int current = rootIndex; char input; - while (current != -1) { - if (tree[current].isAnswer) { + // + while (current != -1) + { + if (tree[current].isAnswer) + { printf("*%s\n", tree[current].text); printf("Koniec\n"); return; @@ -91,25 +96,25 @@ int main() // Load input/ while (fgets(buffer, SIZE, stdin)) { - + //prestane citat vstup, ak je NEPLATNY if (strcmp(buffer, "\n") == 0) { break; } - for (int i = 0; i < SIZE, i++) + //nahradi NewLine symbol za null terminator (Newline sa prida naspat potom na konci) + for (int i = 0; i < SIZE; i++) { if (buffer[i] == '\n') { buffer[i] = '\0'; } } - // buffer[strcspn(buffer, "\n")] = '\0'; - strcpy(lines[lineCount + 1], buffer); + lineCount += 1; + strcpy(lines[lineCount], buffer); } - int root = buildTree(); + int root = buildATree(); - if (root == -1 || indexLine != lineCount) { printf("Koniec vstupu.\n"); return 0;