万年历:是记录一定时间范围内(比如100年或更多)的具体阳历或阴历的日期的年历,方便有需要的人查询使用。

   在用C#语言编写万年历的时候,我们应注意到:日历的表头和主体。再根据要得到的数据生成具体的函数,并将函数体写完整。最后调用,生成具体的哪一年哪一月的日历。(条件:1900年1月1号是星期一)

注意:计算1号是星期几的时候要计算他之前的所有天数;

     每个月里面的日期要注意换行;

     每两个月之间也要换行。

using System;using System.Collections.Generic;using System.Text;namespace prjPerCalender{public class MyCalender//我的日历类{static void Main(string[] args){MyCalender r = new MyCalender();//先创建一个新对象,并将它给它的引用rr.PrintYearCalender(2013);//调用打印年日历的方法,打印具体某一年的日历。}public void PrintMonthCalender(int year, int month)//打印月日历的方法,用来得到月日历。{PrintMonthHeader(year, month);//调用打印月日历的头文件的方法。PrintMonthBody(year, month);//调用打印月日历的主体的方法。}private void PrintMonthBody(int year, int month)//打印月日历的主体的方法。{//打印月日历就要得到这个月的天数,调用计算每月有多少天这个方法来得到这个月有多少天。int days = GetMonthDays(year, month);//要知道每个月的1号是星期几,调用得到每个月的1号是星期几的方法来实现。int week = GetWeekDayByFirstDayOfMonth(year, month);//每个月的1号是星期几就在它前面的几天下面都输入空格。for (int i = 0; i < week; i++){Console.Write("\t");}//,将这个月的每一天依次打印,这个月里面的某一天加上1号的week对7求余等于0的话,就要换行。for (int i = 1; i <= days; i++){Console.Write("{0}\t", i);if ((i + week) % 7 == 0){Console.Write("\n");}}//每个月的日历打印完成后要换行打印下一个月的日历。Console.Write("\n");}//每个月的1号是星期几的方法private int GetWeekDayByFirstDayOfMonth(int year, int month){//要计算某个月1号是星期几,先要计算出从1900年1月1号开始到这个月的1号之前有多少天。int days = 0;//计算1900年到要计算的这一年的整年的天数。for (int i = 1900; i < year; i++){days += GetYearDays(i);}//计算这一年这个月1号前的天数。for (int i = 1; i < month; i++){days += GetMonthDays(year, i);}//用总天数对7求余+1再对7求余得到1号是星期几。int week = (days % 7 + 1) % 7;//返回week.return week;}//计算某一年有多少天,闰年366,平年365;private int GetYearDays(int year){return IsLeapYear(year) ? 366 : 365;}//计算某个月的天数,1,3,5,7,8,10,12月31天,4,6,9,11月30天,闰年2月29天,平年2月28天。private int GetMonthDays(int year, int month){switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:if (IsLeapYear(year)){return 29;}return 28;default:return 0;}}//判断这一年是否是闰年private bool IsLeapYear(int year){return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;}//打印月日历的表头private void PrintMonthHeader(int year, int month){//先输出某年某月,Console.WriteLine("{0}年{1}月", year, month);//再从星期天开始到星期一依次打印。Console.WriteLine("周日\t周一\t周二\t周三\t周四\t周五\t周六");}//打印年日历,把这一年12个月的日历都打印出来。public void PrintYearCalender(int year){for (int i = 1; i < 13; i++){//调用打印月日历的方法。PrintMonthCalender(year, i);}}}}

004801226.png

心情愉快。。。。。。