【问题标题】:Compare variable with hundreds values将变量与数百个值进行比较
【发布时间】: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,如果温度和热电偶未映射在连续范围内。

标签: c# arrays string list


【解决方案1】:

正如已经指出的,您可以使用数组:

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;
    }
}

td 值将值存储在数组中。然后代码循环遍历t 数组并检查条件(milliV &lt;= t[idx])。如果为真,则使用对应的d[idx]t[idx-1] 值来计算结果。

【讨论】:

  • 太棒了,非常感谢。有没有办法可以将这些值从项目资源中复制的 excel 表中提取到数组中?或类似的东西。
  • @FeliceM - 是的,这是可能的。但是,读取 CSV 或文本文件会更容易。谷歌应该返回大量结果。这可能会有所帮助:stackoverflow.com/questions/9777206/…
  • 是的,你是对的,谷歌永远是你最好的朋友。感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2013-01-18
  • 1970-01-01
  • 2022-06-25
相关资源
最近更新 更多