C 如何减少if-else-if语句并使其更好?

C 如何减少if-else-if语句并使其更好?,c,C,我必须在代码中所示的特定范围内调整浮点型剩余计数,这可以正常工作,但问题是,如果计数是10或20或30,将有许多if-else-if条件,这是非常低效的 下面的示例代码用于调整最多5个计数 有什么建议可以改进吗 这是: for(i = 0; i < 12; i++) { RemainingCount[i] = (( (float) CurrentMaterialWeight[i] * (float)TotalMaterialCount[i]) / ( (float) Tot

我必须在代码中所示的特定范围内调整浮点型剩余计数,这可以正常工作,但问题是,如果计数是10或20或30,将有许多if-else-if条件,这是非常低效的

下面的示例代码用于调整最多5个计数

有什么建议可以改进吗

这是:

  for(i = 0; i < 12; i++)
  {
    RemainingCount[i] = (( (float) CurrentMaterialWeight[i] * (float)TotalMaterialCount[i]) / ( (float) TotalMaterialWeight[i]));   

    if (RemainingCount[i] <= 0.3)
     {
        AdjustedRemainingCount[i] = 0;
     }

    else if ( RemainingCount[i] > 0.50 &&  RemainingCount[i] <= 1.50)
    {
        AdjustedRemainingCount[i] = 1;
    }

    else if ( RemainingCount[i] > 1.50 &&  RemainingCount[i] <= 2.50)
    {
        AdjustedRemainingCount[i] = 2;
    }

   else if ( RemainingCount[i] > 2.50 &&  RemainingCount[i] <= 3.50)
    {
            AdjustedRemainingCount[i] = 3;
    }

    else if ( RemainingCount[i] > 3.50 &&  RemainingCount[i] <= 4.50)
    {
            AdjustedRemainingCount[i] = 4;
    }
}
    So on.....
(i=0;i<12;i++)的

{
剩余计数[i]=((浮动)当前物料重量[i]*(浮动)物料总计数[i])/(浮动)物料总重量[i]);

if(RemainingCount[i]0.50&&RemainingCount[i]1.50&&RemainingCount[i]2.50&&RemainingCount[i]3.50&&RemainingCount[i]所有if条件都可以用一轮函数替换。

取整应该是一个技巧。唯一的问题是,您的调整与内置取整略有不同,因为您的条件如
if(…,f我已经为您提供了代码逻辑,可以减少if-else语句。请先了解逻辑。您可以根据自己的需要修改代码

int left, right;   // declare two integer

for()
{
RemainingCount[i] = (( (float) CurrentMaterialWeight[i] * (float)TotalMaterialCount[i]) / ( (float) TotalMaterialWeight[i]));   

 // divide it into two parts RemainingCount[i]= 20.5511;

 char buffer[50];
 sprintf(buffer, "%lf", value);
 sscanf(buffer, "%d.%d", &left, &right);
// use what ever way to divide the number
  // left = 20  right = 5511

  float leftplus = left + 0.5;
  float leftminus = left - 0.5;

     // LEFTplus = 20.5
     // LEFTminus = 19.5

  if ( your_number <= leftsplus && your_number > leftminus  )
  {   //     20.55 <= 20.5      &&       20.55 > 19.5
    AdjustedRemainingCount[i] = LEFT;
   }else{ //  20.55
          AdjustedRemainingCount[i] = left +1;  // 21
             }


  }
int left,right;//声明两个整数
for()
{
剩余计数[i]=((浮动)当前物料重量[i]*(浮动)物料总计数[i])/(浮动)物料总重量[i]);
//将其分为两部分,剩余计数[i]=20.5511;
字符缓冲区[50];
sprintf(缓冲区,“%lf”,值);
sscanf(缓冲区、%d.%d、&左侧和右侧);
//用什么方法来除数
//左=20右=5511
float leftplus=左+0.5;
浮动左减=左-0.5;
//LEFTplus=20.5
//左负=19.5
如果(您的_编号左减)
{   //     20.55  19.5
AdjustedRemainingCount[i]=左侧;
}其他{//20.55
AdjustedRemainingCount[i]=左+1;//21
}
}

为什么要从0.3跳到0.5?颠倒顺序,去掉
&&
的右边。你继续测试你已经知道的。或者只使用
round
。如果是为了某种不能使用预先生成的round函数的练习,你可以使用截断规则。
float some随机float=4.55f;int truncatedNo=someRandomFloat;float diff=someRandomFloat-truncatedNo;
在编写可伸缩的代码时,应该是不言自明的。
0.2 adjustment: 0
0.3 adjustment: 0
0.31 adjustment: 0
0.5 adjustment: 0
1.5 adjustment: 1
1.51 adjustment: 2
2.5 adjustment: 2
2.51 adjustment: 3
3.5 adjustment: 3
int left, right;   // declare two integer

for()
{
RemainingCount[i] = (( (float) CurrentMaterialWeight[i] * (float)TotalMaterialCount[i]) / ( (float) TotalMaterialWeight[i]));   

 // divide it into two parts RemainingCount[i]= 20.5511;

 char buffer[50];
 sprintf(buffer, "%lf", value);
 sscanf(buffer, "%d.%d", &left, &right);
// use what ever way to divide the number
  // left = 20  right = 5511

  float leftplus = left + 0.5;
  float leftminus = left - 0.5;

     // LEFTplus = 20.5
     // LEFTminus = 19.5

  if ( your_number <= leftsplus && your_number > leftminus  )
  {   //     20.55 <= 20.5      &&       20.55 > 19.5
    AdjustedRemainingCount[i] = LEFT;
   }else{ //  20.55
          AdjustedRemainingCount[i] = left +1;  // 21
             }


  }