1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <string>
#include <map>
using namespace std;
string intToColour[] = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" }; // light (RGB)
// string intToColour[] = { "white", "cyan", "magenta", "blue", "yellow", "green", "red", "black" }; // paint (CMY)
map<string,int> colourToInt;
bool validPrimary( string colour )
{
auto it = colourToInt.find( colour );
if ( it == colourToInt.end() ) return false;
int i = it->second;
return ( i == 1 || i == 2 || i == 4 );
}
int main()
{
int i = 0;
for ( string s : intToColour ) colourToInt[s] = i++; // Invert mapping
string col1, col2, result;
cout << "Enter primary colours in lower case: ";
cin >> col1 >> col2;
if ( !validPrimary( col1 ) || !validPrimary( col2 ) ) result = "Invalid";
else if ( col1 == col2 ) result = col1;
else result = intToColour[ colourToInt[col1] + colourToInt[col2] ];
cout << "Result: " << result << '\n';
}
|