RosettaCodeData/Task/Validate-International-Securities-Identification-Number/C++/validate-international-securities-identification-number.cpp

60 lines
1.2 KiB
C++
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
#include <string>
#include <regex>
#include <algorithm>
#include <numeric>
#include <sstream>
bool CheckFormat(const std::string& isin)
{
2026-04-30 12:34:36 -04:00
std::regex isinRegEpx(R"([A-Z]{2}[A-Z0-9]{9}[0-9])");
std::smatch match;
return std::regex_match(isin, match, isinRegEpx);
2023-07-01 11:58:00 -04:00
}
std::string CodeISIN(const std::string& isin)
{
2026-04-30 12:34:36 -04:00
std::string coded;
int offset = 'A' - 10;
for (auto ch : isin)
{
if (ch >= 'A' && ch <= 'Z')
{
std::stringstream ss;
ss << static_cast<int>(ch) - offset;
coded += ss.str();
}
else
{
coded.push_back(ch);
}
}
return std::move(coded);
2023-07-01 11:58:00 -04:00
}
bool CkeckISIN(const std::string& isin)
{
2026-04-30 12:34:36 -04:00
if (!CheckFormat(isin))
return false;
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
std::string coded = CodeISIN(isin);
2023-07-01 11:58:00 -04:00
// from http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#C.2B.2B11
2026-04-30 12:34:36 -04:00
return luhn(coded);
2023-07-01 11:58:00 -04:00
}
#include <iomanip>
#include <iostream>
int main()
{
2026-04-30 12:34:36 -04:00
std::string isins[] = { "US0378331005", "US0373831005", "U50378331005",
"US03378331005", "AU0000XVGZA3", "AU0000VXGZA3",
"FR0000988040" };
for (const auto& isin : isins)
{
std::cout << isin << std::boolalpha << " - " << CkeckISIN(isin) <<std::endl;
}
return 0;
2023-07-01 11:58:00 -04:00
}