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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm> // find()
using std::string;
using std::vector;
struct IDType {
string firstChar;
string secondChar;
vector<int> number;
};
vector<IDType> IdTypes {
{"AKS", "BCD", {101,102,103,104,105,106,107,108,109,110}},
{"BHN", "AFGH", {113,114,115,116,213,220,560,890}},
{"CT", "KLZ", {134,138,145,245}},
{"MOZ", "ADF", {177,178,179,180,181,333,334,335}}
};
const int NumTypes = IdTypes.size();
bool checkID(const string& id) {
std::istringstream iss(id);
char firstChar, secondChar;
int number;
if (!(iss >> firstChar >> secondChar >> number))
return false;
int type = 0;
for ( ; type < NumTypes; ++type)
if (IdTypes[type].firstChar.find(firstChar) != std::string::npos)
break;
if (type == NumTypes)
return false;
const auto& t = IdTypes[type];
return t.secondChar.find(secondChar) != std::string::npos &&
std::find(t.number.begin(), t.number.end(), number) != t.number.end();
}
string numberString(const vector<int>& nums) {
if (nums.size() == 0)
return "";
std::ostringstream oss;
oss << nums[0];
int in_a_row = 1;
for (size_t i = 1; i <= nums.size(); ++i) {
if (i < nums.size() && nums[i] == nums[i - 1] + 1)
++in_a_row;
else {
if (in_a_row > 1)
oss << (in_a_row > 2 ? '-' : ',') << nums[i - 1];
if (i < nums.size())
oss << ',' << nums[i];
in_a_row = 1;
}
}
return oss.str();
}
void showTypes() {
std::cout << "\nID Types:\n";
for (int i = 0; i < NumTypes; ++i)
std::cout << " " << i + 1
<< ". [" << std::left << std::setw(4) << IdTypes[i].firstChar
<< "] [" << std::setw(4) << IdTypes[i].secondChar
<< "] {" << numberString(IdTypes[i].number) << "}\n" << std::right;
}
int main() {
for (;;) {
showTypes();
string id;
std::cout << "\nEnter ID (or nothing to quit): ";
std::getline(std::cin, id);
if (id.size() == 0)
break;
std::cout << '\n' << id << " is "
<< (checkID(id) ? "" : "not ")
<< "valid.\n";
std::cout << "Press 'Enter' to continue... ";
std::cin.ignore(9999, '\n');
}
}
|