1. 程式人生 > >C# 通過各個函數實現控制臺日歷

C# 通過各個函數實現控制臺日歷

tex 年總 summary lda 月的天數 clas tostring pac urn

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(); } } }

C# 通過各個函數實現控制臺日歷