Created
November 24, 2015 19:10
-
-
Save lluchs/679e71eedb17ad0dfade to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
/* Calculates the cross sum of a non-negative integer a. */ | |
int qsum(int a) | |
{ | |
if (a < 10) return a; | |
return (a % 10) + qsum(a / 10); | |
} | |
int ctoi(char c) | |
{ | |
return c - '0'; | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s <RV-number>\n", argv[0]); | |
return 1; | |
} | |
char *rvn = argv[1]; | |
if (strlen(rvn) != 12) { | |
fprintf(stderr, "Invalid RV-number\n"); | |
return 1; | |
} | |
int sum = 0; | |
int c = rvn[8] - 'A' + 1; | |
sum += qsum(2 * ctoi(rvn[0])); | |
sum += qsum(1 * ctoi(rvn[1])); | |
sum += qsum(2 * ctoi(rvn[2])); | |
sum += qsum(5 * ctoi(rvn[3])); | |
sum += qsum(7 * ctoi(rvn[4])); | |
sum += qsum(1 * ctoi(rvn[5])); | |
sum += qsum(2 * ctoi(rvn[6])); | |
sum += qsum(1 * ctoi(rvn[7])); | |
sum += qsum(2 * (c / 10)); | |
sum += qsum(1 * (c % 10)); | |
sum += qsum(2 * ctoi(rvn[9])); | |
sum += qsum(1 * ctoi(rvn[10])); | |
int result = sum % 10; | |
if (result == ctoi(rvn[11])) { | |
printf("Valid number, check digit: %d\n", result); | |
} else { | |
printf("Invalid number, expected %d but got %c\n", result, rvn[11]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment