2013-11-21 189 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