日历

此次以1900年到2100年的日历为案例;首先给日历建立一个数组集合;日历的关键字包括年、月、日以及星期;
首先我们需要判断年的属性,年有平年和闰年的区分,所以先判断年的属性,判断闰年的条件是能整除4并且不能整除100,或者能整除400,这就能判断闰年和平年;
然后年的属性下月份的属性也需要判断,先判断年下的2月份,分为平年28天和闰年29,所以设定在2月,闰年输出的天数和平年输出的天数,再然后判断其他月份
在7月份之前,奇数月都是31天,偶数月都是30天,而7月以后奇数月都是30天,偶数月是31天,可以一次内容设立判断语句,区分输出30天和31天;
然后计算年份穿过的总的天数,然后根据天数将日历中7天一星期输出,而过7天一星期,下一天就跳格,所以写作crossdays%7+1.分为7天一周,然后根据周数将每一周添加到日历排列中;
上面是穿过的天数的计算,然后根据2月份的属性定义日历表中的天数,天数的算法和上述穿过的天数算法相同,能够根据年份和月份输出日历表中一个月份的天数输出,最后制作日历表格
其中当能整除7的时候,输出空格并跳行,就能是表格的数据排列出来;
具体实现代码如下:

List<string> dates = new List<string>();
int crossDaysOfYear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
crossDaysOfYear += 366;
}
else
{
crossDaysOfYear += 365;
}
}
int crossDaysOfMonth = 0;
for (int i = 1; i < month; i++)
{
if (i == 2)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
crossDaysOfMonth += 29;
}
else
{
crossDaysOfMonth += 28;
}
}
else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0)
{
crossDaysOfMonth += 31;
}
else
{
crossDaysOfMonth += 30;
}
}
int crossDays = crossDaysOfYear + crossDaysOfMonth;
int dayOfWeek = crossDays % 7 + 1;
int space = dayOfWeek - 1;
for (int i = 0; i < space; i++)
{
dates.Add("");
}

int days;
if (month == 2)
{
if (year % 4 == 0 && year % 10 != 0 || year % 400 == 0)
{
days = 29;
}
else
{
days = 28;
}
}
else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0)
{
days = 31;
}
else
{
days = 30;
}
for (int i = 1; i <= days; i++)
{
dates.Add(i.ToString());
}
Console.WriteLine("******************************************************");
Console.Write("一\t二\t三\t四\t五\t六\t日");
for (int i = 0; i < dates.Count; i++)
{
if (i % 7 == 0)
{
Console.WriteLine();
}
Console.Write(dates[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("******************************************************");
Console.ReadLine();

posted @ 2017-08-20 16:39  尾巴也不烂  阅读(370)  评论(0)    收藏  举报