2013-11-21 242 views
0

我有一段看起來相當醜陋的代碼。我試圖比較一個十六進制的12位數據,看是否是0xff8到0xfff。如果代碼適合所述範圍內的任何數字,則該代碼返回true。有什麼建議麼?謝謝!優化代碼多個if else語句

/******************************************************************/ 
/* summary: Checks if the cluster is the last cluster in a file. */ 
/* return: returns 1 if true and 0 if false      */ 
/******************************************************************/ 
int lastCluster(unsigned int cluster){ 
    /*Compares the values of the cluster. The cluster is the last cluster in a 
    file if the cluster has a value of 0xff8-0xfff.*/ 
    if(cluster == 0xff8){ 
     return (1); 
    } 
    else if(cluster == 0xff9){ 
     return (1); 
    } 
    else if(cluster == 0xffa){ 
     return (1); 
    } 
    else if(cluster == 0xffb){ 
     return (1); 
    } 
    else if(cluster == 0xffc){ 
     return (1); 
    } 
    else if(cluster == 0xffd){ 
     return (1); 
    } 
    else if(cluster == 0xffe){ 
     return (1); 
    } 
    else if(cluster == 0xfff){ 
     return (1); 
    } 
    else{ 
     return (0); 
    } 
} 

回答

4

您可以將其組合到一個單一的測試

if (cluster>=0xff8 && cluster<=0xfff) 
    return 1; 
return 0; 
+0

哇,非常感謝你! – user2989964