【发布时间】:2013-05-11 17:25:23
【问题描述】:
我将一个由通过串行端口连接的微处理器获取的毫伏值的变量传递给我的应用程序中的一个类。 该类必须将接收到的值“milliV”与对应于 10°C 增量的许多值进行比较。我必须这样做是为了输出的准确性,因为热电偶的输出不是线性的。当“milliV”的值低于特定值时,则在顶部和底部更接近的值之间进行积分计算,并返回一个新值“_tempEx”。
这个类正在工作并返回正确的值,但我认为除了 if/if else 和 switch,应该有更好的方法来实现相同的结果。我还没有编码应该包含大约的整个类。我需要覆盖的范围内的 150 个比较值。 此外,必须对 K 型热电偶做同样的事情,我最终会得到数百个字符串进行比较。
有没有更好的方法来实现这个类中方法相同的结果?
public class Termocoppia
{
//milliV@temperature for thermocouple type "J"
double t10=1.019, t20=1.537, t30=2.059, t40=2.585, t50=3.116;
double t60=3.650, t70=4.187, t80=4.726, t90=5.269, t100=5.814;
double t110=6.360, t120=6.909, t130=7.459, t140=8.010, t150=8.562;
double t160=9.115, t170=9.669, t180=10.224, t190=10.779, t200=11.334;
double t210=11.889, t220=12.445, t230=13.000, t240=13.555, t250=14.110;
double t260=14.665, t270=15.219, t280=15.773, t290=16.327, t300=16.881;
//Temperature References
double d10 = 10.00, d20 = 20.00, d30 = 30, d40 = 40, d50 = 50;
double d60 = 60, d70 = 70, d80 = 80, d90 = 90, d100 = 100;
double d110 = 110, d120 = 120, d130 = 130, d140 = 140, d150 = 150;
double d160 = 160, d170 = 170, d180 = 180, d190 = 190, d200 = 200;
double d210=210, d220=220, d230=230, d240=240, d250=250, d260=260;
double d270=270, d280=280, d290=290, d300=300;
// find the highest value and the bottom one to integrate in between withthe received milliV
// returns the value with _tempEx
public double Calcola(double milliV, double _tempEx)
{
if (milliV <= t10)
{
_tempEx = d10;
}
else if (milliV <= t20)
{
_tempEx = d20 - ((t20 - milliV) / ((t20 - t10) / 10));//Questa formula è corretta
}
else if (milliV <= t30)
{
_tempEx = d30 - ((t30 - milliV) / ((t30 - t20) / 10));
}
else if (milliV <= t40)
{
_tempEx = d40 - ((t40 - milliV) / ((t40 - t30) / 10));
}
else if (milliV <= t50)
{
_tempEx = d50 - ((t50 - milliV) / ((t50 - t40) / 10));
}
...........
...........
else if (milliV <= t300)
{
_tempEx = d300 - ((t300 - milliV) / ((t300 - t290) / 10));
}
else
{
}
return _tempEx;
}
我希望能提供示例代码和/或指向可用参考的答案。
【问题讨论】:
-
听起来你真的想要一个数组,或者一个列表...
-
或
Dictionary,如果温度和热电偶未映射在连续范围内。