标签:readline eric div 第几天 [] ring col str space
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleCalendar { class Program { /// <summary> /// 判断指定年份是不是闰年 /// </summary> /// <param name="year">接收的年份</param> /// <returns>是闰年时,返回true</returns> static bool IsLeap(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; return false; } /// <summary> /// 得到某年某月的天数 /// </summary> /// <param name="year">接收的年份</param> /// <param name="month">接收的月份</param> /// <returns>返回天数</returns> static int GetDaysOfMonth(int year, int month) { if (month == 2) { if (IsLeap(year)) return 29; return 28; } else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0) return 31; return 30; } /// <summary> /// 计算某一年总共的天数 /// </summary> /// <param name="year">接收的年份</param> /// <returns>返回天数</returns> static int GetDaysOfYear(int year) { if (IsLeap(year)) return 366; return 365; } /// <summary> /// 根据提供的年月日,计算是这一年的第几天 /// </summary> /// <param name="year">接收的年份</param> /// <param name="month">接收的月份</param> /// <param name="day">第几天</param> /// <returns>返回是这一年的第几天</returns> static int GetDaysInYear(int year, int month, int day) { int inAllDays = 0; for (int i = 1; i <= month-1; i++) inAllDays += GetDaysOfMonth(year,i); return inAllDays + day; } /// <summary> /// 根据提供的年月日,计算从1900年1月1号,到这一天经过的总天数 /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <returns>返回总天数</returns> static int GetCrossDaysFrom1900(int year, int month, int day) { int allYearDays = 0; for (int i = 1900; i <= year-1; i++) allYearDays += GetDaysOfYear(i); return allYearDays + GetDaysInYear(year, month, day)-1; } /// <summary> /// 计算某年某月某日是星期几 /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <returns></returns> static int GetDayOfWeek(int year, int month, int day) { int daysFrom1900=GetCrossDaysFrom1900(year, month, day); int week = daysFrom1900 % 7 + 1; return week; } /// <summary> /// 得到用户输入的年份 /// </summary> /// <returns>返回输入的年份</returns> static int GetUserInputYear() { Console.Write("请输入一个年份:"); while (true) { int year = int.Parse(Console.ReadLine()); if (year < 1900 || year > 2100) Console.Write("输入有误,请重新输入:"); else return year; } } /// <summary> /// 得到用户输入的月份 /// </summary> /// <returns>返回输入的月份</returns> static int GetUserInputMonth() { Console.Write("请输入一个月份:"); while (true) { int month = int.Parse(Console.ReadLine()); if (month < 1 || month > 12) Console.Write("输入有误,请重新输入:"); else return month; } } /// <summary> /// 打印日历 /// </summary> /// <param name="year"></param> /// <param name="month"></param> static void PrintCalendar(int year,int month) { List<string> calendar = new List<string>(); int daysFrom1900 = GetCrossDaysFrom1900(year, month, 1); int space = daysFrom1900 % 7; for (int i = 0; i < space; i++) calendar.Add(""); for (int i = 1; i <= GetDaysOfMonth(year, month); i++) calendar.Add(i.ToString()); Console.WriteLine("**************************************************"); Console.WriteLine("一\t二\t三\t四\t五\t六\t日"); for (int i = 0; i < calendar.Count; i++) { if (i % 7 == 0 && i != 0) Console.WriteLine(); Console.Write(calendar[i]+"\t"); } Console.WriteLine(); Console.WriteLine("**************************************************"); } static void Main(string[] args) { PrintCalendar(GetUserInputYear(), GetUserInputMonth()); Console.ReadLine(); } } }
标签:readline eric div 第几天 [] ring col str space
原文地址:https://www.cnblogs.com/lithree/p/9195500.html