Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static void usage(const char *program_name) {
- fprintf(stderr, "Usage: %s yyyy [m]m [d]d\n", program_name);
- }
- int main(const int argc, const char **argv) {
- int i, j, len, y, m, d, leap = 0, day_of_week;
- const int days[][13] = {
- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
- { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
- const char *name[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
- if (argc != 4) goto error;
- for (i = 1; i < 4; i++) {
- len = strlen(argv[i]);
- if (i == 1 && len < 4) goto error;
- if ((i == 2 || i == 3) && len > 2) goto error;
- for (j = 0; j < len; j++)
- if (!isdigit(argv[i][j])) goto error;
- }
- y = atoi(argv[1]);
- m = atoi(argv[2]);
- d = atoi(argv[3]);
- if (y < 1582) goto error;
- leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
- if (m < 1 || 12 < m) goto error;
- if (d < 1 || days[leap][m] < d) goto error;
- if (m == 1 || m == 2) {
- y--;
- m += 12;
- }
- day_of_week = (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7;
- printf("%s\n", name[day_of_week]);
- return 0;
- error: usage(argv[0]);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement