2012-11-28 105 views
0

你好我有一個小的二叉樹程序在c,我正在執行一個操作後,主菜單被打印兩次,然後允許下一個輸入被掃描。任何想法來防止這種情況?Switch Case Default case C quiestion

#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; */ 
} 

菜單打印出具有無效選項輸入即使什麼也沒有inputed 所以輸出看起來像:

### 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調用的緩衝區中。

+0

這是我實施「新行」案例 案例6: case'\ n': printf(「\ n」); break;' –

+0

我將scanf從'c'更改爲's',似乎已經解決了問題,謝謝:) –

+0

@VOr:不,它更糟糕。這可能會覆蓋一些其他堆棧變量,具體取決於順序或聲明,字大小,編譯器,實際輸入等。例如:如果您執行類似'char f,g = 50; scanf(「%s」,&f);',並且輸入一個或多個字符,g'值將保持爲'50'的可能性很小。 – netcoder