This commit is contained in:
Oleksandr Hryshchenko 2021-11-25 23:45:39 +01:00
parent ee0d559f12
commit 44bd68c38c
2 changed files with 348 additions and 0 deletions

174
cv8/main.c Normal file
View File

@ -0,0 +1,174 @@
#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 product a name of the product 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* productAppend(struct binary* myTree, char* product){
if(!myTree || (myTree->right != NULL && myTree->left != NULL) && (myTree->left->value[0] == '*' && myTree->right->value[0] == '*'))
return NULL;
struct binary* itrNode = myTree;
while(itrNode->left != NULL){
if(itrNode->left->value[0] != '*')
itrNode = (!productAppend(itrNode->left, product)) ? itrNode : productAppend(itrNode->left, product);
if(endingFound || !itrNode->right || !itrNode->left)
break;
if(itrNode->right->value[0] != '*')
itrNode = (!productAppend(itrNode->right, product)) ? itrNode : productAppend(itrNode->right, product);
}
if(!endingFound && !itrNode->left) {
itrNode->left = createBinary(product);
endingFound = true;
return itrNode;
}
else if(!endingFound && !itrNode->right){
itrNode->right = createBinary(product);
endingFound = true;
return itrNode;
}
return NULL;
}
/**
*
* @param myTree a pointer to the binary tree that the new question should be appended to.
* @param question the text of a question to be added as a new node.
* @return a pointer to the newly created node in case function runs successfully, NULL otherwise.
*/
struct binary* questionAppend(struct binary* myTree, char* question){
struct binary* itrNode1 = myTree;
struct binary* itrNode2 = myTree;
while(itrNode1->left && itrNode1->right && itrNode2->left && itrNode2->right){
if(itrNode1->left->value[0] != '*')
itrNode1 = questionAppend(itrNode1->left, question);
if(endingFound)
break;
if(itrNode2->right->value[0] != '*')
itrNode2 = questionAppend(itrNode2->right, question);
}
if(!itrNode1->left) {
itrNode1->left = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode1->right){
itrNode1->right = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode2->left && !endingFound){
itrNode2->left = createBinary(question);
endingFound = true;
return itrNode2;
}
else if(!itrNode2->right && !endingFound){
itrNode2->right = createBinary(question);
endingFound = true;
return itrNode2;
}
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)
productAppend(itrNode, usrIn);
endingFound = false;
}
}
printf("Expert z bufetu to vie.\n");
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;
}

174
cv8/program.c Normal file
View File

@ -0,0 +1,174 @@
#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 product a name of the product 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* productAppend(struct binary* myTree, char* product){
if(!myTree || (myTree->right != NULL && myTree->left != NULL) && (myTree->left->value[0] == '*' && myTree->right->value[0] == '*'))
return NULL;
struct binary* itrNode = myTree;
while(itrNode->left != NULL){
if(itrNode->left->value[0] != '*')
itrNode = (!productAppend(itrNode->left, product)) ? itrNode : productAppend(itrNode->left, product);
if(endingFound || !itrNode->right || !itrNode->left)
break;
if(itrNode->right->value[0] != '*')
itrNode = (!productAppend(itrNode->right, product)) ? itrNode : productAppend(itrNode->right, product);
}
if(!endingFound && !itrNode->left) {
itrNode->left = createBinary(product);
endingFound = true;
return itrNode;
}
else if(!endingFound && !itrNode->right){
itrNode->right = createBinary(product);
endingFound = true;
return itrNode;
}
return NULL;
}
/**
*
* @param myTree a pointer to the binary tree that the new question should be appended to.
* @param question the text of a question to be added as a new node.
* @return a pointer to the newly created node in case function runs successfully, NULL otherwise.
*/
struct binary* questionAppend(struct binary* myTree, char* question){
struct binary* itrNode1 = myTree;
struct binary* itrNode2 = myTree;
while(itrNode1->left && itrNode1->right && itrNode2->left && itrNode2->right){
if(itrNode1->left->value[0] != '*')
itrNode1 = questionAppend(itrNode1->left, question);
if(endingFound)
break;
if(itrNode2->right->value[0] != '*')
itrNode2 = questionAppend(itrNode2->right, question);
}
if(!itrNode1->left) {
itrNode1->left = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode1->right){
itrNode1->right = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode2->left && !endingFound){
itrNode2->left = createBinary(question);
endingFound = true;
return itrNode2;
}
else if(!itrNode2->right && !endingFound){
itrNode2->right = createBinary(question);
endingFound = true;
return itrNode2;
}
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)
productAppend(itrNode, usrIn);
endingFound = false;
}
}
printf("Expert z bufetu to vie.\n");
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;
}