fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #define WEEK "日 月 火 水 木 金 土"
  8. #define COL 24
  9.  
  10. char disp[8][80];
  11.  
  12. void strfmt(void* dest, char* fmt, int i)
  13. {
  14. char buf[8];
  15.  
  16. sprintf(buf, fmt, i);
  17. memcpy(dest, buf, strlen(buf));
  18. }
  19.  
  20. void calsub(int year, int month, int col)
  21. {
  22. struct tm tm;
  23. struct tm* ptm;
  24. time_t timer;
  25. int line;
  26. int i;
  27.  
  28. if (month < 1) {
  29. year--;
  30. month += 12;
  31. }
  32. if (12 < month) {
  33. year++;
  34. month -= 12;
  35. }
  36. strfmt(disp[0] + col * COL, "%d", year);
  37. strfmt(disp[0] + col * COL + 8, "%d月", month);
  38. memcpy(disp[1] + col * COL, WEEK, strlen(WEEK));
  39.  
  40. memset(&tm, 0, sizeof tm);
  41. tm.tm_year = year - 1900;
  42. tm.tm_mon = month - 1;
  43. tm.tm_mday = 1;
  44. timer = mktime(&tm);
  45. line = 2;
  46. for (i = 1; i <= 31; i++) {
  47. ptm = localtime(&timer);
  48. if (ptm->tm_mon != month - 1) {
  49. break;
  50. }
  51. if (ptm->tm_wday == 0 && 1 < i) {
  52. line++;
  53. }
  54. strfmt(disp[line] + col * COL + ptm->tm_wday * 3, "%2d", i);
  55. timer += 24 * 60 * 60;
  56. }
  57. }
  58.  
  59. void cal(int year, int month)
  60. {
  61. int i;
  62.  
  63. for (i = 0; i < 8; i++) {
  64. memset(disp[i], ' ', 79);
  65. disp[i][79] = '\0';
  66. }
  67. calsub(year, month - 1, 0);
  68. calsub(year, month, 1);
  69. calsub(year, month + 1, 2);
  70. for (i = 0; i < 8; i++) {
  71. printf("%s\n", disp[i]);
  72. }
  73. }
  74.  
  75. int main()
  76. {
  77. time_t timer;
  78. struct tm* ptm;
  79. int year;
  80. int month;
  81. int ch;
  82. int flag;
  83.  
  84. time(&timer);
  85. ptm = localtime(&timer);
  86. year = ptm->tm_year + 1900;
  87. month = ptm->tm_mon + 1;
  88. while (1) {
  89. cal(year, month);
  90. printf("ESCキーで終了\n");
  91. do {
  92. ch = _getch();
  93. flag = 0;
  94. switch (ch) {
  95. case 0x1b: // ESC
  96. return 0;
  97. case 0x48: // up
  98. month -= 3;
  99. break;
  100. case 0x49: // PageUp
  101. year--;
  102. break;
  103. case 0x4b: // left
  104. month--;
  105. break;
  106. case 0x4d: // right
  107. month++;
  108. break;
  109. case 0x50: // down
  110. month += 3;
  111. break;
  112. case 0x51: // PageDown
  113. year++;
  114. break;
  115. case 0xe0:
  116. default:
  117. flag = 1;
  118. }
  119. } while (flag);
  120. if (month < 1) {
  121. year--;
  122. month += 12;
  123. }
  124. if (12 < month) {
  125. year++;
  126. month -= 12;
  127. }
  128. }
  129. return 0;
  130. }
  131.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty