优化这段代码 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; }
时间: 2023-12-10 11:42:18 浏览: 136
可以考虑使用一个数组来存储每个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]);
```
这样可以让代码更加简洁易读,且方便后续的维护和扩展。
阅读全文