using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 编写日历
{
class Program
{
//判断一年是否为闰年
static private bool IsLeapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
//一个月的天数
static private int GetDayByMonth(int year,int month)
{
if (month < 1 || month > 12)
return 0;
switch (month)
{
case 2:
return IsLeapYear(year) ? 29 : 28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
//判断一年中月份的天数是在星期几
static private int GetWeekByDay(int year,int month,int day)
{
DateTime dt=new DateTime(year,month,day);
return (int)dt.DayOfWeek;
}
//打印月历
static private void PrintMonthCalender(int year,int month)
{
Console.WriteLine("{0}年{1}月", year, month);
Console.WriteLine("日\t一\t二\t三\t四\t五\t六\t");
int week=GetWeekByDay(year,month,1);
for(int i=0;i<week;i++)
{
Console.Write("\t");
}
int days = GetDayByMonth(year, month);
for(int i=1;i<=days;i++)
{
Console.Write(i + "\t");
if (GetWeekByDay(year, month, i) == 6)
Console.WriteLine();
}
}
//打印年历
static private void PrintYearCalender(int year)
{
for(int i=1;i<=12;i++)
{
PrintMonthCalender(year,i);
Console.WriteLine();
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入年份:");
int year = int.Parse(Console.ReadLine());
PrintYearCalender(year);
Console.ReadLine();
}
}
}