using System;
namespace Calendar
{
class Program
{
static void Main(string[] args)
{
int year = int.Parse(Console.ReadLine());
//int month = int.Parse(Console.ReadLine());
Console.WriteLine("{0}年年历",year);
PrintYearDay(year);
}
/// <summary>
/// 判断是不是闰年(二月有多少天)
/// </summary>
/// <param name="year">输入年份</param>
/// <returns>返回二月有多少天</returns>
private static int JudgeFeburaryMonth(int year)
{
//能除尽4 并且除以100不等于0为闰年
if (year % 4 == 0 && year % 100 != 0) return 29;
else if (year % 400 == 0) return 29;
else return 28;
}
/// <summary>
/// 判断一个月多少天
/// </summary>
/// <param name="month">月份</param>
/// <param name="year">年份</param>
/// <returns>返回当月有多少天</returns>
private static int JudgeMonthDay(int month, int year)
{
//非法年份返回0;
if (month < 1 || month > 12) return 0;
//1、3、5、7、8、10、12有31天
//其余有30天
switch (month)
{
case 1:
return 31;
break;
case 2:
return JudgeFeburaryMonth(year);//调用判断闰年的方法判断二月有多少天
break;
case 3:
return 31;
break;
case 4:
return 30;
break;
case 5:
return 31;
break;
case 6:
return 30;
break;
case 7:
return 31;
break;
case 8:
return 31;
break;
case 9:
return 30;
break;
case 10:
return 31;
break;
case 11:
return 30;
break;
case 12:
return 31;
break;
default:
return 0;
}
}
/// <summary>
/// 判断星期几
/// </summary>
/// <param name="year">年份</param>
/// <param name="month">月份</param>
/// <param name="day">几号</param>
/// <returns>返回星期几</returns>
private static int JudgeWeekByday(int year, int month, int day)
{
if (month < 1 || month > 12) return 0;//非法月返回0
DateTime da = new DateTime(year, month, day);
return (int)da.DayOfWeek;
}
/// <summary>
/// 打印指定年份指定月份的日历
/// </summary>
/// <param name="year">年份</param>
/// <param name="month">月份</param>
private static void PrintDay(int year, int month)
{
Console.WriteLine("{0}年{1}月", year, month); //输出表头
Console.WriteLine("日\t一\t二\t三\t四\t五\t六"); //输出表头
int week = JudgeWeekByday(year, month, 1); //判断输入的年份和月份的第一天是星期几
int countday = JudgeMonthDay(month, year); //判断输入的月份有多少天
int count = 1; //为了每到周六换行的计数变量
for (int i = 1; i <= countday; i++)
{
int week1 = JudgeWeekByday(year, month, count); //通过计数变量判断现在打印到星期几了
if (week == 0) //第一天为周末的情况
{
Console.Write("{0}\t", i);
if (week1 == 6) //如果打印到周六就换行
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 1)
{
if (count == 1) { Console.Write("\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 2)
{
if (count == 1) { Console.Write("\t\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 3)
{
if (count == 1) { Console.Write("\t\t\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 4)
{
if (count == 1) { Console.Write("\t\t\t\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 5)
{
if (count == 1) { Console.Write("\t\t\t\t\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
if (week == 6)
{
if (count == 1) { Console.Write("\t\t\t\t\t\t"); }
Console.Write("{0}\t", i);
if (week1 == 6)
{
Console.WriteLine();
}
count += 1;
continue;
}
}
}
/// <summary>
/// 打印指定年份年历
/// </summary>
/// <param name="year">输入年份</param>
private static void PrintYearDay(int year)
{
for (int month = 1; month <= 12; month++)
{
switch (month)
{
case 1:
PrintDay(year, 1);
Console.WriteLine();
break;
case 2:
PrintDay(year, 2);
Console.WriteLine();
break;
case 3:
PrintDay(year, 3);
Console.WriteLine();
break;
case 4:
PrintDay(year, 4);
Console.WriteLine();
break;
case 5:
PrintDay(year, 5);
Console.WriteLine();
break;
case 6:
PrintDay(year, 6);
Console.WriteLine();
break;
case 7:
PrintDay(year, 7);
Console.WriteLine();
break;
case 8:
PrintDay(year, 8);
Console.WriteLine();
break;
case 9:
PrintDay(year, 9);
Console.WriteLine();
break;
case 10:
PrintDay(year, 10);
Console.WriteLine();
break;
case 11:
PrintDay(year, 11);
Console.WriteLine();
break;
case 12:
PrintDay(year, 12);
Console.WriteLine();
break;
}
}
}
}
}
初学者不喜勿喷。(代码能运行)