我將通過串口連接的微處理器獲取的毫伏數變量傳遞給我的應用程序中的類。 該課程必須將收到的值「milliV」與許多值相比較,以10°C爲增量進行比較。因爲熱電偶的輸出不是線性的,所以我必須這樣做才能保證輸出的準確性。當「milliV」的值小於特定值時,則對頂部和底部較近的值進行積分計算並返回一個新值「_tempEx」。將變量與數百個值進行比較
該類正在工作並返回正確的值,但I認爲除了if/if else和switch之外,還應該有更好的方法來達到同樣的結果,我沒有編碼整個類,它應該包含我需要覆蓋的大約150個比較值 此外,必須做的對於K型熱電偶一樣,我最終會與上百串的比較。
有沒有更好的方式來實現這個類中的方法相同的結果呢?
public class Termocoppia
{
//[email protected] 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;
}
我將不勝感激示例代碼的答案和/或指向可用的參考。
這聽起來像是你真的想要一個數組或一個列表...... – 2013-05-11 17:26:25
或者一個'Dictionary'如果溫度和熱電偶沒有在連續範圍內映射。 – 2013-05-11 17:46:40