using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalendarConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入年月");
int year = int.Parse(Console.ReadLine());
int month = int.Parse(Console.ReadLine());
int day = 0;
int sum = 0;
for(int i=1900;i<year;i++)
{
if(i%4==0&&i%100!=0||i%400==0)
{
sum+=366;
}
else
{
sum+=365;
}
}
switch(month)
{
case 12:
day = 31;
break;
case 11:
day = 30;
break;
case 10:
day = 31;
break;
case 9:
day = 30;
break;
case 8:
day = 31;
break;
case 7:
day = 31;
break;
case 6:
day = 30;
break;
case 5:
day = 31;
break;
case 4:
day = 30;
break;
case 3:
day = 31;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
day = 29;
else
day = 28;
break;
case 1:
day = 31;
break;
}
int leap;
/*先計算某月以前月份的總天數*/
switch (month)
{
case 1: sum += 0; break;
case 2: sum += 31; break;
case 3: sum += 59; break;
case 4: sum += 90; break;
case 5: sum += 120; break;
case 6: sum += 151; break;
case 7: sum += 181; break;
case 8: sum += 212; break;
case 9: sum += 243; break;
case 10: sum += 273; break;
case 11: sum += 304; break;
case 12: sum += 334; break;
}
/*判斷是不是閏年*/
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
leap = 1;
else
leap = 0;
/*如果是閏年且月份大於2,總天數應該加一天*/
if (leap == 1 && month > 2)
sum++;
int space = (sum+1)%7;
Console.WriteLine("日\t一\t二\t三\t四\t五\t六\t");
for(int i=1;i<=space+day;i++)
{
if(i<=space)
Console.Write("\t");
else
Console.Write(i-space+"\t");
if(i%7 == 0)
Console.WriteLine();
}
Console.WriteLine();
}
}
}
else
Console.Write(i-space+"\t");
if(i%7 == 0)
Console.WriteLine();
}
Console.WriteLine();
}
}
}
作者lin_lujian