usaa24/cv7/program.c

146 lines
2.4 KiB
C
Raw Permalink Normal View History

2024-11-11 20:35:40 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
2024-11-11 23:11:33 +00:00
2024-11-11 20:35:40 +00:00
#define SIZE 200
struct tree
{
2024-11-11 22:13:27 +00:00
2024-11-11 20:35:40 +00:00
char value[SIZE];
2024-11-11 22:19:11 +00:00
2024-11-11 20:35:40 +00:00
struct tree *left, *right;
};
2024-11-11 21:30:26 +00:00
struct tree* read_tree()
2024-11-11 20:35:40 +00:00
{
char buffer[SIZE];
memset(buffer,0,SIZE);
2024-11-11 22:13:27 +00:00
char* r = fgets(buffer,SIZE,stdin);
2024-11-11 22:28:18 +00:00
2024-11-11 23:03:54 +00:00
if(r==NULL) return NULL;
2024-11-11 22:13:27 +00:00
if(buffer[0]=='\n')
return NULL;
2024-11-11 21:10:54 +00:00
struct tree* node = (struct tree*)calloc(1,sizeof(struct tree));
2024-11-11 20:35:40 +00:00
memcpy(node->value,buffer,SIZE);
2024-11-11 22:28:18 +00:00
2024-11-11 21:10:54 +00:00
node->left=NULL;
node->right=NULL;
2024-11-11 20:35:40 +00:00
if(node->value[0]!='*')
2024-11-11 22:19:11 +00:00
{
node->left=read_tree(node->left);
2024-11-11 21:30:26 +00:00
node->right=read_tree(node->right);
2024-11-11 20:35:40 +00:00
}
return node;
}
void print_tree(struct tree* tree,int offset){
for (int i = 0; i < offset; i++){
2024-11-11 21:10:54 +00:00
printf(".");
2024-11-11 22:19:11 +00:00
}
2024-11-11 21:30:26 +00:00
if(tree)
printf("%s",tree->value);
2024-11-11 20:35:40 +00:00
if (tree->left){
print_tree(tree->left,offset +3);
}
2024-11-11 21:10:54 +00:00
if (tree->right){
print_tree(tree->right,offset +3);
2024-11-11 20:35:40 +00:00
}
}
2024-11-11 22:51:03 +00:00
int kkk(struct tree* tree, int x)
{
if (!tree)
{
return x;
}
x += (tree->value[0] == '*');
x = kkk(tree->left, x);
x = kkk(tree->right, x);
return x;
}
2024-11-11 22:13:27 +00:00
void destroy_tree(struct tree* tree){
if(tree==NULL) return;
destroy_tree(tree->left);
destroy_tree(tree->right);
free(tree);
}
2024-11-11 20:35:40 +00:00
int main(void)
{
2024-11-11 22:56:57 +00:00
struct tree *tr=NULL;
2024-11-11 23:11:33 +00:00
char rrrr[333];
2024-11-11 21:10:54 +00:00
2024-11-11 21:30:26 +00:00
tr=read_tree();
2024-11-11 22:56:57 +00:00
printf("Expert z bufetu to vie.\n");
2024-11-11 23:11:33 +00:00
fgets(rrrr, 333, stdin);
if(tr==NULL||rrrr[0]!='\n')
2024-11-11 22:56:57 +00:00
{
printf("Chybna databaza\n");
return 0;
}
2024-11-11 22:51:03 +00:00
int kk;
2024-11-11 21:56:32 +00:00
char t;
2024-11-11 21:30:26 +00:00
2024-11-11 23:11:33 +00:00
kk=kkk(tr,0);
2024-11-11 22:56:57 +00:00
2024-11-11 22:51:03 +00:00
printf("Pozna %d druhov ovocia a zeleniny.\n", kk);
2024-11-11 21:45:58 +00:00
struct tree *p=tr;
2024-11-11 22:03:00 +00:00
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
2024-11-11 21:45:58 +00:00
do
2024-11-11 22:03:00 +00:00
{
2024-11-11 21:45:58 +00:00
printf("%s",p->value);
if(p->value[0]=='*')
2024-11-11 22:28:18 +00:00
break;
do{
2024-11-11 22:19:11 +00:00
t=fgetc(stdin);
if(t=='a')
2024-11-11 21:56:32 +00:00
{
2024-11-11 22:28:18 +00:00
p=p->left; break;
2024-11-11 22:19:11 +00:00
}
else if(t=='n')
{
2024-11-11 22:28:18 +00:00
p=p->right; break;
2024-11-11 22:19:11 +00:00
}
2024-11-11 22:39:13 +00:00
else if(t==-1)
{
printf("Koniec vstupu\n");
break;
}
2024-11-11 22:25:40 +00:00
else if(t!='\n')
2024-11-11 22:19:11 +00:00
{
printf("Nerozumiem\n");
2024-11-11 22:28:18 +00:00
t=44;
break;
2024-11-11 22:19:11 +00:00
}
2024-11-11 22:39:13 +00:00
2024-11-11 22:28:18 +00:00
}while(1);
2024-11-11 22:41:12 +00:00
}while(t!=44&&t!=-1);
2024-11-11 22:28:18 +00:00
2024-11-11 22:39:13 +00:00
if(t!=44&&t!=-1) printf("Koniec\n");
2024-11-11 22:13:27 +00:00
destroy_tree(tr);
tr=NULL;
2024-11-11 22:28:18 +00:00
2024-11-11 20:35:40 +00:00
return 0;
2024-11-11 21:45:58 +00:00
}
2024-11-11 22:19:11 +00:00
2024-11-11 22:39:13 +00:00