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.  
  9. char disp[8][80];
  10.  
  11. void strfmt(void* dest, char* fmt, int i)
  12. {
  13. char buf[8];
  14.  
  15. sprintf(buf, fmt, i);
  16. memcpy(dest, buf, strlen(buf));
  17. }
  18.  
  19. void cal(int year, int month)
  20. {
  21. struct tm tm;
  22. struct tm* ptm;
  23. time_t timer;
  24. int line;
  25. int i;
  26.  
  27. for (i = 0; i < 8; i++) {
  28. memset(disp[i], ' ', 79);
  29. disp[i][79] = '\0';
  30. }
  31. strfmt(disp[0], "%d", year);
  32. strfmt(disp[0] + 8, "%d月", month);
  33. memcpy(disp[1], WEEK, strlen(WEEK));
  34.  
  35. memset(&tm, 0, sizeof tm);
  36. tm.tm_year = year - 1900;
  37. tm.tm_mon = month - 1;
  38. tm.tm_mday = 1;
  39. timer = mktime(&tm);
  40. line = 2;
  41. for (i = 1; i <= 31; i++) {
  42. ptm = localtime(&timer);
  43. if (ptm->tm_mon != month - 1) {
  44. break;
  45. }
  46. if (ptm->tm_wday == 0 && 1 < i) {
  47. line++;
  48. }
  49. strfmt(disp[line] + ptm->tm_wday * 3, "%2d", i);
  50. timer += 24 * 60 * 60;
  51. }
  52.  
  53. for (i = 0; i < 8; i++) {
  54. printf("%s\n", disp[i]);
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. cal(2012, 6);
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0.01s 1808KB
stdin
Standard input is empty
stdout
2012    6月                                                                   
日 月 火 水 木 金 土                                                    
                1  2                                                           
 3  4  5  6  7  8  9                                                           
10 11 12 13 14 15 16                                                           
17 18 19 20 21 22 23                                                           
24 25 26 27 28 29 30