入力値よりも小さいか等しい最も近い標準値を計算する関数を作成しましたが、入力値より大きい数値が返されます。 たとえば、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";
}
}
}
}
私はこのコードは何も理解していませんが、ループ_while(p <= val)_はpがvalより大きいときに終了します – Steve
'Closest_Standard_Value'メソッドは意味をなさない。 「p」とは何ですか?「i」とは何ですか? – Sefe
変数はリストE12を通過するためのもので、pは最も近い標準値が計算される場所です! – Pavle