a4:
git commit -m a4:
This commit is contained in:
parent
2f5087cd54
commit
1f4d693fa2
14
du4/Makefile
14
du4/Makefile
@ -1,13 +1,5 @@
|
||||
CC = gcc
|
||||
CFLAGS = -std=c11 -Wall -Wextra -Werror
|
||||
|
||||
TARGET = game
|
||||
SRC = game.c
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(SRC)
|
||||
$(CC) $(CFLAGS) $(SRC) -o $(TARGET)
|
||||
all:
|
||||
gcc -std=c11 -Wall -Wextra -Werror game.c -o game
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
rm -f game
|
||||
|
||||
52
du4/game.c
52
du4/game.c
@ -1,18 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "a4.h"
|
||||
#include "game.h"
|
||||
|
||||
int error = 0;
|
||||
|
||||
typedef struct Tree {
|
||||
char text[SIZE];
|
||||
int isAnswer;
|
||||
struct Tree *yes;
|
||||
struct Tree *no;
|
||||
} Tree;
|
||||
|
||||
// uvolnenie pamate
|
||||
void freeTree(Tree *node) {
|
||||
if (!node) return;
|
||||
|
||||
@ -21,58 +14,47 @@ void freeTree(Tree *node) {
|
||||
free(node);
|
||||
}
|
||||
|
||||
// citanie stromu (preorder)
|
||||
Tree* readTree() {
|
||||
char line[SIZE];
|
||||
|
||||
if (fgets(line, SIZE, stdin) == NULL) {
|
||||
error = 1;
|
||||
if (fgets(line, SIZE, stdin) == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// odstranenie konca riadku
|
||||
line[strcspn(line, "\r\n")] = '\0';
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
|
||||
// prazdny riadok = chyba
|
||||
if (line[0] == '\0') {
|
||||
error = 1;
|
||||
if (line[0] == '\0')
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Tree *node = (Tree*)malloc(sizeof(Tree));
|
||||
|
||||
if (!node) {
|
||||
error = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Tree *node = malloc(sizeof(Tree));
|
||||
node->yes = NULL;
|
||||
node->no = NULL;
|
||||
|
||||
// list
|
||||
if (line[0] == '*') {
|
||||
node->isAnswer = 1;
|
||||
strcpy(node->text, line + 1);
|
||||
return node;
|
||||
}
|
||||
// otazka
|
||||
else {
|
||||
|
||||
node->isAnswer = 0;
|
||||
strcpy(node->text, line);
|
||||
|
||||
node->yes = readTree();
|
||||
node->no = readTree();
|
||||
|
||||
if (!node->yes || !node->no) {
|
||||
error = 1;
|
||||
if (!node->yes) {
|
||||
freeTree(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->no = readTree();
|
||||
if (!node->no) {
|
||||
freeTree(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// kontrola prazdneho riadku po strome
|
||||
|
||||
|
||||
int EmptyLine() {
|
||||
char line[SIZE];
|
||||
|
||||
@ -83,7 +65,6 @@ int EmptyLine() {
|
||||
strcmp(line, "\r\n") == 0;
|
||||
}
|
||||
|
||||
// pocet listov
|
||||
int Leaves(Tree *node) {
|
||||
if (!node) return 0;
|
||||
|
||||
@ -93,7 +74,6 @@ int Leaves(Tree *node) {
|
||||
return Leaves(node->yes) + Leaves(node->no);
|
||||
}
|
||||
|
||||
// hra
|
||||
void start(Tree *node) {
|
||||
char input[SIZE];
|
||||
|
||||
@ -141,7 +121,6 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// po strome musi byt prazdny riadok
|
||||
if (!EmptyLine()) {
|
||||
printf("Chybna databaza\n");
|
||||
freeTree(root);
|
||||
@ -154,7 +133,6 @@ int main() {
|
||||
start(root);
|
||||
|
||||
freeTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
7
du4/input.txt
Normal file
7
du4/input.txt
Normal file
@ -0,0 +1,7 @@
|
||||
Je to ovocie?
|
||||
Je to zlte?
|
||||
*banan
|
||||
*jablko
|
||||
Je to zelenina?
|
||||
*mrkva
|
||||
*brokolica
|
||||
Loading…
Reference in New Issue
Block a user