优化这段代码 int Lcd_Modify_Param(int ikey,unsigned char mode,int _boardid,int gapid,int ioa,int digit) { float param; int len; int index = digit - 1; const float add_arr[3][8] = { {pow(10,0), 0 ,pow(10,-1),pow(10,-2), pow(10,-3),pow(10,-4)}, {pow(10,1),pow(10,0), 0 , pow(10,-1), pow(10,-2),pow(10,-3),pow(10,-4)}, {pow(10,2),pow(10,1),pow(10,0), 0 , pow(10,-1),pow(10,-2),pow(10,-3),pow(10,-4)} }; if(mode == ALTER_RUNPARAM) param = get_RunParaInfo_val(_boardid,gapid,ioa); else if (mode == ALTER_PROTECT) param = get_ActionDZInfo_val(_boardid,gapid,ioa); else if (mode == ALTER_SERI) param = gRunPara.COMMS_SerialInfo[gapid][ioa].val; if ((mode == ALTER_SERI) || (mode == ALTER_PROTECT&&(ioa == RT1064KZZ_UAB_CH || ioa == RT1064KZZ_UBC_CH || ioa == RT1064_DZ_CHZCS))) { printf("szName:%s\n",gRunPara.gap_ActionDZInfo[gapid][ioa].szName); param = SetInteger(ikey,param,digit); printf("param:%f\n", param); } else { len = snprintf(NULL, 0, "%0.3f", param); // 获取字符串长度 char buf[len+1]; // 创建缓冲区 snprintf(buf, len+1, "%0.3f", param); // 将浮点数转换为字符串 if (ikey == LCD_KEY_ADD) { if (len >= 5 && len <= 7 && index >= 0 && index <= 7) param += add_arr[len-5][index]; } else if(ikey == LCD_KEY_DECREASE) { if (len >= 5 && len <= 7 && index >= 0 && index <= 7) param -= add_arr[len-5][index]; } } if (param >= 0) { if(mode == ALTER_RUNPARAM) { if (_boardid == UNIT_PUBLIC_MX6) { if(gRunPara.ALLptRunParaInfo[ioa].IDbyBoard == 0) { if(gRunPara.ALLptRunParaInfo[ioa].IDbyPt < MX6RUN_TOTALSUM) { gRunPara.pub_RunParaInfo[gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } } else { if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt != RT1064KZZ_PTDX && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RUN_INNER_PARA_SIZE) { gRunPara.gap_RunParaInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } else if ((gRunPara.ALLptRunParaInfo[ioa].IDbyPt == RT1064KZZ_PTDX || gRunPara.ALLptRunParaInfo[ioa].IDbyPt >= RT1064_DZ_YY) && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RT1064_YS_TOTALSUM) //--四个参数在 内部动作参数区 { gRunPara.gap_ActionDZInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } } } else if (_boardid == UNIT_GAP_RT1064) gRunPara.gap_RunParaInfo[gapid][ioa].val= param; } else if (mode == ALTER_PROTECT) { if (_boardid == UNIT_PUBLIC_MX6) gRunPara.pub_ActionDZInfo[ioa].val = param; else { if (param <= 999999) gRunPara.gap_ActionDZInfo[gapid][ioa].val = param; } } else if (mode == ALTER_SERI) { if (param <= 999999) gRunPara.COMMS_SerialInfo[gapid][ioa].val = param; } } return 1; }
时间: 2024-01-26 18:05:05 浏览: 130
ldt.rar_The Call
1. 避免在函数内部定义变长数组。这里使用了变长数组来存储字符串,但是它的长度是在运行时才确定的,会占用大量的栈空间,容易引发栈溢出问题。可以使用动态分配内存来代替。
2. 将重复的代码抽象为函数,提高代码的可读性和可维护性。例如,多次使用了 `SetInteger` 函数,可以将其封装为一个单独的函数。
3. 将常量提取为全局变量或宏定义,提高代码的可维护性和可读性。
4. 使用更有意义的变量名,增加代码的可读性。例如,`ikey` 可以改为 `key`,`gapid` 可以改为 `gap_id` 等等。
5. 使用 `switch` 语句来替代多个 `if-else` 语句,提高代码的可读性。
下面是优化后的代码:
```c
#define FLOAT_PRECISION 0.001
#define MAX_FLOAT_LENGTH 10
typedef enum {
ALTER_RUNPARAM,
ALTER_PROTECT,
ALTER_SERI
} AlterMode;
typedef enum {
LCD_KEY_ADD,
LCD_KEY_DECREASE
} KeyType;
float add_arr[3][8] = {
{pow(10,0), 0 ,pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)},
{pow(10,1), pow(10,0), 0 , pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)},
{pow(10,2), pow(10,1), pow(10,0), 0 , pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)}
};
float get_param(AlterMode mode, int board_id, int gap_id, int ioa) {
switch (mode) {
case ALTER_RUNPARAM:
return get_RunParaInfo_val(board_id, gap_id, ioa);
case ALTER_PROTECT:
return get_ActionDZInfo_val(board_id, gap_id, ioa);
case ALTER_SERI:
return gRunPara.COMMS_SerialInfo[gap_id][ioa].val;
default:
return 0.0;
}
}
void set_param(AlterMode mode, int board_id, int gap_id, int ioa, float param) {
if (param >= 0) {
switch (mode) {
case ALTER_RUNPARAM:
if (board_id == UNIT_PUBLIC_MX6) {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyBoard == 0) {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt < MX6RUN_TOTALSUM) {
gRunPara.pub_RunParaInfo[gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
}
} else {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt != RT1064KZZ_PTDX && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RUN_INNER_PARA_SIZE) {
gRunPara.gap_RunParaInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
} else if ((gRunPara.ALLptRunParaInfo[ioa].IDbyPt == RT1064KZZ_PTDX || gRunPara.ALLptRunParaInfo[ioa].IDbyPt >= RT1064_DZ_YY) && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RT1064_YS_TOTALSUM) {
gRunPara.gap_ActionDZInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
}
}
} else if (board_id == UNIT_GAP_RT1064) {
gRunPara.gap_RunParaInfo[gap_id][ioa].val = param;
}
break;
case ALTER_PROTECT:
if (board_id == UNIT_PUBLIC_MX6) {
gRunPara.pub_ActionDZInfo[ioa].val = param;
} else {
if (param <= 999999) {
gRunPara.gap_ActionDZInfo[gap_id][ioa].val = param;
}
}
break;
case ALTER_SERI:
if (param <= 999999) {
gRunPara.COMMS_SerialInfo[gap_id][ioa].val = param;
}
break;
default:
break;
}
}
}
float modify_param(KeyType key, AlterMode mode, int board_id, int gap_id, int ioa, int digit) {
float param = get_param(mode, board_id, gap_id, ioa);
if ((mode == ALTER_SERI) || (mode == ALTER_PROTECT && (ioa == RT1064KZZ_UAB_CH || ioa == RT1064KZZ_UBC_CH || ioa == RT1064_DZ_CHZCS))) {
printf("szName:%s\n",gRunPara.gap_ActionDZInfo[gap_id][ioa].szName);
param = SetInteger(key, param, digit);
printf("param:%f\n", param);
} else {
char buf[MAX_FLOAT_LENGTH];
snprintf(buf, MAX_FLOAT_LENGTH, "%0.3f", param);
int len = strlen(buf);
int index = digit - 1;
if (key == LCD_KEY_ADD) {
if (len >= 5 && len <= 7 && index >= 0 && index <= 7) {
param += add_arr[len-5][index];
}
} else if(key == LCD_KEY_DECREASE) {
if (len >= 5 && len <= 7 && index >= 0 && index <= 7) {
param -= add_arr[len-5][index];
}
}
}
set_param(mode, board_id, gap_id, ioa, param);
return param;
}
```
阅读全文