a4
This commit is contained in:
parent
d5fc3c1ca3
commit
2f5087cd54
13
du4/Makefile
Normal file
13
du4/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC = gcc
|
||||||
|
CFLAGS = -std=c11 -Wall -Wextra -Werror
|
||||||
|
|
||||||
|
TARGET = game
|
||||||
|
SRC = game.c
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) $(SRC) -o $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
160
du4/game.c
Normal file
160
du4/game.c
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "a4.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;
|
||||||
|
|
||||||
|
freeTree(node->yes);
|
||||||
|
freeTree(node->no);
|
||||||
|
free(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
// citanie stromu (preorder)
|
||||||
|
Tree* readTree() {
|
||||||
|
char line[SIZE];
|
||||||
|
|
||||||
|
if (fgets(line, SIZE, stdin) == NULL) {
|
||||||
|
error = 1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// odstranenie konca riadku
|
||||||
|
line[strcspn(line, "\r\n")] = '\0';
|
||||||
|
|
||||||
|
// prazdny riadok = chyba
|
||||||
|
if (line[0] == '\0') {
|
||||||
|
error = 1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *node = (Tree*)malloc(sizeof(Tree));
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
|
error = 1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
node->yes = NULL;
|
||||||
|
node->no = NULL;
|
||||||
|
|
||||||
|
// list
|
||||||
|
if (line[0] == '*') {
|
||||||
|
node->isAnswer = 1;
|
||||||
|
strcpy(node->text, line + 1);
|
||||||
|
}
|
||||||
|
// otazka
|
||||||
|
else {
|
||||||
|
node->isAnswer = 0;
|
||||||
|
strcpy(node->text, line);
|
||||||
|
|
||||||
|
node->yes = readTree();
|
||||||
|
node->no = readTree();
|
||||||
|
|
||||||
|
if (!node->yes || !node->no) {
|
||||||
|
error = 1;
|
||||||
|
freeTree(node);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// kontrola prazdneho riadku po strome
|
||||||
|
int EmptyLine() {
|
||||||
|
char line[SIZE];
|
||||||
|
|
||||||
|
if (fgets(line, SIZE, stdin) == NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return strcmp(line, "\n") == 0 ||
|
||||||
|
strcmp(line, "\r\n") == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pocet listov
|
||||||
|
int Leaves(Tree *node) {
|
||||||
|
if (!node) return 0;
|
||||||
|
|
||||||
|
if (node->isAnswer)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return Leaves(node->yes) + Leaves(node->no);
|
||||||
|
}
|
||||||
|
|
||||||
|
// hra
|
||||||
|
void start(Tree *node) {
|
||||||
|
char input[SIZE];
|
||||||
|
|
||||||
|
while (node && !node->isAnswer) {
|
||||||
|
|
||||||
|
printf("%s\n", node->text);
|
||||||
|
|
||||||
|
if (fgets(input, SIZE, stdin) == NULL) {
|
||||||
|
printf("Koniec vstupu\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (input[i] == ' ' || input[i] == '\t')
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (input[i] == 'a' || input[i] == 'A') {
|
||||||
|
node = node->yes;
|
||||||
|
}
|
||||||
|
else if (input[i] == 'n' || input[i] == 'N') {
|
||||||
|
node = node->no;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Nerozumiem\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node && node->isAnswer) {
|
||||||
|
printf("%s\n", node->text);
|
||||||
|
printf("Koniec\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
printf("Expert z bufetu to vie.\n");
|
||||||
|
|
||||||
|
Tree *root = readTree();
|
||||||
|
|
||||||
|
if (!root || error) {
|
||||||
|
printf("Chybna databaza\n");
|
||||||
|
freeTree(root);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// po strome musi byt prazdny riadok
|
||||||
|
if (!EmptyLine()) {
|
||||||
|
printf("Chybna databaza\n");
|
||||||
|
freeTree(root);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Pozna %d druhov ovocia a zeleniny.\n", Leaves(root));
|
||||||
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
|
|
||||||
|
start(root);
|
||||||
|
|
||||||
|
freeTree(root);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#ifndef A4_H
|
#ifndef GAME_H
|
||||||
#define A4_H
|
#define GAME_H
|
||||||
|
|
||||||
#define SIZE 256
|
#define SIZE 256
|
||||||
|
|
||||||
@ -10,19 +10,10 @@ typedef struct Tree {
|
|||||||
struct Tree *no;
|
struct Tree *no;
|
||||||
} Tree;
|
} Tree;
|
||||||
|
|
||||||
// citanie stromu
|
|
||||||
Tree* readTree();
|
Tree* readTree();
|
||||||
|
|
||||||
// kontrola prazdneho riadku
|
|
||||||
int EmptyLine();
|
int EmptyLine();
|
||||||
|
|
||||||
// pocet listov
|
|
||||||
int Leaves(Tree *node);
|
int Leaves(Tree *node);
|
||||||
|
|
||||||
// uvolnenie pamate
|
|
||||||
void freeTree(Tree *node);
|
void freeTree(Tree *node);
|
||||||
|
|
||||||
// hra
|
|
||||||
void start(Tree *node);
|
void start(Tree *node);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user