Aktualizovat du5/program.c

This commit is contained in:
Tomáš Vlček 2026-04-15 09:12:43 +00:00
parent 5267644e31
commit 44fdbcf292

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#define SIZE_LINES 1024 #define SIZE_LINES 1024
#define SIZE 256 #define SIZE 256
@ -7,8 +8,8 @@
typedef struct { typedef struct {
char text[SIZE]; char text[SIZE];
bool isAnswer; bool isAnswer;
int yes; //ekvivalent 'left' (alebo 'a' v pripade nasej ulohy) int yes; //ekvivalent 'left' (alebo 'a')
int no; //ekvivalent 'right' (alebo 'n' v pripade nasej ulohy) int no; //ekvivalent 'right' (alebo 'n')
} Node; } Node;
Node tree[SIZE]; Node tree[SIZE];
@ -22,14 +23,15 @@ int answerCount = 0;
int invalidIndex = -1; int invalidIndex = -1;
// vytvorii binarny strom na zakl. vstupu // 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() { int buildATree() {
if (indexLine >= lineCount) if (indexLine >= lineCount)
{ {
return -1; return -1;
} }
char *line = lines[indexLine + 1]; indexLine += 1;
char *line = lines[indexLine];
//pomocny index //pomocny index
int current = nodeCount++; int current = nodeCount++;
@ -45,7 +47,7 @@ int buildATree() {
tree[current].isAnswer = false; tree[current].isAnswer = false;
strcpy(tree[current].text, line); 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].yes = buildATree(); // 'a'
tree[current].no = buildATree(); // 'n' tree[current].no = buildATree(); // 'n'
@ -58,8 +60,11 @@ void goThroughTheTree(int rootIndex)
int current = rootIndex; int current = rootIndex;
char input; char input;
while (current != -1) { //
if (tree[current].isAnswer) { while (current != -1)
{
if (tree[current].isAnswer)
{
printf("*%s\n", tree[current].text); printf("*%s\n", tree[current].text);
printf("Koniec\n"); printf("Koniec\n");
return; return;
@ -91,25 +96,25 @@ int main()
// Load input/ // Load input/
while (fgets(buffer, SIZE, stdin)) while (fgets(buffer, SIZE, stdin))
{ {
//prestane citat vstup, ak je NEPLATNY
if (strcmp(buffer, "\n") == 0) if (strcmp(buffer, "\n") == 0)
{ {
break; 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') if (buffer[i] == '\n')
{ {
buffer[i] = '\0'; buffer[i] = '\0';
} }
} }
// buffer[strcspn(buffer, "\n")] = '\0'; lineCount += 1;
strcpy(lines[lineCount + 1], buffer); strcpy(lines[lineCount], buffer);
} }
int root = buildTree(); int root = buildATree();
if (root == -1 || indexLine != lineCount) { if (root == -1 || indexLine != lineCount) {
printf("Koniec vstupu.\n"); printf("Koniec vstupu.\n");
return 0; return 0;