2016-12-31 95 views
-3

我已经创建了一个函数来计算最接近的标准值,它小于或等于输入值,但是我给了我一个比输入值大的数字! 例如,当我输入1344欧姆时,它给我1500欧姆作为最接近的标准值而不是1200欧姆!C#电阻最接近的标准值

int i = 0; 
     string[] colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "White", "Silver", "Gold" }; 
     Dictionary<char, double> factorDictionary = new Dictionary<char, double>() { { 'm', 0.001 }, { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } }; 
     string res = string.Empty; 
     double p; 
     List<double> E6 = new List<double> { 1, 1.5, 2.2, 3.3, 4.7, 6.8, 10 }; 
     List<double> E12 = new List<double> { 1, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2, 10 }; 
     List<double> E24 = new List<double> { 1, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1, 10 }; 
private void Closest_Standard_Value(double val) 
     { 
      while (p <= val) 
      { 
       if (val.ToString().Length == 1) 
       { 
        p = E12[i] * 1; 
       } 
       if (val.ToString().Length == 2) 
       { 
        p = E12[i] * 10; 
       } 
       if (val.ToString().Length == 3) 
       { 
        p = E12[i] * 100; 
       } 
       if (val.ToString().Length == 4) 
       { 
        p = E12[i] * 1000; 
       } 
       if (val.ToString().Length == 5) 
       { 
        p = E12[i] * 10000; 
       } 
       if (val.ToString().Length == 6) 
       { 
        p = E12[i] * 100000; 
       } 
       if (val.ToString().Length == 7) 
       { 
        p = E12[i] * 1000000; 
       } 
       if (val.ToString().Length == 8) 
       { 
        p = E12[i] * 10000000; 
       } 
       if (val.ToString().Length == 9) 
       { 
        p = E12[i] * 100000000; 
       } 
       if (val.ToString().Length == 10) 
       { 
        p = E12[i] * 1000000000; 
       } 
       if (val.ToString().Length == 11) 
       { 
        p = E12[i] * 10000000000; 
       } 
       if (val.ToString().Length == 12) 
       { 
        p = E12[i] * 100000000000; 
       } 
       i++; 
      } 
     } 

/////This function calculates colors of resistors using entered value///// 
    private void Band_4() 
      { 
       double val, mul; 
       if (radioButton1.Checked) 
       { 
        if (textBox1.Text == "0") 
        { 
         MessageBox.Show("Otpornost mora biti veca od nule!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         return; 
        } 
        if (comboBox1.Text == "") 
        { 
         MessageBox.Show("Izaberi vrednost tolerancije!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         return; 
        } 

        while (string.IsNullOrEmpty(res)) 
        { 
         res = textBox1.Text; 
        } 

        var lastChar = res.Last(); 
        var isUnitCorrect = factorDictionary.ContainsKey(lastChar); 
        var value = res.Substring(0, res.Length - 1); 
        var isValueCorrect = !value.Any(x => !char.IsDigit(x)); 

        if (isUnitCorrect && isValueCorrect) 
        { 
         mul = factorDictionary[lastChar]; 
         val = double.Parse(value) * mul; 
         int third = 0; 

         if (val < 0.1) 
         { 
          MessageBox.Show("Otpornost ne moze da bude manja od 0.1 oma!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error); 
          return; 
         } 
         if (val < 1) 
         { 
          val *= 100; 
          third = 10; 
         } 
         else if (val < 10) 
         { 
          val *= 10; 
          third = 11; 
         } 

         res = val.ToString(); 

         Closest_Standard_Value(val); 

         if (res.Count() > 24) 
         { 
          MessageBox.Show("Pogresna vrednost!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error); 
          return; 
         } 

         else 
         { 
          label14.BackColor = Color.FromName(colours[res[0] - '0']); 
          label15.BackColor = Color.FromName(colours[res[1] - '0']); 
          label16.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 2]); 
          textBox2.Text = colours[res[0] - '0']; 
          textBox3.Text = colours[res[1] - '0']; 
          textBox4.Text = colours[third != 0 ? third : res.Count() - 2]; 
          label3.Text = Convert.ToString(p); 
          Entered_Value(val, lastChar); 
         } 
        } 
        if (radioButton1.Checked) 
        { 
         if (comboBox1.Text == "E24 (5%)") 
         { 
          label17.BackColor = Color.Gold; 
          textBox5.Text = "Gold"; 
         } 
         if (comboBox1.Text == "E12 (10%)") 
         { 
          label17.BackColor = Color.Silver; 
          textBox5.Text = "Silver"; 
         } 
        } 
       } 
      } 
+0

我对此代码一无所知,但仅当p大于val时,循环_while(p <= val)_退出 – Steve

+0

您的'Closest_Standard_Value'方法没有任何意义。什么是'p',什么是'i'? – Sefe

+0

我的变量是通过列表E12和p是哪里去计算最接近的标准值! – Pavle

回答

0

那是因为你总是每p设置后递增i,所以你应该初始化i=-1并检查E12[i+1]

也,你Closest_Standard_Value()方法的代码可以严重减少到以下几点:

public void Closest_Standard_Value(double val) 
    { 
     while (E12[i + 1] * Math.Pow(10, val.ToString().Length - 1) <= val) 
     { 
      i++; 
     } 
    } 

或者,在每次迭代时不重复Math.Pow()

public void Closest_Standard_Value(double val) 
    { 
     val = val * Math.Pow(10, -(val.ToString().Length - 1)); 
     while (E12[i + 1] <= val) 
     { 
      i++; 
     } 
    } 

最后,两个注解:

  • 你设想的方法作为Void一个所以不返回任何值

    ,如果你想回到你搜索的List元素那么你会写如下

    public double Closest_Standard_Value(double val) 
    { 
        int decimals = val.ToString().Length - 1; 
        val = val * Math.Pow(10, -decimals);    
        while (E12[i + 1]<= val) 
         i++; 
    
        return E12[i]*Math.Pow(10, decimals); 
    } 
    
  • 你实际上假定的val真正typeint,否则他们应该通过一些2.2val参数然后val.ToString().Length将返回3而不是预期的,如果你想让真正double1

    通过为val并让你的方法正确对待它,那么你应该采用以下代码:

    public double Closest_Standard_Value(double val) 
    { 
        int decimals = ((int)val).ToString().Length - 1; 
        val = val * Math.Pow(10, -decimals);    
        while (E12[i + 1]<= val) 
         i++; 
    
        return E12[i]*Math.Pow(10, decimals); 
    } 
    
+0

它现在正在工作!非常感谢你!!! – Pavle

+0

不客气。您可能需要将答案标记为已接受。谢谢 – user3598756

+0

形成一些价值观的代码行不通:https://postimg.org/image/qkh8u1v0x/ https://postimg.org/image/5c85qd5xt/ – Pavle