Advertisement
Pavlex4

Untitled

Oct 6th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // ConsoleApplication38.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <cctype>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. const string colours[11]{ "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
  12.  
  13. int main()
  14. {
  15. int q;
  16. string res;
  17.  
  18. cout << "Enter resistor value (use R, K, M ,G notation for .): ";
  19. cin >> res;
  20.  
  21. int mul = 1;
  22.  
  23. for (auto& c : res) {
  24. static bool spec = false;
  25. if (((c == 'K') || (c == 'R') || (c == 'M') || (c == 'G')))
  26. if (spec) {
  27. cout << "Invalid value" << endl;
  28. return 1;
  29. }
  30. else {
  31. switch (c) {
  32. case 'R':
  33. mul = 1;
  34. break;
  35.  
  36. case 'K':
  37. mul = 1000;
  38. break;
  39.  
  40. case 'M':
  41. mul = 1000000;
  42. break;
  43. case 'G':
  44. mul = 1000000000;
  45. break;
  46. }
  47.  
  48. c = '.';
  49. spec = true;
  50. }
  51. else
  52. if (!isdigit(c)) {
  53. cout << "Invalid value" << endl;
  54. return 2;
  55. }
  56. }
  57.  
  58. int third = 0;
  59. double val = stod(res) * mul;
  60.  
  61. if (val < 1) {
  62. val *= 100;
  63. third = 9;
  64. }
  65. else
  66. if (val < 10) {
  67. val *= 10;
  68. third = 10;
  69. }
  70.  
  71. res = to_string((unsigned int)(val));
  72.  
  73. if (res.size() > 11) {
  74. cout << "Invalid value";
  75. return 1;
  76. }
  77.  
  78. cout << colours[res[0] - '0'] << " " << colours[res[1] - '0'] << " " << colours[third ? third : res.size() - 2] << endl;
  79. system("pause");
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement