Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
- var factorDictionary = new Dictionary<char, int>() { { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } };
- string res = string.Empty;
- while (string.IsNullOrEmpty(res))
- {
- // MessageBox.Show("Enter resistor value (use R, K, M ,G notation for .): ");
- 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)
- {
- int mul = factorDictionary[lastChar];
- double val = double.Parse(value) * mul;
- int third = 0;
- if (val < 1)
- {
- val *= 100;
- third = 9;
- }
- else if (val < 10)
- {
- val *= 10;
- third = 10;
- }
- res = val.ToString();
- if (res.Count() > 11)
- MessageBox.Show("Invalid value");
- else
- // textBox4.Text=(colours[res[0] - '0'] + " " + colours[res[1] - '0'] + " " + colours[third != 0 ? third : res.Count() - 2]);
- textBox4.Text = colours[res[0] - '0'];
- textBox5.Text= colours[res[1] - '0'];
- textBox6.Text= colours[third != 0 ? third : res.Count() - 2];
- }
- else
- MessageBox.Show("Invalid value!");
- // Console.WriteLine("\nPress any key to exit.");
- // Console.ReadKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement