#1
by 油田 » 8年前
二分木のプログラムについて途中まで作ったのですがここから先がうまくいかないので質問させてください。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char sentence[100];
struct node *yes;
struct node *no;
};
void error(char *s);
struct node *make_node();
void questionTree(struct node *node);
int main(void){
struct node *node;
struct node *node_head;
char a[]="鼻が長い";
char b[]="ゾウ";
char c[]="ウマ";
char d='\n';
char dummy;
node=make_node();
node->yes=make_node();
node->no=make_node();
node_head=node;
strcpy(node->sentence,a);
strcpy(node->yes->sentence,b);
strcpy(node->no->sentence,c);
while(d!='n'){
questionTree(node);
printf("もう一度やりますか?");
scanf("%c",&d);
scanf("%c",&dummy);
}
return(0);
}
struct node *make_node(){
struct node *p;
if((p=(struct node*)malloc(sizeof(struct node)))==NULL)
error("メモリが足りません");
memset(p->sentence,'\0', 100);
p->yes =NULL;
p->no =NULL;
return(p);
}
void questionTree(struct node *node){
char m;
char b;
char dummy;
char c[100];
printf("それは%sですか?\n",node->sentence);
scanf("%c",&m);
scanf("%c",&dummy);
if(m=='y'){
if(node->yes!=NULL)
questionTree(node->yes);
else{
printf("\n");
}
}
else{
if(node->no!=NULL)
questionTree(node->no);
else{
printf("その動物の特徴はなんですか?\n");
scanf("%s",&c);
node->no=make_node();
node=node->no;
strcpy(node->sentence,c);
printf("その動物は何ですか?\n");
scanf("%s",&c);
node->yes=make_node();
node=node->yes;
strcpy(node->sentence,c);
}
}
}
void error(char *s){
fprintf(stderr, s);
exit(1);
}
一度新しい動物を入力する方向に分岐すると、そこから先なにを入力しても新しい動物を入力する方向にしか進めなくなってしまいます。どうしたら改善できるでしょうか?よろしくおねがいします
二分木のプログラムについて途中まで作ったのですがここから先がうまくいかないので質問させてください。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char sentence[100];
struct node *yes;
struct node *no;
};
void error(char *s);
struct node *make_node();
void questionTree(struct node *node);
int main(void){
struct node *node;
struct node *node_head;
char a[]="鼻が長い";
char b[]="ゾウ";
char c[]="ウマ";
char d='\n';
char dummy;
node=make_node();
node->yes=make_node();
node->no=make_node();
node_head=node;
strcpy(node->sentence,a);
strcpy(node->yes->sentence,b);
strcpy(node->no->sentence,c);
while(d!='n'){
questionTree(node);
printf("もう一度やりますか?");
scanf("%c",&d);
scanf("%c",&dummy);
}
return(0);
}
struct node *make_node(){
struct node *p;
if((p=(struct node*)malloc(sizeof(struct node)))==NULL)
error("メモリが足りません");
memset(p->sentence,'\0', 100);
p->yes =NULL;
p->no =NULL;
return(p);
}
void questionTree(struct node *node){
char m;
char b;
char dummy;
char c[100];
printf("それは%sですか?\n",node->sentence);
scanf("%c",&m);
scanf("%c",&dummy);
if(m=='y'){
if(node->yes!=NULL)
questionTree(node->yes);
else{
printf("\n");
}
}
else{
if(node->no!=NULL)
questionTree(node->no);
else{
printf("その動物の特徴はなんですか?\n");
scanf("%s",&c);
node->no=make_node();
node=node->no;
strcpy(node->sentence,c);
printf("その動物は何ですか?\n");
scanf("%s",&c);
node->yes=make_node();
node=node->yes;
strcpy(node->sentence,c);
}
}
}
void error(char *s){
fprintf(stderr, s);
exit(1);
}
一度新しい動物を入力する方向に分岐すると、そこから先なにを入力しても新しい動物を入力する方向にしか進めなくなってしまいます。どうしたら改善できるでしょうか?よろしくおねがいします