Verhoeff algorithm: Difference between revisions
Content added Content deleted
m (Added language identifier.) |
m (Made code more C++ idiomatic.) |
||
Line 233: | Line 233: | ||
<syntaxhighlight lang="c++"> |
<syntaxhighlight lang="c++"> |
||
#include <cstdint> |
|||
#include <iostream> |
#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 266: | Line 267: | ||
} }; |
} }; |
||
int32_t verhoeff_checksum(std::string number, bool doValidation, bool doDisplay) { |
int32_t verhoeff_checksum(std::string number, const bool doValidation, const bool doDisplay) { |
||
if ( doDisplay ) { |
if ( doDisplay ) { |
||
std::string calculationType = doValidation ? "Validation" : "Check digit"; |
std::string calculationType = doValidation ? "Validation" : "Check digit"; |
||
Line 302: | Line 303: | ||
std::make_pair("123", true), std::make_pair("12345", true), std::make_pair("123456789012", false) }; |
std::make_pair("123", true), std::make_pair("12345", true), std::make_pair("123456789012", false) }; |
||
for ( data test : tests ) { |
for ( const data& test : tests ) { |
||
int32_t digit = verhoeff_checksum(test.first, false, test.second); |
int32_t digit = verhoeff_checksum(test.first, false, test.second); |
||
std::cout << "The check digit for " << test.first << " is " << digit << "\n" << std::endl; |
std::cout << "The check digit for " << test.first << " is " << digit << "\n" << std::endl; |
||
std::string numbers[2] = { test.first + std::to_string(digit), test.first + "9" }; |
std::string numbers[2] = { test.first + std::to_string(digit), test.first + "9" }; |
||
for ( std::string number : numbers ) { |
for ( const std::string& number : numbers ) { |
||
digit = verhoeff_checksum(number, true, test.second); |
digit = verhoeff_checksum(number, true, test.second); |
||
std::string result = ( digit == 1 ) ? "correct" : "incorrect"; |
std::string result = ( digit == 1 ) ? "correct" : "incorrect"; |