#include <stdio.h>
const char *title_weekday[7]= {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
const int year_normal_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 常(規)年
const int year_leap_days[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 閏(補)年
bool is_leap_year(int ad_year) { // 公(西)元是潤年的條件: (4 倍數且非 100 倍數) 或是 400 倍數
return (ad_year % 4 == 0) && (ad_year % 100 != 0) || (ad_year % 400 == 0);
}
int main() {
int year = 2024;
int month = 1;
int y0 = year - 1;// 註: 公(西)元 1 年 1 月 1 日 是 星期日 (weekday = 0)
int leap_weekday = year + y0/4 + y0/400 - y0/100;// 潤年補天數
const int *days_of_month = is_leap_year(year) ? year_leap_days : year_normal_days;
for (int i = 0; i + 1 < month; i ++) leap_weekday += days_of_month[i];
leap_weekday %= 7;
for (int to_show = month; to_show <= 12; to_show ++) {
printf("\t\t%6d 月\t\t西元 %6d 年\n", to_show, year);
for (int i = 0; i < 7; i++) printf("%s\t", title_weekday[i]);
int position = 0;
if (leap_weekday != 0) printf("\n");
while (position < leap_weekday) {
if (++ position % 7 == 0) printf("\n");
printf(" \t");
}
for (int day = 1; day <= days_of_month[to_show - 1]; day ++) {
printf(position ++ % 7 == 0 ? "\n%6d\t" : "%6d\t", day);
}
printf("\n\n");
leap_weekday = position % 7;
}
}
沒有留言:
張貼留言