1 /* 2 QQ:778138708 3 date:2020-5-14 4 输入年份和月份,打印当月日历 5 6 */ 7 /* 8 日期转换为星期的方法 9 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7 10 在公式中d表示日期中的日数,m表示月份数,y表示年数 11 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。 12 */ 13 #include <stdio.h> 14 int weekNum(int year, int month, int day); 15 int isLeap(int year); 16 void printBlank(int n); 17 int main(void) 18 { 19 int year, month; 20 int leap, days; 21 int i, firstDateWeek, dateWeek; 22 int tab[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 23 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; 24 25 printf("请输入一个年份(例如:2020):"); 26 scanf("%d", &year); 27 printf("请输入一个月份(1-12的数字):"); 28 scanf("%d", &month); 29 30 leap = isLeap(year); 31 days = tab[leap][month]; //每个月的总天数 32 33 printf(" %d年%d月 ", year, month); 34 printf("-------------------------- "); 35 printf("日 一 二 三 四 五 六 "); 36 37 //计算1号的星期数 38 firstDateWeek = weekNum(year, month, 1); 39 printBlank(firstDateWeek); //每个月日历前的空白 40 41 //开始打印日期 42 for (i = 1; i <= days; i++) { 43 printf("%d ", i); 44 dateWeek = weekNum(year, month, i); 45 if (dateWeek == 6) { 46 printf(" "); 47 } 48 } 49 50 printf(" "); 51 52 return 0; 53 } 54 //日期转换为星期 55 int weekNum(int year, int month, int day) 56 { 57 int week; 58 59 if (month == 1 || month == 2) { 60 year =year - 1; 61 month = 12 + month; 62 } 63 week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7; 64 65 return week; 66 } 67 //判断闰年 68 int isLeap(int year) 69 { 70 int leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 71 72 return leap; 73 } 74 //打印1号之前的空白 75 void printBlank(int n) 76 { 77 int i; 78 79 for (i = 0; i < n; i++) 80 { 81 printf(" "); 82 } 83 }
方法二
1 #include <stdio.h> 2 #include <stdbool.h> 3 4 #define START_DAY_FOR_JAN_1_1800 3 5 6 void printMonth(int year, int month); 7 void printMonthTitle(int year, int month); 8 void printMonthBody(int year, int month); 9 int getStartDay(int year, int month); 10 int getTotalNumberOfDays(int year, int month); 11 int getNumberOfDaysInMonth(int year, int month); 12 bool isLeapYear(int year); 13 int main(void) 14 { 15 int year, month; 16 17 printf("请输入一个年份:(例如:2020):"); 18 scanf("%d", &year); 19 20 printf("请输入一个月份:(1-12的数字):"); 21 scanf("%d", &month); 22 23 printMonth(year, month); 24 25 return 0; 26 } 27 28 //打印相应年份和月份的日历 29 void printMonth(int year, int month) 30 { 31 //打印日历的开头 32 printMonthTitle(year, month); 33 34 //打印日历内容 35 printMonthBody(year, month); 36 } 37 38 //打印日历的开头 39 void printMonthTitle(int year, int month) 40 { 41 printf(" %d年%d月 ",year,month); 42 printf("-------------------------- "); 43 printf("日 一 二 三 四 五 六 "); 44 45 } 46 47 //打印日历的内容 48 void printMonthBody(int year, int month) 49 { 50 int startDay = getStartDay(year, month); 51 int i; 52 53 //在当月的第一天前面加上若干个空格 54 for (i = 0; i < startDay; i++) 55 { 56 printf(" "); 57 } 58 59 for (i = 1; i <= getNumberOfDaysInMonth(year, month); i++) 60 { 61 printf("%2d ", i); 62 //星期六换行 63 if ((i + startDay) % 7 == 0) 64 { 65 printf(" "); 66 } 67 } 68 printf(" "); 69 } 70 71 //返回某月第一天是星期几 72 int getStartDay(int year, int month) 73 { 74 return (START_DAY_FOR_JAN_1_1800 + getTotalNumberOfDays(year, month)) % 7; 75 76 } 77 //返回从1800年1月1日至year年month月1日之间的天数 78 int getTotalNumberOfDays(int year, int month) 79 { 80 int totalDays = 0; 81 int i; 82 for (i = 1800; i < year; i++) 83 { 84 //闰年问题 85 if (isLeapYear(i)) 86 { 87 totalDays += 366; 88 } 89 else 90 { 91 totalDays += 365; 92 } 93 } 94 95 for (i = 1; i < month; i++) 96 { 97 totalDays += getNumberOfDaysInMonth(year, i); 98 } 99 100 return totalDays; 101 } 102 //返回每个月的天数 103 int getNumberOfDaysInMonth(int year, int month) 104 { 105 int days; 106 107 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 108 { 109 days = 31; 110 } 111 else if (month == 4 || month == 6 || month == 9 || month == 11) 112 { 113 days = 30; 114 } 115 else if (month == 2) 116 { 117 //闰年问题 118 days = isLeapYear(year)?29:28; 119 } 120 else 121 { 122 days = 0; 123 } 124 125 return days; 126 } 127 128 //判断是否是闰年 129 bool isLeapYear(int year) 130 { 131 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 132 }