0

こんにちは、私が取り組んでいる c の小さなバイナリ ツリー プログラムがあり、操作が実行された後、次の入力をスキャンできるようにする前に、メイン メニューが 2 回印刷されます。これを防ぐためのアイデアはありますか?

#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 に答える