1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <ios>
#include <iostream>
struct NAS {
signed char data_[9];
NAS(
std::istream & input) {
static signed char const doubles_[] = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };
// Read 3 digits, maybe a space, 3 digits, maybe a space and 3 digits.
int j(0);
char window(input.get());
if(input and std::isdigit(window)) do {
int i(0);
do {
data_[j*3+i]= window - '0'; // Let suppose '0', '1', ..., '9'.
++i;
window= input.get();
} while(input and std::isdigit(window) and i != 3);
if(i != 3) {
input.setstate(std::ios::badbit);
throw "Failure";
}
if(input and window == ' ') { // Maybe use std::isspace.
window= input.get();
}
++j;
} while(input and std::isdigit(window) and j != 3);
if(j != 3) {
input.setstate(std::ios::badbit);
throw "Failure";
}
// See <http://www.cra-arc.gc.ca/eservices/magmedia/sin-f.pdf>.
if((data_[0] + doubles_[data_[1]]+ data_[2] + doubles_[data_[3]] + data_[4]
+ doubles_[data_[5]] + data_[6] + doubles_[data_[7]] + data_[8]) % 10 != 0) {
throw "Failure";
}
}
};
int
main() {
NAS n(std::cin);
} |
Partager