gpt4 book ai didi

c - Switch Case Default case C题

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:15 24 4
gpt4 key购买 nike

你好,我正在用 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

最佳答案

如果“未输入任何内容”,

scanf 将使用带有上述代码的换行符 (\n) 填充 opn。因为你没有一个 case ,它会被默认处理。

即使输入了一个字符,换行符也会保留在缓冲区中以供下一次 scanf 调用。

关于c - Switch Case Default case C题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com