using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 打印1993年到2018年每月12月7日是星期几
for (int year = 1900; year <= 2018; year++)
{
Console.WriteLine(year + "年的生日是星期:" + GetDayOfWeek(year, 12, 7));
}
Console.WriteLine();
// 打印用户输出的年份每个月的日历
int inputYear = GetUserInputYear();
for (int i = 1; i <= 12; i++)
{
Console.WriteLine(inputYear + "年" + i + "月");
PrintCalendar(inputYear, i, 1);
Console.WriteLine();
}
// 打印用户输入的年,月份日历
while (true)
{
int year2 = GetUserInputYear();
int month2 = GetUserInputMonth();
PrintCalendar(year2, month2, 1);
Console.WriteLine();
Console.Write("按回车键继续,退出输入Y后按回车键:");
if (Console.ReadLine() == "Y")
{
break;
}
Console.Clear();
}
Console.ReadLine();
}
#region 判断是不是闰年
static bool IsLead(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return true;
}
return false;
}
#endregion
#region 从1900年至year-1年的天数
static int GetDayOfYear(int year)
{
int dayOfYear = 0;
for (int i = 1900; i < year; i++)
{
//if (IsLead(year))
if(IsLead(i))
{
dayOfYear += 366;
// Console.Write(i + "闰年的天数:" + dayOfYear);
//Console.WriteLine();
}
else
{
dayOfYear += 365;
// Console.Write(i + "平年的天数:" + dayOfYear);
// Console.WriteLine();
}
}
// Console.Write("去年的天数:"+dayOfYear);
// Console.WriteLine();
return dayOfYear;
}
#endregion
#region year年1月至month-1月的天数
static int GetDayOfMonth(int year, int month)
{
int getDayOfMonth = 0;
for (int i = 1; i < month; i++)
{
if(i == 2)
{
if (IsLead(year))
{
getDayOfMonth += 29;
}
else
{
getDayOfMonth += 28;
}
}
else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0)
{
getDayOfMonth += 31;
}
else
{
getDayOfMonth += 30;
}
}
return getDayOfMonth;
}
#endregion
#region 计算今天是该年的第几天
static int GetDayOfOneYear(int year,int month,int day)
{
// Console.Write("今天是今年的第几天:" + GetDayOfMonth(year, month) + "+" + day);
return GetDayOfMonth(year, month) + day;
}
#endregion
#region 计算1900年1月日至现在一共经过了多少天
static int GetDayOfAll(int year, int month, int day)
{
return GetDayOfYear(year) + GetDayOfOneYear(year, month, day) - 1;
}
#endregion
#region 计算今天是今年的星期几
static int GetDayOfWeek(int year, int month, int day)
{
return GetDayOfAll(year,month,day) % 7 + 1;
}
#endregion
#region 判断month月有多少天
static int GetDayOfOneMonth(int year,int month)
{
int days;
if(month == 2)
{
if(IsLead(year))
{
days = 29;
}
else
{
days = 28;
}
}
else if(month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0)
{
days = 31;
}
else
{
days = 30;
}
return days;
}
#endregion
#region 用户输入的年份
/// <summary>
/// 得到用户输入的年份
/// </summary>
/// <returns></returns>
static int GetUserInputYear()
{
int year;
while (true)
{
Console.Write("请输入年份(1900-2100):");
year = int.Parse(Console.ReadLine());
if (year >= 1900 && year <= 2100)
{
break;
}
}
return year;
}
#endregion
#region 用户输入的月份
static int GetUserInputMonth()
{
int month;
while (true)
{
Console.Write("请输入月份(1-12):");
month = int.Parse(Console.ReadLine());
if (month > 0 && month <= 12)
{
break;
}
}
return month;
}
#endregion
#region 控制台日历
/// <summary>
/// 打印一个日历
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
static void PrintCalendar(int year, int month, int day)
{
List<string> nums = new List<string>();
int dayOfWeek = GetDayOfWeek(year, month, day);
int space = dayOfWeek - 1;
Console.WriteLine(space);
for (int i = 0; i < space; i++)
{
nums.Add("");
}
// 算天数,把字符串凑齐
int days = GetDayOfOneMonth(year, month);
for (int i = 1; i <= days; i++)
{
nums.Add(i.ToString());
}
Console.Write("一 二 三 四 五 六 日");
for (int i = 0; i < nums.Count; i++)
{
if (i % 7 == 0)
{
Console.WriteLine();
}
Console.Write(nums[i] + " ");
}
}
#endregion
}
}