funguje
This commit is contained in:
parent
d53ab280c9
commit
3e011eb80f
148
du6/program.c
148
du6/program.c
@ -6,49 +6,54 @@
|
||||
|
||||
#define SIZE 256
|
||||
|
||||
struct tree
|
||||
{
|
||||
struct tree {
|
||||
char value[SIZE];
|
||||
struct tree* left;
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
void trim_whitespace(char* str)
|
||||
{
|
||||
int i=strlen(str)-1;
|
||||
while (i>=0 && isspace(str[i]))
|
||||
{
|
||||
str[i]='\0';
|
||||
// Funkcia na odstránenie bieleho miesta z reťazca
|
||||
void trim_whitespace(char* str) {
|
||||
int i = strlen(str) - 1;
|
||||
while (i >= 0 && isspace(str[i])) {
|
||||
str[i] = '\0';
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
struct tree* read_tree()
|
||||
{
|
||||
// Funkcia na načítanie stromu z pravidiel
|
||||
struct tree* read_tree() {
|
||||
char buffer[SIZE];
|
||||
if (fgets(buffer, SIZE, stdin)==NULL)
|
||||
{
|
||||
|
||||
// Načítanie riadku
|
||||
if (fgets(buffer, SIZE, stdin) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (buffer[0]=='\n')
|
||||
{
|
||||
|
||||
// Ak je prázdny riadok, vráť NULL
|
||||
if (buffer[0] == '\n') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Odstráň biele miesto
|
||||
trim_whitespace(buffer);
|
||||
struct tree* node=(struct tree*)calloc(1, sizeof(struct tree));
|
||||
|
||||
// Vytvorenie nového uzla
|
||||
struct tree* node = (struct tree*)calloc(1, sizeof(struct tree));
|
||||
strcpy(node->value, buffer);
|
||||
if (buffer[0]!='*')
|
||||
{
|
||||
node->left=read_tree();
|
||||
node->right=read_tree();
|
||||
|
||||
// Ak nie je listový uzol (nezačína sa *), rekurzívne načítaj ľavého a pravého syna
|
||||
if (buffer[0] != '*') {
|
||||
node->left = read_tree();
|
||||
node->right = read_tree();
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
// Funkcia na uvoľnenie pamäte stromu
|
||||
void destroy_tree(struct tree* tree) {
|
||||
if (tree == NULL) {
|
||||
return;
|
||||
}
|
||||
destroy_tree(tree->left);
|
||||
@ -56,86 +61,89 @@ void destroy_tree(struct tree* tree)
|
||||
free(tree);
|
||||
}
|
||||
|
||||
int count_answers(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
// Funkcia na spočítanie listových uzlov (odpovedí)
|
||||
int count_answers(struct tree* tree) {
|
||||
if (tree == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (tree->value[0]=='*')
|
||||
{
|
||||
|
||||
// Ak je listový uzol (odpoveď)
|
||||
if (tree->value[0] == '*') {
|
||||
return 1;
|
||||
}
|
||||
return count_answers(tree->left)+count_answers(tree->right);
|
||||
|
||||
// Inak rekurzívne spočítaj odpovede z podstromov
|
||||
return count_answers(tree->left) + count_answers(tree->right);
|
||||
}
|
||||
|
||||
void run_knowledge_system(struct tree* node)
|
||||
{
|
||||
if (node==NULL)
|
||||
{
|
||||
// Funkcia na spustenie znalostného systému
|
||||
void run_knowledge_system(struct tree* node) {
|
||||
if (node == NULL) {
|
||||
return;
|
||||
}
|
||||
if (node->value[0]=='*')
|
||||
{
|
||||
printf("%s\n", node->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s\n", node->value);
|
||||
}
|
||||
if (node->value[0]=='*')
|
||||
{
|
||||
|
||||
// Vypíš otázku/odpoveď
|
||||
printf("%s\n", node->value);
|
||||
|
||||
// Ak je to listový uzol (odpoveď), skonči
|
||||
if (node->value[0] == '*') {
|
||||
return;
|
||||
}
|
||||
if (node->left!=NULL || node->right!=NULL)
|
||||
{
|
||||
|
||||
// Ak má potomkov, pokračuj podľa odpovede používateľa
|
||||
if (node->left != NULL || node->right != NULL) {
|
||||
char answer[10];
|
||||
if (fgets(answer, sizeof(answer), stdin)==NULL)
|
||||
{
|
||||
|
||||
// Načítaj odpoveď používateľa
|
||||
if (fgets(answer, sizeof(answer), stdin) == NULL) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
if (answer[0]=='a')
|
||||
{
|
||||
|
||||
// Spracuj odpoveď
|
||||
if (answer[0] == 'a') {
|
||||
run_knowledge_system(node->left);
|
||||
}
|
||||
else if (answer[0]=='n')
|
||||
{
|
||||
} else if (answer[0] == 'n') {
|
||||
run_knowledge_system(node->right);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("Nespravny vstup.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
printf("Koniec\n");
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct tree* knowledge_base=read_tree();
|
||||
int main() {
|
||||
// Načítanie bázy pravidiel
|
||||
struct tree* knowledge_base = read_tree();
|
||||
|
||||
// Overenie, či nasleduje prázdny riadok
|
||||
char empty_line[SIZE];
|
||||
if (fgets(empty_line, SIZE, stdin)==NULL || empty_line[0]!='\n')
|
||||
{
|
||||
if (fgets(empty_line, SIZE, stdin) == NULL || empty_line[0] != '\n') {
|
||||
printf("Nespravna baza pravidiel.\n");
|
||||
if (knowledge_base!=NULL)
|
||||
{
|
||||
if (knowledge_base != NULL) {
|
||||
destroy_tree(knowledge_base);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (knowledge_base==NULL)
|
||||
{
|
||||
|
||||
// Overenie platnosti bázy pravidiel
|
||||
if (knowledge_base == NULL) {
|
||||
printf("Nespravna baza pravidiel.\n");
|
||||
return 1;
|
||||
}
|
||||
int answer_count=count_answers(knowledge_base);
|
||||
printf("Expert_x_bufetu_to_vie.\n");
|
||||
printf("Pozna_%d_druhov_ovocia_a_zeleniny.\n", answer_count);
|
||||
printf("Odpovedajte_'a'_pre_prvu_moznost_alebo_'n'_pre_druhu_moznost.\n");
|
||||
|
||||
// Spočítanie a výpis počtu tovarov
|
||||
int answer_count = count_answers(knowledge_base);
|
||||
printf("Expert z bufetu to vie.\n"); // Medzery namiesto podtržníkov
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
// Spustenie znalostného systému
|
||||
run_knowledge_system(knowledge_base);
|
||||
|
||||
// Vyčistenie pamäte
|
||||
destroy_tree(knowledge_base);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user