优化这段代码 LCD_DisString((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } }
时间: 2024-02-05 07:10:54 浏览: 60
以下是对这段代码的优化建议:
1. 将重复调用的函数get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])的返回值存储在一个变量中,这样可以避免重复计算,提高效率。
2. 将常量字符串"gcszOnOff"的类型从char*改为const char*,以便更好地表达其只读特性。
3. 将重复使用的表达式(i%9)+1提取出来存储在一个变量中,以提高可读性。
4. 将if语句中的判断条件`(not == 2)`改为`(not != 2)`,以避免可能的逻辑错误。
优化后的代码如下:
```
int index = i % 9 + 1;
const char* onOffStr = gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064, gapid, DBData[i])];
LCD_DisString(index, 20, (char*)onOffStr);
if (not != 2 && draw == i + 1) {
LCD_DisString_Not(index, 20, (char*)onOffStr);
}
```
相关问题
优化这段代码 switch (i) { case COM_COM_PCL: LCD_DisString((i%9)+1, 15, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_BR: LCD_DisString((i%9)+1, 15, (char *)gcszLcdComBaud[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_PR: LCD_DisString((i%9)+1, 15, (char *)gcszComParity[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_DIR: LCD_DisString((i%9)+1, 15, (char *)gcszStateName1[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_YXM: LCD_DisString((i%9)+1, 15, (char *)gcszPoint[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_YKM: LCD_DisString((i%9)+1, 15, (char *)gcszPoint[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_YCM: LCD_DisString((i%9)+1, 15, (char *)gcszData[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_JM: LCD_DisString((i%9)+1, 15, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_AREA: LCD_DisString((i%9)+1, 15, (char *)gcszLcdAreaMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_SYNCTM: LCD_DisString((i%9)+1, 15, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; case COM_COM_RS485: LCD_DisString((i%9)+1, 15, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); break; default: break; }
可以考虑使用一个数组来存储每个case对应的要执行的函数,然后利用i作为索引调用对应的函数,避免了大量的重复代码,示例如下:
```
void (*displayFunc[])(int, int, char*) = {
NULL,
[COM_COM_PCL] = LCD_DisString,
[COM_COM_BR] = LCD_DisString,
[COM_COM_PR] = LCD_DisString,
[COM_COM_DIR] = LCD_DisString,
[COM_COM_YXM] = LCD_DisString,
[COM_COM_YKM] = LCD_DisString,
[COM_COM_YCM] = LCD_DisString,
[COM_COM_JM] = LCD_DisString,
[COM_COM_AREA] = LCD_DisString,
[COM_COM_SYNCTM] = LCD_DisString,
[COM_COM_RS485] = LCD_DisString,
};
void display(int i, int col, char* str) {
if (displayFunc[i] != NULL) {
displayFunc[i](i%9+1, col, str);
}
}
```
然后在原来的代码中,可以直接调用`display()`函数,示例如下:
```
display(i, 15, gcszLcdComPcl[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
```
这样可以让代码更加简洁易读,且方便后续的维护和扩展。
优化这段代码 if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { LCD_DisString((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if(DBData[i] == RT1064KZZ_MODE) { LCD_DisString((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { if (DBData[i] == RT1064KZZ_FBS) { LCD_DisString((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if(DBData[i] == RT1064KZZ_FBS_MODE ) { LCD_DisString((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%9)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%9)+1, 24, (char *)"次"); if (not == 2) { if(draw == (i+1)) { len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); } } }
可以考虑将重复的代码抽象成一个函数,然后在需要的地方调用该函数。具体来说,可以把以下部分抽象成一个函数:
```
LCD_DisString((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
if (not == 2) {
if(draw == (i+1)) {
LCD_DisString_Not((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
}
}
```
然后可以把其他类似的代码也进行抽象。这样可以大大简化代码,增强可读性。
阅读全文