Advertisement
cd62131

Zeller

Jul 15th, 2014
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. static void usage(const char *program_name) {
  6.   fprintf(stderr, "Usage: %s yyyy [m]m [d]d\n", program_name);
  7. }
  8. int main(const int argc, const char **argv) {
  9.   int i, j, len, y, m, d, leap = 0, day_of_week;
  10.   const int days[][13] = {
  11.       { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  12.       { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
  13.   const char *name[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  14.   if (argc != 4) goto error;
  15.   for (i = 1; i < 4; i++) {
  16.     len = strlen(argv[i]);
  17.     if (i == 1 && len < 4) goto error;
  18.     if ((i == 2 || i == 3) && len > 2) goto error;
  19.     for (j = 0; j < len; j++)
  20.       if (!isdigit(argv[i][j])) goto error;
  21.   }
  22.   y = atoi(argv[1]);
  23.   m = atoi(argv[2]);
  24.   d = atoi(argv[3]);
  25.   if (y < 1582) goto error;
  26.   leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  27.   if (m < 1 || 12 < m) goto error;
  28.   if (d < 1 || days[leap][m] < d) goto error;
  29.   if (m == 1 || m == 2) {
  30.     y--;
  31.     m += 12;
  32.   }
  33.   day_of_week = (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7;
  34.   printf("%s\n", name[day_of_week]);
  35.   return 0;
  36.   error: usage(argv[0]);
  37.   return 1;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement