Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication38.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- const string colours[11]{ "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
- int main()
- {
- int q;
- string res;
- cout << "Enter resistor value (use R, K, M ,G notation for .): ";
- cin >> res;
- int mul = 1;
- for (auto& c : res) {
- static bool spec = false;
- if (((c == 'K') || (c == 'R') || (c == 'M') || (c == 'G')))
- if (spec) {
- cout << "Invalid value" << endl;
- return 1;
- }
- else {
- switch (c) {
- case 'R':
- mul = 1;
- break;
- case 'K':
- mul = 1000;
- break;
- case 'M':
- mul = 1000000;
- break;
- case 'G':
- mul = 1000000000;
- break;
- }
- c = '.';
- spec = true;
- }
- else
- if (!isdigit(c)) {
- cout << "Invalid value" << endl;
- return 2;
- }
- }
- int third = 0;
- double val = stod(res) * mul;
- if (val < 1) {
- val *= 100;
- third = 9;
- }
- else
- if (val < 10) {
- val *= 10;
- third = 10;
- }
- res = to_string((unsigned int)(val));
- if (res.size() > 11) {
- cout << "Invalid value";
- return 1;
- }
- cout << colours[res[0] - '0'] << " " << colours[res[1] - '0'] << " " << colours[third ? third : res.size() - 2] << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement