Verhoeff algorithm: Difference between revisions

Line 235:
#include <iostream>
#include <string>
#include <array>
 
typedef std::pair<std::string, bool> data;
 
const std::array<const std::array<int32_t, 10>, 10> multiplication_table = { {
int multiplication_table[10][10] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
Line 249 ⟶ 250:
{ 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
} };
 
intconst std::array<int32_t, inverse[10]> inverse = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
 
const std::array<const std::array<int32_t, 10>, 8> permutation_table = { {
int permutation_table[8][10] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
Line 262 ⟶ 263:
{ 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
{ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
} };
 
intint32_t verhoeff_checksum(std::string number, bool doValidation, bool doDisplay) {
if ( doDisplay ) {
std::string calculationType = doValidation ? "Validation" : "Check digit";
Line 276 ⟶ 277:
}
 
intint32_t c = 0;
const intint32_t le = number.length() - 1;
for ( intint32_t i = le; i >= 0; i-- ) {
const intint32_t ni = number[i] - '0';
const intint32_t pi = permutation_table[(le - i) % 8][ni];
c = multiplication_table[c][pi];
 
Line 296 ⟶ 297:
 
int main( ) {
const std::array<data, tests[3]> tests = {
std::make_pair("123", true), std::make_pair("12345", true), std::make_pair("123456789012", false) };
 
for ( data test : tests ) {
intint32_t digit = verhoeff_checksum(test.first, false, test.second);
std::cout << "The check digit for " << test.first << " is " << digit << "\n" << std::endl;
 
895

edits