du6 - 2
This commit is contained in:
parent
b1cc764178
commit
1375e5d3fe
@ -10,21 +10,21 @@ typedef struct tree {
|
||||
struct tree *right;
|
||||
} Tree;
|
||||
|
||||
/* Odstráni koncový znak \n */
|
||||
//odstranit koncovy znak
|
||||
void strip_newline(char *s) {
|
||||
int len = strlen(s);
|
||||
if (len > 0 && s[len-1] == '\n')
|
||||
s[len-1] = '\0';
|
||||
}
|
||||
|
||||
/* Rekurzívne načítanie stromu v preorder */
|
||||
// rekurzivne nacitanie stromu v preorder
|
||||
Tree* read_tree() {
|
||||
char buffer[SIZE];
|
||||
|
||||
if (!fgets(buffer, SIZE, stdin))
|
||||
return NULL;
|
||||
|
||||
/* prázdny riadok = koniec bázy pravidiel */
|
||||
// prazdny riadok -> koniec
|
||||
if (strcmp(buffer, "\n") == 0)
|
||||
return NULL;
|
||||
|
||||
@ -35,12 +35,12 @@ Tree* read_tree() {
|
||||
|
||||
strcpy(node->value, buffer);
|
||||
|
||||
/* Odpoveď = list */
|
||||
// odpoved -> list
|
||||
if (buffer[0] == '*') {
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Inak načítaj oboch potomkov */
|
||||
// inak nacitat oboch potomkov
|
||||
node->left = read_tree();
|
||||
if (!node->left) {
|
||||
free(node);
|
||||
@ -57,7 +57,7 @@ Tree* read_tree() {
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Uvoľnenie pamäte */
|
||||
// uvolnenie pamate
|
||||
void destroy_tree(Tree *t) {
|
||||
if (!t) return;
|
||||
destroy_tree(t->left);
|
||||
@ -65,7 +65,7 @@ void destroy_tree(Tree *t) {
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* Počet listov = počet tovarov */
|
||||
// pocet listov = pocet tovarov
|
||||
int count_leaves(Tree *t) {
|
||||
if (!t) return 0;
|
||||
if (!t->left && !t->right)
|
||||
@ -73,11 +73,11 @@ int count_leaves(Tree *t) {
|
||||
return count_leaves(t->left) + count_leaves(t->right);
|
||||
}
|
||||
|
||||
/* Spustenie znalostného systému */
|
||||
// spustenie znalostneho systemu
|
||||
void run_system(Tree *node) {
|
||||
printf("%s\n", node->value);
|
||||
|
||||
/* List → koniec */
|
||||
// list -> koniec
|
||||
if (!node->left && !node->right) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
@ -94,20 +94,20 @@ void run_system(Tree *node) {
|
||||
} else if (c == 'n') {
|
||||
run_system(node->right);
|
||||
} else {
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
printf("Nerozumiem\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Tree *root = read_tree();
|
||||
|
||||
/* Ak sa nepodarilo načítať koreň, chyba */
|
||||
// nepodarilo sa nacitat koren -> chyba
|
||||
if (!root) {
|
||||
printf("Chyba\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Skontrolovať prázdny riadok po báze pravidiel */
|
||||
// skontrolovat prazdny riadok
|
||||
char buffer[SIZE];
|
||||
if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) {
|
||||
printf("Chyba\n");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user