优化这段代码 sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%9)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%9)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%9)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%9)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%9)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%9)+1, 25, (char *)"HZ"); } else LCD_DisString((i%9)+1, 25, (char *)"V"); if (not == 2) { if(draw == (i+1)) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }
时间: 2024-02-01 13:17:05 浏览: 139
这段代码可以进行如下优化:
1. 将重复出现的 RT1064_DZ_xxx 值定义为常量,以提高代码可读性。
2. 将 if-else 语句转换为 switch-case 语句,以简化代码。
3. 将重复出现的 (i%9)+1 计算结果赋值给变量,以提高代码可读性。
以下是优化后的代码:
```c
const int RT1064_YS_GL1 = 1;
const int RT1064_YS_FBS_JY = 2;
const int RT1064_DZ_GL1 = 3;
// ... 其他 RT1064_DZ_xxx 常量
int iMod9 = i % 9 + 1;
char szVal[8];
sprintf(szVal, "%0.3f", get_ActionDZInfo_val(UNIT_GAP_RT1064, gapid, DBData[i]));
LCD_DisString(iMod9, 19, szVal);
switch (DBData[i]) {
case RT1064_DZ_LMJ:
case RT1064_DZ_JC:
case RT1064_DZ_GYJC:
LCD_DisString(iMod9, 25, (char *) "°");
break;
case RT1064_DZ_YL_HAR:
LCD_DisString(iMod9, 25, (char *) "%");
break;
case RT1064_DZ_IDS:
case RT1064_DZ_OPENCS:
case RT1064_DZ_I03:
case RT1064_DZ_I04:
case RT1064_DZ_I05:
case RT1064_DZ_I06:
case RT1064_DZ_I07:
case RT1064_DZ_I08:
case RT1064_DZ_I09:
case RT1064_DZ_I10:
case RT1064_DZ_I11:
case RT1064_DZ_FBS_I0:
LCD_DisString(iMod9, 24, (char *) "次");
break;
case RT1064_DZ_DP:
case RT1064_DZ_GP:
LCD_DisString(iMod9, 25, (char *) "HZ");
break;
default:
LCD_DisString(iMod9, 25, (char *) "V");
if (not == 2 && draw == i + 1) {
len = LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
break;
}
if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) {
LCD_DisString(iMod9, 25, (char *) "S");
}
else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) ||
((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) &&
DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) ||
DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS ||
DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0) ||
DBData[i] == RT1064_DZ_FBS_OL) {
LCD_DisString(iMod9, 25, (char *) "A");
}
```
阅读全文