万年历

mooc161031464079970630 发表于2023年11月24日
<p>#include &lt;stdio.h&gt;</p><p><br ></p><p>char poem[12][50]={</p><p>&quot;一月一花开,开时月常好&quot;,</p><p>&quot;二月二日新雨晴,草芽菜甲一时生&quot;,</p><p>&quot;曾记三月春浓,花风烂熳,吹散鸳鸯侣&quot;,</p><p>&quot;人间四月芳菲尽,山寺桃花始盛开&quot;,</p><p>&quot;小池残暑退,高树早凉归&quot;,</p><p>&quot;六月之雨田成溪,七月之旱烟尘飞&quot;,</p><p>&quot;七月诗成种田法,庶人章作劝农文&quot;,</p><p>&quot;七月诗成种田法,庶人章作劝农文&quot;,</p><p>&quot;银烛秋光冷画屏,轻罗小扇扑流萤&quot;,</p><p>&quot;三秋多湿雨,十月见黄花&quot;,</p><p>&quot;边城十一月,雨雪乱霏霏&quot;,</p><p>&quot;幽州思妇十二月,停歌罢笑双蛾摧&quot;</p><p>};</p><p><br ></p><p><br ></p><p>// 判断是否为闰年</p><p><br ></p><p>int isLeapYear(int year) {</p><p><br ></p><p>&nbsp; &nbsp; return (year % 4 == 0 &amp;&amp; year % 100 != 0) || (year % 400 == 0);</p><p><br ></p><p>}</p><p><br ></p><p><br ></p><p><br ></p><p>// 获取某年某月的天数</p><p><br ></p><p>int getDaysInMonth(int year, int month) {</p><p><br ></p><p>&nbsp; &nbsp; int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; if (month == 2 &amp;&amp; isLeapYear(year)) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; return 29;</p><p><br ></p><p>&nbsp; &nbsp; } else {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; return days[month];</p><p><br ></p><p>&nbsp; &nbsp; }</p><p><br ></p><p>}</p><p><br ></p><p><br ></p><p><br ></p><p>// 获取某年某月第一天是星期几</p><p><br ></p><p>int getFirstDayOfWeek(int year, int month) {</p><p><br ></p><p>&nbsp; &nbsp; // Zeller's Congruence算法,计算某一天是星期几</p><p><br ></p><p>&nbsp; &nbsp; if (month &lt; 3) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; month += 12;</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; year--;</p><p><br ></p><p>&nbsp; &nbsp; }</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; int h = (1 + (13 * (month + 1)) / 5 + year + year / 4 - year / 100 + year / 400) % 7;</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 0表示星期六,1表示星期日,2表示星期一,依此类推</p><p><br ></p><p>&nbsp; &nbsp; return (h + 5) % 7;</p><p><br ></p><p>}</p><p><br ></p><p><br ></p><p><br ></p><p>// 打印月历</p><p><br ></p><p>void printCalendar(int year, int month) {</p><p><br ></p><p>&nbsp; &nbsp; int i, j;</p><p><br ></p><p>&nbsp; &nbsp; int daysInMonth = getDaysInMonth(year, month);</p><p><br ></p><p>&nbsp; &nbsp; int firstDayOfWeek = getFirstDayOfWeek(year, month);</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 打印月份和年份</p><p><br ></p><p>&nbsp; &nbsp; printf(&quot;&nbsp; &nbsp;%d 年 %d 月\n&quot;, year, month);</p><p><br ></p><p>&nbsp; &nbsp; printf(&quot;日 一 二 三 四 五 六\n&quot;);</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 打印月份的第一行前面的空格</p><p><br ></p><p>&nbsp; &nbsp; for (i = 0; i &lt; firstDayOfWeek; i++) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;&nbsp; &nbsp;&quot;);</p><p><br ></p><p>&nbsp; &nbsp; }</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 打印日历</p><p><br ></p><p>&nbsp; &nbsp; for (j = 1; j &lt;= daysInMonth; j++) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%2d &quot;, j);</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; // 换行</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; if ((j + firstDayOfWeek) % 7 == 0) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;\n&quot;);</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p><br ></p><p>&nbsp; &nbsp; }</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 打印最后一行的换行</p><p><br ></p><p>&nbsp; &nbsp; if ((daysInMonth + firstDayOfWeek) % 7 != 0) {</p><p><br ></p><p>&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;\n&quot;);</p><p><br ></p><p>&nbsp; &nbsp; }</p><p><br ></p><p>}</p><p><br ></p><p><br ></p><p>void f1_month(){</p><p> int i=0;&nbsp;</p><p>&nbsp; &nbsp; int year, month;</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 输入年份和月份</p><p>&nbsp; &nbsp;&nbsp;</p><p><br ></p><p>&nbsp; &nbsp; printf(&quot;请输入年份和月份(以空格分隔): &quot;);</p><p>&nbsp; &nbsp; scanf(&quot;%d %d&quot;, &amp;year, &amp;month);</p><p>&nbsp; &nbsp; while(month&gt;12||month&lt;0){</p><p>&nbsp; &nbsp; printf(&quot;输入月数错误,请重新输入&quot;);</p><p>&nbsp; &nbsp; &nbsp; printf(&quot;请输入年份和月份(以空格分隔): &quot;);</p><p>&nbsp; &nbsp; scanf(&quot;%d %d&quot;, &amp;year, &amp;month);</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; if(month&lt;=12&amp;&amp;month&gt;0){</p><p>&nbsp; &nbsp; printf(&quot;------------------------------&quot;);</p><p>&nbsp; &nbsp; printf(&quot;%d年%d月&quot;,year,month);</p><p> printCalendar(year, month);</p><p>&nbsp; &nbsp;printf(&quot;%s&quot;,poem[month-1]);</p><p>&nbsp; &nbsp;puts(&quot;\n&quot;);</p><p>&nbsp; &nbsp; }&nbsp;</p><p>}</p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 打印月历</p><p><br ></p><p>void f2_year()</p><p>{</p><p>int i=0;&nbsp;</p><p>int month=0;</p><p>int year;</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 输入年份和月份</p><p>&nbsp; &nbsp;&nbsp;</p><p><br ></p><p>&nbsp; &nbsp; printf(&quot;请输入年: &quot;);</p><p>&nbsp; &nbsp; scanf(&quot;%d&quot;, &amp;year);</p><p>&nbsp; &nbsp; &nbsp;printf(&quot;%d年&quot;,year);</p><p>&nbsp; &nbsp; &nbsp;for(month=1;month&lt;13;month++){</p><p>&nbsp; &nbsp; &nbsp;printCalendar(year, month);</p><p>&nbsp; &nbsp;printf(&quot;%s&quot;,poem[month-1]);</p><p>&nbsp; &nbsp;puts(&quot;\n&quot;);</p><p>&nbsp; &nbsp; </p><p>&nbsp; &nbsp; &nbsp;}</p><p> </p><p>&nbsp; &nbsp;&nbsp;</p><p>&nbsp; &nbsp; }</p><p>void f3_jijie(){</p><p> ////////////////////////////char jijie[]////////////////////////</p><p> int i=0;&nbsp;</p><p>int month=0;</p><p>int year;</p><p><br ></p><p><br ></p><p><br ></p><p>&nbsp; &nbsp; // 输入年份和月份</p><p>&nbsp; &nbsp;&nbsp;</p><p><br ></p><p>&nbsp; &nbsp; printf(&quot;请输入年: &quot;);</p><p>&nbsp; &nbsp; scanf(&quot;%d&quot;, &amp;year);</p><p>&nbsp; &nbsp; &nbsp;printf(&quot;%d年&quot;,year);</p><p>&nbsp; &nbsp; &nbsp;for(month=1;month&lt;13;month++){</p><p>&nbsp; &nbsp; &nbsp;printCalendar(year, month);&nbsp;</p><p> printf(&quot;%s&quot;,poem[month-1]);</p><p>&nbsp; &nbsp; &nbsp;puts(&quot;\n&quot;);</p><p>&nbsp; &nbsp; &nbsp;if(month%4==0){</p><p>&nbsp; &nbsp; &nbsp; printf(&quot;\n\n\n\n&quot;);</p><p>&nbsp; &nbsp; &nbsp;}</p><p>&nbsp; &nbsp; &nbsp;</p><p>&nbsp;&nbsp;</p><p>&nbsp; &nbsp; </p><p>&nbsp; &nbsp; &nbsp;}</p><p> </p><p>}</p><p><br ></p><p>&nbsp; &nbsp;</p><p><br ></p><p>int main() {</p><p>//f1_month();</p><p>//f2_year();</p><p>f3_jijie();</p><p>&nbsp; &nbsp; return 0;</p><p><br ></p><p>}</p><p><br ></p>