优化这段代码 Lcd_Disp((i%9)+1, 20,0, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { Lcd_Disp((i%9)+1, 20,1, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } }
时间: 2024-02-05 12:11:16 浏览: 44
C++图书管理系统代码.docx
可以对这段代码进行如下优化:
1. 将多次调用相同的函数和变量提取出来:
```
char* onOffStr = (char*)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])];
Lcd_Disp((i%9)+1, 20, 0, onOffStr);
```
2. 将 if 语句中的条件判断语句提取到外面,避免重复计算:
```
int curDraw = i + 1;
int isDraw = (not == 2 && draw == curDraw);
Lcd_Disp((i%9)+1, 20, isDraw ? 1 : 0, onOffStr);
```
整合后的代码如下:
```
char* onOffStr;
int curDraw = i + 1;
int isDraw = (not == 2 && draw == curDraw);
onOffStr = (char*)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])];
Lcd_Disp((i%9)+1, 20, isDraw ? 1 : 0, onOffStr);
```
阅读全文