优化这段代码 if (draw == k+1) { if( k == 0 ) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<8) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else if( k == 1 ) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<9) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else if( k == 2 || k == 7 || k == 18 ) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<2) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else if( k == 6 || k == 8 ) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<2) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else if( k == 3 || k == 13 || k == 14 || k == 16 || k == 20 || k == 21 ) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<1) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else if( k == 15 || k == 17) { if (gRunPara.COMMS_SerialInfo[serialid][k].val<2) ++gRunPara.COMMS_SerialInfo[serialid][k].val; change = 1; } else change = Lcd_Modify_Param(LCD_KEY_ADD,ALTER_SERI,UNIT_PUBLIC_MX6,serialid,k,j); }
时间: 2024-01-30 18:01:35 浏览: 58
You can optimize this code by removing the repeated code blocks and using a switch statement instead of multiple if-else statements. Here's an example:
```
if (draw == k+1) {
switch (k) {
case 0:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 8) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
case 1:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 9) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
case 2:
case 7:
case 18:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 2) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
case 3:
case 13:
case 14:
case 16:
case 20:
case 21:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 1) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
case 6:
case 8:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 2) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
case 15:
case 17:
if (gRunPara.COMMS_SerialInfo[serialid][k].val < 2) {
++gRunPara.COMMS_SerialInfo[serialid][k].val;
change = 1;
}
break;
default:
change = Lcd_Modify_Param(LCD_KEY_ADD, ALTER_SERI, UNIT_PUBLIC_MX6, serialid, k, j);
break;
}
}
```
This code replaces the multiple if-else statements with a switch statement that checks the value of `k` and performs the appropriate action. The repeated code blocks have been removed, and the code is more concise and easier to read.
阅读全文