0

您好,我正在使用 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 
4

1 回答 1

3

scanf如果“未输入任何内容”,将使用上述代码填充opn换行符 ( )。\n由于您没有case为此,它将默认处理。

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

于 2012-11-28T22:06:31.537 回答