2016-12-31 4 views
-3

入力値よりも小さいか等しい最も近い標準値を計算する関数を作成しましたが、入力値より大きい数値が返されます。 たとえば、1344オームと入力すると、1200オームではなく、最も近い標準値として1500オームが与えられます。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

私はこのコードは何も理解していませんが、ループ_while(p <= val)_は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++; 
     } 
    } 

最後に、さらに2つの注釈:

  • あなたは、あなたが検索さList要素を返すしたい場合ので、それは任意の値

    を返していませんVoid一つとして、あなたの方法を考案あなたのためには、次のように書いてください。

    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); 
    } 
    
  • あなたが実際にあなたが本当doubleをできるようにしたい場合は代わりに期待1

    3を返しますval.ToString().Lengthその後、そうでない場合、彼らはvalパラメータとして、いくつかの2.2を渡す必要があり、val本当typeintであると仮定しています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