#include <iostream>
#include <iomanip>
//1900/1/1 as monday??
int LastDayOfMonth(int Month, bool IsLeap = false){
if (1 > Month || Month > 12) return -1;
if (Month == 2){
if (IsLeap == true) return 29;
return 28;
}
if (Month == 4)return 30;
if (Month == 6)return 30;
if (Month == 9)return 30;
if (Month == 11)return 30;
return 31;
}
bool IsLeapYear(int Year){
return (Year % 4) == 0 && (Year % 100) != 0 || (Year % 400) == 0;//from wikipedia jp @2014
}
int ElapsedDayByYear(int year){
if (year < 0)return -1;
std::size_t Day = 0;
for (int i = 1900; i < year; i++){
Day += 365;
if (IsLeapYear(i) == true)Day++;
}
return Day;
}
int ElapsedDayByMonth(int Year, int Month){
int Day = 0;
for (int i = 1; i < Month; i++){
Day += LastDayOfMonth(i, IsLeapYear(Year));
}
return Day;
}
bool ShowCalender(int Year, int Month){
int DOTW = (ElapsedDayByYear(Year) + ElapsedDayByMonth(Year,Month))%7;
int Day = LastDayOfMonth(Month, IsLeapYear(Year));
char *Name[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
for (int i = 0; i <7; i++)std::cout << std::setw(3) << std::right << Name[i]<< ' ';
std::cout << std::endl;
for (int i = 0; i <= DOTW; i++){
if (DOTW == 6)break;
std::cout << std::setw(3) << std::right << ' ' << ' ';
}
for (int i = 0; i < Day; i++){
std::cout << std::setw(3) << std::right << i+1<< ' ';
if ((DOTW + i) % 7 == 5) std::cout << std::endl;
}
std::cout << std::endl;
return true;
}
int main(){
int Year = 2014;
for (int i = 1; i <= 12; i++){
std::cout << '['<<Year << "/" << i<<']'<<std::endl;
ShowCalender(Year, i);
std::cout << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KCi8vMTkwMC8xLzEgYXMgbW9uZGF5Pz8KCmludCBMYXN0RGF5T2ZNb250aChpbnQgTW9udGgsIGJvb2wgSXNMZWFwID0gZmFsc2UpewoJCglpZiAoMSA+IE1vbnRoIHx8IE1vbn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