用capl代码写一段水温表测试的代码
//定义水温表结构体 typedef struct { float temperature; //水温 int status; //水温状态: 0-正常, 1-过高, 2-过低 } WaterTemperature;
//定义水温表对象 WaterTemperature waterTemp;
//定义水温范围 const float MIN_TEMP = 5.0; const float MAX_TEMP = 100.0;
//定义水温状态枚举 enum { NORMAL_TEMP, HIGH_TEMP, LOW_TEMP };
//定义水温检测函数 void checkWaterTemp() { if (waterTemp.temperature > MAX_TEMP) { waterTemp.status = HIGH_TEMP; } else if (waterTemp.temperature < MIN_TEMP) { waterTemp.status = LOW_TEMP; } else { waterTemp.status = NORMAL_TEMP; } }
//定义测试函数 void testWaterTemp() { //设置水温为正常值 waterTemp.temperature = 25.0; checkWaterTemp(); assert(waterTemp.status == NORMAL_TEMP);
//设置水温为过高值 waterTemp.temperature = 110.0; checkWaterTemp(); assert(waterTemp.status == HIGH_TEMP);
//设置水温为过低值 waterTemp.temperature = -10.0; checkWaterTemp(); assert(waterTemp.status == LOW_TEMP); }
//调用测试函数 testWaterTemp();
原文地址: https://www.cveoy.top/t/topic/bpSm 著作权归作者所有。请勿转载和采集!