-
Notifications
You must be signed in to change notification settings - Fork 0
/
barcode.cpp
42 lines (39 loc) · 1.02 KB
/
barcode.cpp
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
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char a[13];
while(printf("input barcode: ") && scanf("%s", a) != EOF){
if(strlen(a) != 12){
printf("error: length should be 12!\n\n");
continue;
}
int valid = 0;
for(int i = 0; i < strlen(a); i++){
if(a[i] < 48 || a[i] >= 58){
printf("error: digit only!\n\n");
valid = 1;
break;
}
}
if(valid == 1)
continue;
int s = 0;
for(int i = 0; i < strlen(a) - 1; i+=2){
s += (a[i] - 48);
}
s *= 3;
for(int i = 1; i < strlen(a) - 1; i += 2){
s += (a[i] - 48);
}
s = 10 - (s % 10);
if(s == 10)
s = 0;
if(s == a[strlen(a) - 1] - 48)
printf("check digit correct!");
else
printf("check digit incorrect!\ncheck digit should be: %d", s);
printf("\n\n");
}
return 0;
}