fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. //1900/1/1 as monday??
  5.  
  6. int LastDayOfMonth(int Month, bool IsLeap = false){
  7.  
  8. if (1 > Month || Month > 12) return -1;
  9.  
  10. if (Month == 2){
  11. if (IsLeap == true) return 29;
  12. return 28;
  13. }
  14.  
  15. if (Month == 4)return 30;
  16. if (Month == 6)return 30;
  17. if (Month == 9)return 30;
  18. if (Month == 11)return 30;
  19.  
  20. return 31;
  21. }
  22.  
  23. bool IsLeapYear(int Year){
  24. return (Year % 4) == 0 && (Year % 100) != 0 || (Year % 400) == 0;//from wikipedia jp @2014
  25. }
  26.  
  27. int ElapsedDayByYear(int year){
  28. if (year < 0)return -1;
  29.  
  30. std::size_t Day = 0;
  31.  
  32. for (int i = 1900; i < year; i++){
  33. Day += 365;
  34. if (IsLeapYear(i) == true)Day++;
  35. }
  36.  
  37. return Day;
  38. }
  39. int ElapsedDayByMonth(int Year, int Month){
  40. int Day = 0;
  41.  
  42. for (int i = 1; i < Month; i++){
  43. Day += LastDayOfMonth(i, IsLeapYear(Year));
  44. }
  45.  
  46. return Day;
  47. }
  48.  
  49. bool ShowCalender(int Year, int Month){
  50. int DOTW = (ElapsedDayByYear(Year) + ElapsedDayByMonth(Year,Month))%7;
  51. int Day = LastDayOfMonth(Month, IsLeapYear(Year));
  52. char *Name[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  53.  
  54. for (int i = 0; i <7; i++)std::cout << std::setw(3) << std::right << Name[i]<< ' ';
  55. std::cout << std::endl;
  56. for (int i = 0; i <= DOTW; i++){
  57. if (DOTW == 6)break;
  58. std::cout << std::setw(3) << std::right << ' ' << ' ';
  59. }
  60. for (int i = 0; i < Day; i++){
  61. std::cout << std::setw(3) << std::right << i+1<< ' ';
  62. if ((DOTW + i) % 7 == 5) std::cout << std::endl;
  63. }
  64. std::cout << std::endl;
  65. return true;
  66.  
  67. }
  68.  
  69. int main(){
  70.  
  71. int Year = 2014;
  72.  
  73. for (int i = 1; i <= 12; i++){
  74. std::cout << '['<<Year << "/" << i<<']'<<std::endl;
  75. ShowCalender(Year, i);
  76. std::cout << std::endl;
  77. }
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
[2014/1]
Sun Mon Tue Wed Thu Fri Sat 
              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  31 

[2014/2]
Sun Mon Tue Wed Thu Fri Sat 
                          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 

[2014/3]
Sun Mon Tue Wed Thu Fri Sat 
                          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  31 

[2014/4]
Sun Mon Tue Wed Thu Fri Sat 
          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 

[2014/5]
Sun Mon Tue Wed Thu Fri Sat 
                  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  31 


[2014/6]
Sun Mon Tue Wed Thu Fri Sat 
  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 

[2014/7]
Sun Mon Tue Wed Thu Fri Sat 
          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  31 

[2014/8]
Sun Mon Tue Wed Thu Fri Sat 
                      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 
 31 

[2014/9]
Sun Mon Tue Wed Thu Fri Sat 
      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 

[2014/10]
Sun Mon Tue Wed Thu Fri Sat 
              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  31 

[2014/11]
Sun Mon Tue Wed Thu Fri Sat 
                          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 

[2014/12]
Sun Mon Tue Wed Thu Fri Sat 
      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  31