Skip to content

Instantly share code, notes, and snippets.

@pyllyukko
Last active June 27, 2020 19:03
Show Gist options
  • Save pyllyukko/dbae5776ac8773f97d28594f17a12ac7 to your computer and use it in GitHub Desktop.
Save pyllyukko/dbae5776ac8773f97d28594f17a12ac7 to your computer and use it in GitHub Desktop.
hetu.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define C "0123456789ABCDEFHJKLMNPRSTUVWXY"
#define X(y, z) y*10+z-48-y
#define Y(z) (!(z%4)&&(z%100)||!(z%400))
extern struct _IO_FILE *stderr;
int main(int argc, char *argv[])
{
unsigned char
j, m, d, s, *p;
int i = 0;
unsigned short y;
static unsigned char D[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
if(argc!=2||strlen(argv[1])!=10)
{
fprintf(stderr, "error!\n");
exit(-1);
}
p = argv[1];
for(j=0; j<=9; j++)
{
if(!isdigit(*p)&&j!=6)
{
fprintf(stderr, "error!\n");
exit(-1);
}
switch(j)
{
case 0:
d = *p - 48;
break;
case 1:
d += X(d, *p);
break;
case 2:
m = *p - 48;
break;
case 3:
m += X(m, *p);
break;
case 4:
y = *p - 48;
break;
case 5:
y += X(y, *p);
break;
case 6:
switch(*p)
{
case 43:
y += 0x708;
break;
case 45:
y += 0x76c;
break;
case 65:
y += 0x7d0;
break;
case 97:
y += 03720;
break;
default:
fprintf(stderr, "error!\n");
exit(-1);
}
if(Y(y)&&d>D[1][m-1]||!Y(y)&&d>D[0][m-1])
{
fprintf(stderr, "invalid date!\n");
exit(-1);
}
break;
case 9:
s = *p%2;
break;
}
if(j!=6)
i += X(i, *p);
p++;
}
if(m==2&&d==29) printf("tough luck with your birthday!\n");
printf("%d.%d.%d\n%s\n%c\n", d, m, y, s?"man":"woman", C[i%31]);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment