輸入年份和月份打印當月日歷


 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("\t\t%d年%d月\n", year, month);
34     printf("--------------------------\n");
35     printf("日\t一\t二\t三\t四\t五\t六\n");
36     
37     //計算1號的星期數
38     firstDateWeek = weekNum(year, month, 1);
39     printBlank(firstDateWeek);      //每個月日歷前的空白
40     
41     //開始打印日期
42     for (i = 1; i <= days; i++) {
43         printf("%d\t", i);
44         dateWeek = weekNum(year, month, i);
45         if (dateWeek == 6) {
46             printf("\n");
47         }
48     }
49     
50     printf("\n");
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("\t");
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("\t\t%d年%d月\n",year,month);
 42     printf("--------------------------\n");
 43     printf("日\t一\t二\t三\t四\t五\t六\n");
 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("\t");
 57     }
 58     
 59     for (i = 1; i <= getNumberOfDaysInMonth(year, month); i++)
 60     {
 61         printf("%2d\t", i);
 62         //星期六換行
 63         if ((i + startDay) % 7 == 0)
 64         {
 65             printf("\n");
 66         }
 67     }
 68     printf("\n");
 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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM