Advertisement
Pavlex4

Untitled

Oct 8th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
  2. var factorDictionary = new Dictionary<char, int>() { { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } };
  3. string res = string.Empty;
  4.  
  5. while (string.IsNullOrEmpty(res))
  6. {
  7. // MessageBox.Show("Enter resistor value (use R, K, M ,G notation for .): ");
  8. res = textBox1.Text;
  9. }
  10.  
  11. var lastChar = res.Last();
  12. var isUnitCorrect = factorDictionary.ContainsKey(lastChar);
  13. var value = res.Substring(0, res.Length - 1);
  14. var isValueCorrect = !value.Any(x => !char.IsDigit(x));
  15.  
  16. if (isUnitCorrect && isValueCorrect)
  17. {
  18. int mul = factorDictionary[lastChar];
  19. double val = double.Parse(value) * mul;
  20. int third = 0;
  21.  
  22. if (val < 1)
  23. {
  24. val *= 100;
  25. third = 9;
  26. }
  27. else if (val < 10)
  28. {
  29. val *= 10;
  30. third = 10;
  31. }
  32.  
  33. res = val.ToString();
  34.  
  35. if (res.Count() > 11)
  36. MessageBox.Show("Invalid value");
  37. else
  38. // textBox4.Text=(colours[res[0] - '0'] + " " + colours[res[1] - '0'] + " " + colours[third != 0 ? third : res.Count() - 2]);
  39. textBox4.Text = colours[res[0] - '0'];
  40. textBox5.Text= colours[res[1] - '0'];
  41. textBox6.Text= colours[third != 0 ? third : res.Count() - 2];
  42. }
  43. else
  44. MessageBox.Show("Invalid value!");
  45.  
  46. // Console.WriteLine("\nPress any key to exit.");
  47. // Console.ReadKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement