优化这段代码 if(gRunPara.COMMS_SerialInfo[serialid][i].val == 0) LCD_DisString((i%9)+1, 15, (char *)"退出"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 1) LCD_DisString((i%9)+1, 15, (char *)"101平"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 2) LCD_DisString((i%9)+1, 15, (char *)"101非"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 3) LCD_DisString((i%9)+1, 15, (char *)"104"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 4) LCD_DisString((i%9)+1, 15, (char *)"CDT"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 5) LCD_DisString((i%9)+1, 15, (char *)"DNP3.0"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 6) LCD_DisString((i%9)+1, 15, (char *)"MudBus"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 7) LCD_DisString((i%9)+1, 15, (char *)"GSM"); else if(gRunPara.COMMS_SerialInfo[serialid][i].val == 8) LCD_DisString((i%9)+1, 15, (char *)"线损通讯");
时间: 2023-11-22 13:54:49 浏览: 63
可以将不同的 val 值对应的字符串保存在一个数组中,然后使用 val 值作为索引来获取对应的字符串,从而避免大量的重复代码。示例如下:
```
char* comms_strings[] = {"退出", "101平", "101非", "104", "CDT", "DNP3.0", "MudBus", "GSM", "线损通讯"};
LCD_DisString((i%9)+1, 15, comms_strings[gRunPara.COMMS_SerialInfo[serialid][i].val]);
```
相关问题
优化这段代码 if( i == 0 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 1 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComBaud[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 2 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszComParity[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 3 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszStateName1[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 13 || i == 14) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszPoint[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 15 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszData[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 16 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 17 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdAreaMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 20 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 21 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); }
可以将每种情况下的 LCD_DisString_Not 函数调用提取出来,放到一个数组中,然后根据 i 的值直接调用相应的函数,代码如下:
```c
void (*lcd_funcs[])(uint8_t, uint8_t, char*) = {
LCD_DisString_Not,
LCD_DisString_Not,
LCD_DisString_Not,
LCD_DisString_Not,
NULL, // 4
NULL, // 5
NULL, // 6
NULL, // 7
NULL, // 8
NULL, // 9
NULL, // 10
NULL, // 11
LCD_DisString_Not,
LCD_DisString_Not,
NULL, // 12
LCD_DisString_Not,
LCD_DisString_Not,
LCD_DisString_Not,
NULL, // 18
NULL, // 19
LCD_DisString_Not,
LCD_DisString_Not
};
void show_serial_info(int serialid) {
for (int i = 0; i < 22; i++) {
if (lcd_funcs[i] != NULL) {
lcd_funcs[i]((i%9)+1, 15, (char *)gRunPara.COMMS_SerialInfo[serialid][i].val);
}
}
}
```
可以看到,我们将每种情况下的函数调用放到了一个指针数组 lcd_funcs 中,然后根据 i 的值来调用相应的函数。其中,数组中的 NULL 表示该情况下不需要调用任何函数。最后,将整个 if-else 分支结构替换成了一个函数调用 show_serial_info。这样可以使代码更加简洁易懂。
优化这段代码 if( i == 0 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 1 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComBaud[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 2 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszComParity[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 3 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszStateName1[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 13 || i == 14) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszPoint[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 15 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszData[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 16 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 17 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdAreaMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 20 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); } else if( i == 21 ) { LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]); }
可以将这段代码进行优化,使用一个 switch case 语句来替代多个 if else 语句。具体实现代码如下:
```c
switch (i) {
case 0:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 1:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdComBaud[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 2:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszComParity[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 3:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszStateName1[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 13:
case 14:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszPoint[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 15:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszData[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 16:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 17:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdAreaMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 20:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
case 21:
LCD_DisString_Not((i%9)+1, 15, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_SerialInfo[serialid][i].val]);
break;
default:
break;
}
```
使用 switch case 语句可以让代码更加简洁明了,易于维护和修改。
阅读全文