Damm algorithm: Difference between revisions

m
→‎{{header|C++}}: Shortened code a bit
(Add MACRO-11)
m (→‎{{header|C++}}: Shortened code a bit)
Line 760:
=={{header|C++}}==
{{trans|C#|C sharp}}
<syntaxhighlight lang="cpp">#include <sstreamstring>
 
#include <cstdio>
const int TABLE[][10] = {
 
constinline constexper int TABLE[][10] = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
Line 775 ⟶ 777:
};
 
using[[nodiscard]] bool damm(std::string; s) noexcept {
bool damm(string s) {
int interim = 0;
for (charconst auto c : s) {
interim = TABLE[interim][c - '0'];
}
Line 785 ⟶ 786:
 
int main() {
for (const auto numbersnum =: { 5724, 5727, 112946, 112949 };) {
if (damm(std::to_string(num))) {
for (int num : numbers) {
std::printf("%6d is valid\n", num);
using std::stringstream;
}
stringstream ss;
else std::printf("%6d is invalid\n", num);
ss << num;
bool isValid = damm(ss.str());
if (isValid) {
printf("%6d is valid\n", num);
} else {
printf("%6d is invalid\n", num);
}
}
 
return 0;
}</syntaxhighlight>
{{out}}
11

edits