usaa21/cv8/program.c
2021-11-26 01:09:14 +00:00

144 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
/**
* A structure, representing a single node (fruit/vegetable) and it's children nodes
*/
struct binary{
char value[120];
struct binary* left;
struct binary* right;
};
/**
* A global boolean variable that shows if the end of the binary tree was found.
* It's a very important one, as there's a constant need of such information
*/
bool endingFound = false;
/**
* This function is used to create and initialize a new binary tree
* @param name a name of the product that will become the first node of a newly created binary tree.
* @return a pointer to the newly created binary tree.
*/
struct binary* createBinary(char* name){
struct binary* newTree = (struct binary*)calloc(1, sizeof(struct binary));
strcpy(newTree->value, name);
newTree->left = NULL;
newTree->right = NULL;
return newTree;
}
/**
* The function recursively iterated through the whole binary tree until it finds a place to add new node to. Once the
* place is found, it created a new node with a given value.
* @param myTree a pointer to the binary tree that the new node should be appended to.
* @param item a name of the item that will be added as a new node.
* @return NULL if the node was not yet added, pointer to the new node if the node was added.
*/
struct binary* itemAppend(struct binary* myTree, char* item){
if(!myTree || ((myTree->right != NULL && myTree->left != NULL) && (myTree->left->value[0] == '*' && myTree->right->value[0] == '*')))
return NULL;
if(!strcmp(item, "Je to zelene?"))
printf("ti");
struct binary* itrNode = myTree;
while(itrNode->left != NULL){
if(itrNode->left->value[0] != '*')
itrNode = (!itemAppend(itrNode->left, item)) ? itrNode : itemAppend(itrNode->left, item);
if(endingFound || !itrNode->right || !itrNode->left)
break;
if(itrNode->right->value[0] != '*')
itrNode = (!itemAppend(itrNode->right, item)) ? itrNode : itemAppend(itrNode->right, item);
if(!itrNode)
return NULL;
if((itrNode->left->value[0] == '*' || !itemAppend(itrNode->left, item)) && (itrNode->left->value[0] == '*' || !itemAppend(itrNode->left, item)))
break;
}
if(!endingFound && !itrNode->left) {
itrNode->left = createBinary(item);
endingFound = true;
return itrNode;
}
else if(!endingFound && !itrNode->right){
itrNode->right = createBinary(item);
endingFound = true;
return itrNode;
}
return NULL;
}
int main() {
char* usrIn = (char*)calloc(120, sizeof(char));
struct binary* binaryTree;
struct binary* itrNode;
char* usrResponse = (char*)calloc(10, sizeof(char));
int inputsAmount = 0;
for(int i = 0; fgets(usrIn, 120, stdin); i++){
if(!usrIn || !strcmp(usrIn, "") || !strcmp(usrIn, " ") || !strcmp(usrIn, "\n"))
break;
usrIn[strlen(usrIn) - 1] = '\0';
if(i == 0){
binaryTree = createBinary(usrIn);
itrNode = binaryTree;
}
else{
if(usrIn[0] == '*')
inputsAmount++;
if(strlen(usrIn) > 1)
itemAppend(itrNode, usrIn);
endingFound = false;
}
}
printf("Expert z bufetu to vie.\n");
if(inputsAmount == 0){
printf("Chybna databaza\n");
return 0;
}
printf("Pozna %d druhov ovocia a zeleniny.\n", inputsAmount);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
while(binaryTree->value[0] != '*'){
printf("%s\n", binaryTree->value);
scanf("%s", usrResponse);
if(strlen(usrResponse) == 0){
printf("Koniec vstupu\n");
return 0;
}
if (strlen(usrResponse) == 1) {
if (tolower(usrResponse[0]) == 'a')
binaryTree = binaryTree->left;
else if (tolower(usrResponse[0] == 'n'))
binaryTree = binaryTree->right;
else {
printf("Nerozumiem\n");
return 0;
}
}
else{
printf("Nerozumiem\n");
return 0;
}
}
printf("%s\n", binaryTree->value);
printf("Koniec\n");
return 0;
}