-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnpj.c
53 lines (40 loc) · 1.02 KB
/
cnpj.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "brtools.h"
static int A[12] = {5,4,3,2,9,8,7,6,5,4,3,2};
static int B[13] = {6,5,4,3,2,9,8,7,6,5,4,3,2};
static char numbers[10] = "0123456789";
static void to_string(const int* const buffer, char* dest, int length) {
for (int c = 0; c < length; c++) {
dest[c] = numbers[buffer[c]];
}
}
int main(void) {
(void)srandom(time(NULL));
int* buffer = (int*)malloc(sizeof(int) * 16);
if (!buffer)
return 1;
memset(buffer, 0, sizeof(int) * 16);
for (int c = 0; c < 12; c++) {
buffer[c] = (int)((random() * 9 / RAND_MAX));
}
int calc = 0;
calc = calculate(buffer, A, 14);
buffer[12] = calc;
calc = calculate(buffer, B, 14);
buffer[13] = calc;
buffer[14] = 0;
char* as_str = (char*)malloc(sizeof(char) * 16);
if (!as_str) {
free(buffer);
return 1;
}
(void)memset(as_str, 0, sizeof(char) * 16);
to_string(buffer, as_str, 14);
printf("%s\n", as_str);
free(buffer);
free(as_str);
return 0;
}