cviko8
This commit is contained in:
parent
35094fbee3
commit
8641a66494
96
cv8/program.c
Normal file
96
cv8/program.c
Normal file
@ -0,0 +1,96 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#define SIZE 100
|
||||
|
||||
struct tree{
|
||||
char question[SIZE];
|
||||
struct tree* left;
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
struct tree* read_tree();
|
||||
void system_execute(struct tree* root);
|
||||
int get_input(char *string);
|
||||
int type_count(struct tree* root);
|
||||
|
||||
|
||||
int main(){
|
||||
struct tree* root = NULL;
|
||||
root = read_tree();
|
||||
getchar();
|
||||
|
||||
puts("Expert z bufetu to vie.");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n",type_count(root));
|
||||
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
|
||||
|
||||
system_execute(root);
|
||||
|
||||
getchar();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct tree* read_tree(){
|
||||
char buffer[SIZE];
|
||||
memset(buffer,0,SIZE);
|
||||
|
||||
int flag = get_input(buffer);
|
||||
if(flag==-1){
|
||||
puts("EOF");
|
||||
}
|
||||
|
||||
struct tree* node = calloc(1,sizeof(struct tree));
|
||||
memcpy(node->question,buffer,SIZE);
|
||||
// Ak je nacitany riadok listovy uzol ,tak netreba robit nic
|
||||
// inak rekurzivne nacitanie laveho syna
|
||||
// a rekurzivne nacitanie praveho syna
|
||||
if(node->question[0]!='*'){
|
||||
node->left = read_tree();
|
||||
node->right = read_tree();
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
int get_input(char *string){
|
||||
char* r = fgets(string,SIZE,stdin);
|
||||
if(r==NULL){
|
||||
return -1;//EOF
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void system_execute(struct tree* root){
|
||||
printf("%s\n",root->question);
|
||||
char *resp = calloc(SIZE,sizeof(char));
|
||||
|
||||
int flag = get_input(resp);
|
||||
if(flag==-1){
|
||||
puts("Koniec vstupu");
|
||||
return;
|
||||
}
|
||||
if(resp[0]=='a'){
|
||||
system_execute(root->left);
|
||||
}else if(resp[0]=='n'){
|
||||
system_execute(root->right);
|
||||
}else{
|
||||
puts("Nerozumiem");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int type_count(struct tree* root){//https://www.youtube.com/watch?v=Uze4GgUj3Fs&feature=emb_logo
|
||||
if(root==NULL){
|
||||
return 0;
|
||||
}
|
||||
if(root->question[0]=='*'){
|
||||
return 1;
|
||||
}
|
||||
int count= type_count(root->left)+type_count(root->right);
|
||||
return count;
|
||||
}
|
Loading…
Reference in New Issue
Block a user