优化这段代码void Speed_auto_cool(void) { if ((Mot_Settemperature + 6) <= Mot_readtemper) { Speed_control_tem = 3; // 大3(华氏为6)度,高 } else if ((Mot_Settemperature + 4) == Mot_readtemper) { Speed_control_tem = 2; // 大2(华氏为4)度,中 } else if (Mot_readtemper <= (Mot_Settemperature + 2)) { Speed_control_tem = 1; // 低 } else { if ((Mot_Settemperature + 4) < Mot_readtemper) { if (Speed_control_tem != 3) Speed_control_tem = 2; } else { if (Speed_control_tem != 2) Speed_control_tem = 1; } } }
时间: 2024-02-29 20:52:24 浏览: 59
matlab中匹配模型代码-DMAN_MOT:具有双重匹配注意网络的在线多对象跟踪代码,ECCV2018
这段代码可以进行一些优化,具体如下:
1. 使用常量替代魔法数字,提高代码可读性。
2. 建议使用 switch 语句替代多个 if-else if 语句,可以提高代码的可读性和可维护性。
3. 将相同的代码块提取出来,避免重复代码出现。
下面是优化后的代码:
```
void Speed_auto_cool(void) {
const int HIGH_TEMPERATURE_DIFFERENCE = 6; // 华氏6度
const int MEDIUM_TEMPERATURE_DIFFERENCE = 4; // 华氏4度
const int LOW_TEMPERATURE_DIFFERENCE = 2; // 华氏2度
int temperatureDifference = Mot_readtemper - Mot_Settemperature;
switch (temperatureDifference) {
case HIGH_TEMPERATURE_DIFFERENCE:
Speed_control_tem = 3; // 大3(华氏为6)度,高
break;
case MEDIUM_TEMPERATURE_DIFFERENCE:
Speed_control_tem = 2; // 大2(华氏为4)度,中
break;
default:
if (Mot_readtemper <= (Mot_Settemperature + LOW_TEMPERATURE_DIFFERENCE)) {
Speed_control_tem = 1; // 低
break;
}
if (temperatureDifference > MEDIUM_TEMPERATURE_DIFFERENCE) {
if (Speed_control_tem != 3) {
Speed_control_tem = 2;
}
} else {
if (Speed_control_tem != 2) {
Speed_control_tem = 1;
}
}
break;
}
}
```
阅读全文