110,545
社区成员
发帖
与我相关
我的任务
分享
SU MO TU WE TH FR SA
01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string calendar = "";
// 在这里编写代码
Console.WriteLine(calendar);
}
}
}
static void Main()
{
string calendar = "";
int year = 2011;
int month = 6;
Calendar a = new Calendar(year, month);
calendar = a.Write();
Console.WriteLine(calendar);
}
class Calendar : IFormattable
{
static readonly string formatStr;
static Calendar()
{
string s = new string('1', 6).Replace("1", "{0} ") + "{0}\n";
formatStr = "SU MO TU WE TH FR SA\n";
formatStr += new string('1', 6).Replace("1", s);
}
public Calendar(int year, int month)
{
this.week = (int)new DateTime(year, month, 1).DayOfWeek * -1;
this.max = DateTime.DaysInMonth(year, month);
}
int week, max;
public string ToString(string format, IFormatProvider formatProvider)
{
return week++ < 0 || week > max ? " " : week.ToString("00");
}
public string Write()
{
return string.Format(formatStr, this).TrimEnd();
}
}