我被困在一个程序上,该程序需要用户期望的年份并打印出同年的 12 个月日历。这就是我到目前为止所拥有的,我很确定我有正确的方法来确定这一年是否是闰年(在代码中)以及如何找出一月 1 日是什么时候(下面首先只有变量然后填写)。此外,我正在尝试以正常的日历格式打印它,上面是月份,然后是下面的星期几,然后是天数。任何帮助,将不胜感激。
查找第一天:
h = (1 + [(13(m + 1))/5] + K + [K/4] + [J/4] - 2J) mod 7
h = (1 + [(13(13 + 1))/5] + (year % 100) + [(year % 100)/4] + [(year/100)/4] - 2(year/100) % 7
H 是开始日期,M 是月份,K 是 yr % 100,J 是 yr / 100。
/* Calendar.c */
#include <stdio.h>
int main(void){
int year, month, date;
int startingDay; /* initfrom user input*/
printf("Enter the year of your desired calendar: ");
scanf("%d\n", &year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
int months[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (month = 0; month < 12; month++) {
const int daysInMonth = ; /* set # of days */
int dayOfWeek;
printf(…); //month name
printf(…); //days of week
for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++)
printf(/*blanks*/);
for (int date = 1; date <= daysInMonth; date++) {
printf("…", date);
if (++dayOfWeek>6) {
printf("\n");
dayOfWeek = 0;
}
} // for date
if (dayOfWeek !=0)
printf("\n");
startingDay = dayOfWeek;
} // for month
}
输出:
February 2009
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14