C# 将变量与数百个值进行比较
我将一个值为毫伏的变量传递给我应用程序中的一个类,该变量由一个通过串行端口连接的微处理器获取。 该类必须将接收值“毫伏”与对应于10°C增量的许多值进行比较。我必须这样做是为了输出的准确性,因为热电偶的输出不是线性的。当“毫伏”的值低于特定值时,在顶部和底部更接近的值之间进行积分计算,并返回一个新值“_tempEx” 这个类正在工作并返回正确的值,但我认为除了if/if-else和switch之外,应该有更好的方法来实现相同的结果。我没有对整个类进行编码,该类应该包含大约150个比较值,在我需要覆盖的范围内。 此外,对于K型热电偶也必须这样做,我最终会得到数百个字符串进行比较 有没有更好的方法在这个类中实现相同的方法结果C# 将变量与数百个值进行比较,c#,arrays,string,list,C#,Arrays,String,List,我将一个值为毫伏的变量传递给我应用程序中的一个类,该变量由一个通过串行端口连接的微处理器获取。 该类必须将接收值“毫伏”与对应于10°C增量的许多值进行比较。我必须这样做是为了输出的准确性,因为热电偶的输出不是线性的。当“毫伏”的值低于特定值时,在顶部和底部更接近的值之间进行积分计算,并返回一个新值“_tempEx” 这个类正在工作并返回正确的值,但我认为除了if/if-else和switch之外,应该有更好的方法来实现相同的结果。我没有对整个类进行编码,该类应该包含大约150个比较值,在我需要
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;
}
公共类TERMOCOPIA
{
//milliV@temperature对于“J”型热电偶
双t10=1.019,t20=1.537,t30=2.059,t40=2.585,t50=3.116;
双t60=3.650,t70=4.187,t80=4.726,t90=5.269,t100=5.814;
双t110=6.360,t120=6.909,t130=7.459,t140=8.010,t150=8.562;
双t160=9.115,t170=9.669,t180=10.224,t190=10.779,t200=11.334;
双t210=11.889,t220=12.445,t230=13.000,t240=13.555,t250=14.110;
双t260=14.665,t270=15.219,t280=15.773,t290=16.327,t300=16.881;
//温度基准
双d10=10.00,d20=20.00,d30=30,d40=40,d50=50;
双d60=60,d70=70,d80=80,d90=90,d100=100;
双d110=110,d120=120,d130=130,d140=140,d150=150;
双d160=160,d170=170,d180=180,d190=190,d200=200;
双d210=210,d220=220,d230=230,d240=240,d250=250,d260=260;
双d270=270,d280=280,d290=290,d300=300;
//找到最高值和底部值,将其与接收到的毫伏进行积分
//返回带有_tempEx的值
公共双Calcola(双毫伏,双温度)
{
如果(毫伏如前所述,您可以使用阵列:
class Termocoppia
{
// be sure to add all of your values here...
double[] t = { 1.019, 1.537, 2.059, ... };
double[] d = { 10, 20, 30, ... };
public double Calcola(double milliV, double _tempEx)
{
if (milliV <= t[0])
{
// handle first case
_tempEx = d[0];
}
else
{
bool success = false;
int count = t.Length;
// loop through all t values, test, and then calculate
for (int idx = 1; idx < count; idx++)
{
if (milliV <= t[idx])
{
_tempEx = d[idx] -
((t[idx] - milliV) / ((t[idx] - t[idx - 1]) / 10));
success = true;
break;
}
}
if (success == false)
throw new Exception("Unable to calculate _tempEX");
}
return _tempEx;
}
}
class termocopia
{
//请确保在此处添加所有值。。。
双[]t={1.019,1.537,2.059,…};
双[]d={10,20,30,…};
公共双Calcola(双毫伏,双温度)
{
如果(毫伏如前所述,您可以使用阵列:
class Termocoppia
{
// be sure to add all of your values here...
double[] t = { 1.019, 1.537, 2.059, ... };
double[] d = { 10, 20, 30, ... };
public double Calcola(double milliV, double _tempEx)
{
if (milliV <= t[0])
{
// handle first case
_tempEx = d[0];
}
else
{
bool success = false;
int count = t.Length;
// loop through all t values, test, and then calculate
for (int idx = 1; idx < count; idx++)
{
if (milliV <= t[idx])
{
_tempEx = d[idx] -
((t[idx] - milliV) / ((t[idx] - t[idx - 1]) / 10));
success = true;
break;
}
}
if (success == false)
throw new Exception("Unable to calculate _tempEX");
}
return _tempEx;
}
}
class termocopia
{
//请确保在此处添加所有值。。。
双[]t={1.019,1.537,2.059,…};
双[]d={10,20,30,…};
公共双Calcola(双毫伏,双温度)
{
如果(毫伏听起来你真的想要一个数组,或者一个列表…或者一个字典,如果温度和热电偶没有在一个连续的范围内映射的话。听起来你真的想要一个数组,或者一个列表…或者一个字典,如果温度和热电偶没有在一个连续的范围内映射的话。那太棒了,非常感谢有没有一种方法可以将这些值从项目资源中复制的excel工作表或类似的内容中复制到数组中。@FeliceM-是的,这是可能的。但是,读取CSV或文本文件会更容易。谷歌应该返回大量结果。这可能会有帮助:是的,谷歌永远是你最好的朋友。谢谢你的支持。这太棒了,非常感谢。有什么方法可以将这些值从项目资源中复制的excel工作表或类似的内容中复制到数组中吗?@FeliceM-是的,这是可能的。但是,读取CSV或文本文件会更容易。谷歌应该返回大量结果。这可能会有帮助:是的,你是对的,谷歌永远是你最好的朋友,谢谢你的支持。