优化这段代码 if(i >= (RUN_INNER_PARA_SIZE -1)) break; memset(szVal, 0, sizeof(szVal)); isDraw = (not == 2 && (draw == (i+1))); val = get_RunParaInfo_val(UNIT_GAP_RT1064,gapid,i); if( ((i>=RUN_INNER_EN_OL)&&(i<=RUN_INNER_EN_OVL)) || ((i>=RUN_INNER_ENW_OC)&&(i<=RUN_INNER_ENW_ZIC)) || (i == RUN_INNER_ENW_ZIV) || (i == RUN_INNER_SW_PN) || (i == RUN_INNER_SY_ALM) || ((i>=RUN_INNER_DY_ON)&&(i<=RUN_INNER_U0_ON))) { LCD_DisString((i%LISTOFFSET)+1, 0, (char *) *(RunGapName+i)); displayString(isDraw,(i == RUN_INNER_SW_PN ? gcszLcdPNMode[(int)val] :gcszOnOff[(int)val]),(i%LISTOFFSET)+1,(i == RUN_INNER_SW_PN ? 14 :18)); } else { LCD_DisString((i%LISTOFFSET)+1, 0, (char *)*(RunGapName+i)); sprintf(szVal, "%0.4f", val); LCD_DisString((i%LISTOFFSET)+1, 17, szVal); if (isDraw == 1) str = LCD_BitNot(UNIT_RUNPARAM,UNIT_GAP_RT1064,gapid,i,i,bit); }
时间: 2024-03-03 10:47:04 浏览: 82
首先,可以将重复使用的变量提取出来,避免多次申明和初始化。比如可以将 `isDraw` 的计算提到循环外部;将 `szVal` 的申明和初始化提到循环外部;将 `val` 的计算提到循环外部。
其次,可以将多个 if 判断合并成一个 if-else if-else 的结构,避免重复判断。比如可以将 `(i>=RUN_INNER_EN_OL)&&(i<=RUN_INNER_EN_OVL)` 和 `(i>=RUN_INNER_ENW_OC)&&(i<=RUN_INNER_ENW_ZIC)` 合并成一个 else if 判断。
最后,可以使用三元表达式简化代码,避免重复的判断。比如可以将 `(i == RUN_INNER_SW_PN ? gcszLcdPNMode[(int)val] :gcszOnOff[(int)val])` 使用三元表达式替换。
优化后的代码如下:
```c
memset(szVal, 0, sizeof(szVal));
int isDraw = (not == 2 && (draw == (i+1)));
float val = get_RunParaInfo_val(UNIT_GAP_RT1064,gapid,i);
char *str = NULL;
if (i >= (RUN_INNER_PARA_SIZE -1)) break;
if ((i>=RUN_INNER_EN_OL && i<=RUN_INNER_EN_OVL) || (i>=RUN_INNER_ENW_OC && i<=RUN_INNER_ENW_ZIC) || (i == RUN_INNER_ENW_ZIV) || (i == RUN_INNER_SW_PN) || (i == RUN_INNER_SY_ALM) || (i>=RUN_INNER_DY_ON && i<=RUN_INNER_U0_ON))
{
LCD_DisString((i%LISTOFFSET)+1, 0, (char *) *(RunGapName+i));
char *valStr = (i == RUN_INNER_SW_PN ? gcszLcdPNMode[(int)val] : gcszOnOff[(int)val]);
displayString(isDraw, valStr, (i%LISTOFFSET)+1, (i == RUN_INNER_SW_PN ? 14 : 18));
}
else
{
LCD_DisString((i%LISTOFFSET)+1, 0, (char *)*(RunGapName+i));
sprintf(szVal, "%0.4f", val);
LCD_DisString((i%LISTOFFSET)+1, 17, szVal);
str = (isDraw == 1) ? LCD_BitNot(UNIT_RUNPARAM, UNIT_GAP_RT1064, gapid, i, i, bit) : NULL;
}
```
阅读全文