Advertisement
cd62131

calendar

May 24th, 2019
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4.   int y, m;
  5.   setbuf(stdout, NULL);
  6.   for (;;) {
  7.     fputs("year? ", stdout);
  8.     if (scanf("%d", &y) != 1) { exit(1); }
  9.     if (1581 < y) { break; }
  10.   }
  11.   for (;;) {
  12.     fputs("month? ", stdout);
  13.     if (scanf("%d", &m) != 1) { exit(1); }
  14.     if (0 < m && m < 13) { break; }
  15.   }
  16.   int w = 0;
  17.   {
  18.     int yy = y, mm = m;
  19.     if (mm < 3) { --yy, mm += 12; }
  20.     w = (yy + yy / 4 - yy / 100 + yy / 400 + (13 * mm + 8) / 5 + 1) % 7;
  21.   }
  22.   int days =
  23.       (m == 4 || m == 6 || m == 9 || m == 11)
  24.           ? 30
  25.           : (m == 2) ? (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28)
  26.                      : 31;
  27.   puts(""), puts(" Sun Mon Tue Wed Thu Fri Sat");
  28.   for (int d = 1, c = 0; d <= days;) {
  29.     if (0 < w--) {
  30.       fputs("    ", stdout), ++c;
  31.       continue;
  32.     }
  33.     printf("%4d", d++);
  34.     if (++c % 7 == 0) { puts(""); }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement