【发布时间】:2014-03-27 12:18:20
【问题描述】:
这是我用 C 编写的代码。
void BFS()
{
int index[4][2];
int p=10,i,j,x,y;
int chld=-1;
int cnt=1;
int size=sizeof(NODE);
char ch='n';
qstart=NULL;
lstart=NULL;
NODE *ptr,*tmp;
NODE* broot=(NODE*)malloc(size);
NODE* bgoal=(NODE*)malloc(size);
memcpy(broot,root,size);
memcpy(bgoal,goal,size);
push_queue(broot);
//---------------------------------------------------------------
//This scanf does not work well, don't read from input stream or console
//---------------------------------------------------------------
printf("Want to see the nodes\t[y/n]\n");
scanf("%c",&ch);
printf("\n\nTOTAL NODE COUNT ");
//---------------------------------------------------------------
while(p>0)
{
if((ptr=pop_queue())!=NULL)
{
positions(ptr,index,&x,&y);
if(ch=='y' || ch=='Y')
{
printf("\t\t\tParent\n");
print_node(ptr);
}
i=0; j=0; chld=-1;
for(i=0;i<4;i++)
if(index[i][0]!=-1)
if((tmp=node_exchange(ptr,broot,index[i][0],index[i][1],x,y))!=NULL)
{
printf("%d",cnt);
j=cnt;
cnt++;
tmp->parent=ptr;
ptr->child[++chld]=tmp;
if(!memcmp(tmp,bgoal,9*sizeof(int)))
{
trace_path(tmp);
printf("Total Nodes Skipped : %d",TOTAL_SKIP);
free_list();
return ;
}
push_queue(tmp);
push_list(tmp);
while(j>0)
{
j=j/10;
printf("\b");
}
}
if(ch=='y' || ch=='Y')
print_child(ptr,chld);
for(i=chld+1;i<4;i++)
ptr->child[i]=NULL;
}
else{
printf("WARNING: popped null\n");
break;
}
// p--;
}
qstart=NULL;
}
这是我的程序的一个功能。 scanf 调用(由 cmets 在代码内标记)不会从控制台读取。之后它继续执行所有代码,并且不等待它应该做的输入。我正在使用 gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 和 gnome 终端。
示例输出为: 请进入初始状态: 初始节点
0 1 2
3 4 5
6 7 8
请输入目标节点: 目标节点
8 7 6
5 4 3
2 1 0
想查看节点 [y/n]
节点总数 885^C
相信我,我不知道为什么会这样进一步。
【问题讨论】:
-
将
scanf("%c",&ch);更改为scanf(" %c",&ch);
标签: c linux gcc terminal scanf