This commit is contained in:
mr314ot 2025-11-17 11:32:14 +01:00
parent 8d48338993
commit 388cc3b02d

View File

@ -10,9 +10,9 @@ typedef struct tree {
struct tree *right; struct tree *right;
} Tree; } Tree;
int valid_db = 1; // flag pre validitu databazy int valid_db = 1;
// odstranit koncovy znak \n //odstranit koncovy znak
void strip_newline(char *s) { void strip_newline(char *s) {
int len = strlen(s); int len = strlen(s);
if (len > 0 && s[len-1] == '\n') if (len > 0 && s[len-1] == '\n')
@ -26,6 +26,7 @@ Tree* read_tree() {
if (!fgets(buffer, SIZE, stdin)) if (!fgets(buffer, SIZE, stdin))
return NULL; return NULL;
// prazdny riadok -> koniec
if (strcmp(buffer, "\n") == 0) if (strcmp(buffer, "\n") == 0)
return NULL; return NULL;
@ -36,12 +37,12 @@ Tree* read_tree() {
strcpy(node->value, buffer); strcpy(node->value, buffer);
// list // odpoved -> list
if (buffer[0] == '*') { if (buffer[0] == '*') {
return node; return node;
} }
// očakávame presne 2 potomkov // inak nacitat oboch potomkov
node->left = read_tree(); node->left = read_tree();
if (!node->left) { if (!node->left) {
valid_db = 0; valid_db = 0;
@ -51,8 +52,8 @@ Tree* read_tree() {
node->right = read_tree(); node->right = read_tree();
if (!node->right) { if (!node->right) {
free(node->left);
valid_db = 0; valid_db = 0;
destroy_tree(node->left);
free(node); free(node);
return NULL; return NULL;
} }
@ -88,11 +89,15 @@ void run_system(Tree *node) {
return; return;
} }
char c; int c;
if (scanf(" %c", &c) != 1) {
printf("Nerozumiem\n"); do {
c = getchar();
if (c == EOF) {
printf("Koniec vstupu\n");
return; return;
} }
} while (c=='\n' || c=='\r' || c==' ' || c=='\t');
if (c == 'a') { if (c == 'a') {
run_system(node->left); run_system(node->left);
@ -106,14 +111,14 @@ void run_system(Tree *node) {
int main() { int main() {
Tree *root = read_tree(); Tree *root = read_tree();
// kontrola prázdneho alebo chybného stromu // nepodarilo sa nacitat koren -> chyba
if (!root || !valid_db) { if (!root || !valid_db) {
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Chybna databaza\n"); printf("Chybna databaza\n");
return 0; return 0;
} }
// skontrolovat prazdny riadok po databaze // skontrolovat prazdny riadok
char buffer[SIZE]; char buffer[SIZE];
if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) { if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) {
printf("Koniec vstupu\n"); printf("Koniec vstupu\n");