你好我在c中有一个小的二叉树程序正在处理,在执行一个操作后,主菜单被打印两次,然后才允许扫描下一个输入。有什么想法可以防止这种情况吗?
#include<stdio.h>
#include<stdlib.h>
struct btree{
int id, val;
struct btree *left, *right;
};
typedef struct btree node;
void myparent(node *tree, int myid, node **parent){
if(tree->id==(myid/2))
*parent = tree;
if(tree->left)
myparent(tree->left, myid, parent);
if(tree->right)
myparent(tree->right, myid, parent);
}
void insert(node **tree, node *item){
node *parent;
if(item->id==1)
*tree=item;
else{
myparent(*tree, item->id,&parent);
if((item->id)%2)
parent->right=item;
else
parent->left=item;
}
}
void preorder_print(node *tree){
if (tree!=NULL){
printf(" %d\n",tree->val);
printf("\t");
preorder_print(tree->left);
printf("\n");
preorder_print(tree->right);
}
}
void inorder_print(node *tree){
if (tree!= NULL){
inorder_print(tree->left);
printf("%d ", tree->val);
inorder_print(tree->right);
}
}
void printout(node *tree){
if(tree->left)
printout(tree->left);
printf("%d\n", tree->val);
if(tree->right)
printout(tree->right);
}
main(){
char opn;
node *root, *curr;
int idcount=1, inp=0;
root=NULL;
//input 9999 is the exit point
do{
printf("\n ### Binary Tree Operations ### \n\n");
printf("\n Enter one of the following Operations:\n a- Add\n e- Empty\n i- List IN ORDER\n r- List PRE-ORDER\n p- List POST ORDER\n q- Quit\n");
scanf("%c", &opn);
switch (opn) {
case 1:
case 'a':
case 'A':
printf("\nEnter a Node>");
scanf("%d",&inp);
curr=(node*)malloc(sizeof(node));
curr->val=inp;
curr->id=idcount++;
curr->left = curr->right = NULL;
insert(&root, curr);
printf("The number was inserted\n");
break;
case 2:
case 'e':
case 'E':
printf("The value was deleted\n");
break;
case 3:
case 'i':
case 'I':
printf("IN ORDER Tree: \n");
inorder_print(root);
break;
case 4:
case 'r':
case 'R':
printf("PRE ORDER Tree: \n");
preorder_print (root);
break;
case 5:
case 'p':
case 'P':
printf("POST ORDER Tree: \n");
printout(root);
break;
case 6:
case 'q':
case 'Q':
printf("\n\n Terminating \n\n");
exit(1);
default:
printf("Invalid Opition \n \n");
break;
}
printf("\n Press any key to continue . . .");
} while (opn != 'q');
/* printf("\n Entered Binary Tree is \n");
printout(root);
return 0; */
}
即使没有输入任何内容,也会打印出输入无效选项的菜单 所以输出看起来像:
### Binary Tree Operations ###
Enter one of the following Operations:
a- Add
e- Empty
i- List IN ORDER
r- List PRE-ORDER
p- List POST ORDER
q- Quit
a
Enter a Node>1
The number was inserted
Press any key to continue . . .
### Binary Tree Operations ###
Enter one of the following Operations:
a- Add
e- Empty
i- List IN ORDER
r- List PRE-ORDER
p- List POST ORDER
q- Quit
Invalid Opition
Press any key to continue . . .
### Binary Tree Operations ###
Enter one of the following Operations:
a- Add
e- Empty
i- List IN ORDER
r- List PRE-ORDER
p- List POST ORDER
q- Quit
i
IN ORDER Tree:
2 4 1 3 5
Press any key to continue . . .
### Binary Tree Operations ###
Enter one of the following Operations:
a- Add
e- Empty
i- List IN ORDER
r- List PRE-ORDER
p- List POST ORDER
q- Quit
Invalid Opition
Press any key to continue . . .
### Binary Tree Operations ###
Enter one of the following Operations:
a- Add
e- Empty
i- List IN ORDER
r- List PRE-ORDER
p- List POST ORDER
q- Quit
q
Terminating
答案 0 :(得分:3)
scanf
将使用上述代码填充opn
换行符(\n
)。由于您没有case
,因此默认情况下将对其进行处理。
即使输入了某个字符,换行也会留在缓冲区中,以便进行下一次scanf
通话。