优化这段代码 if(i >= COMMS_NET_TOTALSUM) break; memset(szVal, 0, sizeof(szVal)); sprintf(szVal, "%s", gRunPara.COMMS_NetInfo[netid][i].szName); LCD_DisString((i%9)+1, 0, szVal); if( ((i >= COM_NET_PCL) && (i <= COM_NET_DIR)) || ((i >= COM_NET_YXM) && (i <= COM_NET_SNTP)) || (i == COM_NET_SYNCTM)) { if( i == COM_NET_PCL ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdComPcl[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if(((i > COM_NET_PCL) && (i < COM_NET_DIR)) || i == COM_NET_SNTP ) { unsigned char bytesforIP[4]; if (i == COM_NET_IP) { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(buf,"%d%d%d.%d%d%d.%d%d%d.%d%d%d",bytesforIP[0]/100,bytesforIP[0]%100/10,bytesforIP[0]%10,bytesforIP[1]/100,bytesforIP[1]%100/10,bytesforIP[1]%10, bytesforIP[2]/100,bytesforIP[2]%100/10,bytesforIP[2]%10,bytesforIP[3]/100,bytesforIP[3]%100/10,bytesforIP[3]%10); LCD_DisString((i%9)+1, 10, buf); len = strlen(buf); if (not == 2) Lcd_IP_Not(netid,i,j,len,buf); } else { if (i == COM_NET_SNTP ) { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 14, szVal); } else { (float)bytesforIP = gRunPara.COMMS_NetInfo[netid][i].val; sprintf(szVal,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]); LCD_DisString((i%9)+1, 10, szVal); } } } else if( i == COM_NET_DIR ) { LCD_DisString((i%9)+1, 16, (char *)gcszStateName1[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YXM || i == COM_NET_YKM) { LCD_DisString((i%9)+1, 16, (char *)gcszPoint[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_YCM ) { LCD_DisString((i%9)+1, 16, (char *)gcszData[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_JM ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdJmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_AREA ) { LCD_DisString((i%9)+1, 16, (char *)gcszLcdSynctmMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } else if( i == COM_NET_SYNCTM ) { LCD_DisString((i%9)+1, 18, (char *)gcszLcdRSMode[(int)gRunPara.COMMS_NetInfo[netid][i].val]); } } else { if (i >= 12 && i <= 14) { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 16, szVal); } else { sprintf(szVal, "%.0f", gRunPara.COMMS_NetInfo[netid][i].val); LCD_DisString((i%9)+1, 18, szVal); } }
时间: 2023-12-14 15:38:38 浏览: 63
可以考虑将部分重复的代码提取出来,封装成函数,减少重复代码的出现。此外,可以使用更加直观的变量名,增加代码的可读性。代码示例:
```c
void display_net_info(int netid, int index, int row) {
char buf[20];
float val = gRunPara.COMMS_NetInfo[netid][index].val;
// display name
memset(buf, 0, sizeof(buf));
sprintf(buf, "%s", gRunPara.COMMS_NetInfo[netid][index].szName);
LCD_DisString((index%9)+1, row, buf);
// display value
if ((index >= COM_NET_PCL && index <= COM_NET_DIR) || (index >= COM_NET_YXM && index <= COM_NET_SNTP) || index == COM_NET_SYNCTM) {
if (index == COM_NET_PCL) {
LCD_DisString((index%9)+1, row+16, (char *)gcszLcdComPcl[(int)val]);
} else if ((index > COM_NET_PCL && index < COM_NET_DIR) || index == COM_NET_SNTP) {
unsigned char bytesforIP[4];
if (index == COM_NET_IP) {
(float)bytesforIP = val;
sprintf(buf,"%d%d%d.%d%d%d.%d%d%d.%d%d%d",bytesforIP[0]/100,bytesforIP[0]%100/10,bytesforIP[0]%10,bytesforIP[1]/100,bytesforIP[1]%100/10,bytesforIP[1]%10, bytesforIP[2]/100,bytesforIP[2]%100/10,bytesforIP[2]%10,bytesforIP[3]/100,bytesforIP[3]%100/10,bytesforIP[3]%10);
LCD_DisString((index%9)+1, row+10, buf);
int len = strlen(buf);
if (not == 2) Lcd_IP_Not(netid,index,j,len,buf);
} else {
if (index == COM_NET_SNTP) {
(float)bytesforIP = val;
sprintf(buf,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]);
LCD_DisString((index%9)+1, row+14, buf);
} else {
(float)bytesforIP = val;
sprintf(buf,"%d.%d.%d.%d",bytesforIP[0],bytesforIP[1],bytesforIP[2],bytesforIP[3]);
LCD_DisString((index%9)+1, row+10, buf);
}
}
} else if (index == COM_NET_DIR) {
LCD_DisString((index%9)+1, row+16, (char *)gcszStateName1[(int)val]);
} else if (index == COM_NET_YXM || index == COM_NET_YKM) {
LCD_DisString((index%9)+1, row+16, (char *)gcszPoint[(int)val]);
} else if (index == COM_NET_YCM) {
LCD_DisString((index%9)+1, row+16, (char *)gcszData[(int)val]);
} else if (index == COM_NET_JM) {
LCD_DisString((index%9)+1, row+16, (char *)gcszLcdJmMode[(int)val]);
} else if (index == COM_NET_AREA) {
LCD_DisString((index%9)+1, row+16, (char *)gcszLcdSynctmMode[(int)val]);
} else if (index == COM_NET_SYNCTM) {
LCD_DisString((index%9)+1, row+18, (char *)gcszLcdRSMode[(int)val]);
}
} else {
memset(buf, 0, sizeof(buf));
sprintf(buf, "%.0f", val);
if (index >= 12 && index <= 14) {
LCD_DisString((index%9)+1, row+16, buf);
} else {
LCD_DisString((index%9)+1, row+18, buf);
}
}
}
```
在调用 `display_net_info` 函数时,可以直接传入对应的参数,简化代码,提高可读性。
阅读全文