【发布时间】:2023-02-09 20:14:30
【问题描述】:
我正在做一个项目,其中我从传感器收集数据并将数据存储在大小为 3 的数组中。数据存储是这样的,它总是在索引 [2] 中存储最大值,在索引 [0] 中存储最小值,并且新数据进来的应该与已经存在的数据进行比较,并相应地在数组中排序或被丢弃。
我就是这样做的
if(New_value > three_values_list[2]){
three_values_list[0] = three_values_list[1];
three_values_list[1] = three_values_list[2];
three_values_list[2] = New_value;
}else if(three_values_list[2] > New_value > three_values_list[1]){
three_values_list[0] = three_values_list[1];
three_values_list[1] = New_value;
}else if(three_values_list[1] > New_value > three_values_list[0]){
three_values_list[0] = New_value;
}
我希望更有效地做到这一点,而不仅仅是单独比较。
我也使用过三元运算符,但这似乎太大而且不太可读,有没有更好的方法来做到这一点?
【问题讨论】:
-
a > b > c并不代表你认为它在 C 中的含义。我认为你的意思是a > b && b > c。