My Computer Programs

법인등록번호 검증기

steloflute 2014. 10. 29. 23:30

 

corpnum.exe

 

 

C:

 

// 법인등록번호 검증기
// Corporation registration number validator
#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
int isCRN(char *a) {
  if (strlen(a) != 13) return 0;
  int s = 0, i;
  for (i = 0; i <= 11; i++) {
    s += ((i % 2) + 1) * (a[i]-'0');
  }
  s = (10 - (s % 10)) % 10;
  return s == (a[12]-'0');
}
 
char *readline(char *prompt) {
  size_t size = 80;
  char *str = malloc(sizeof(char) * size);
  int c;
  size_t len = 0;
  printf(prompt);
  while (EOF != (c = getchar()) && c != '\r' && c != '\n') {
    str[len++] = c;
    if(len == size) str = realloc(str, sizeof(char) * (size *= 2));
  }
  str[len++]='\0';
  return realloc(str, sizeof(char) * len);
}
 
int main() {
  puts("Corporation registration number validator");
  for (;;) {
    char *s;
    s = readline("> ");
    if (feof(stdin)) break;
    puts(isCRN(s) ? "true" : "false");
    free(s);
  }
  return 0;
}

 

 

'My Computer Programs' 카테고리의 다른 글

(Windows, C#) SecondClock  (0) 2015.01.30
csv-maker  (0) 2014.11.18
Arcadia: An implementation of the Arc programming language  (0) 2014.10.26
Arcadia에서 string에 대해서 GC  (0) 2014.10.26
time value of money  (0) 2014.05.19