天天看点

我用C#写的日历

//只要调用构造方法就可以啦

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

    /// <summary>

    /// 名称:万年历

    /// 制作人:唐增辉

    /// 时间:2009年11月21日

    /// </summary>

    public class Calendar

    {

        public Calendar()

        {

        }

        /// <summary>

        /// 构造方法

        /// </summary>

        /// <param name="year">年</param>

        public Calendar(int year)

        {

            for (int i = 1; i <= 12; i++)

            {

                Calendar c = new Calendar();

                c.year = year;

                c.month = i;

                c.day = 1;

                if (c.Year % 4 == 0 && c.Year % 100 != 0 || c.Year % 400 == 0)

                {

                    c.IsRun = true;

                }

                else

                {

                    c.IsRun = false;

                }

                c.dayByAll();

            }

        }

        /// <summary>

        /// 构造方法

        /// </summary>

        /// <param name="year">年</param>

        /// <param name="month">月</param>

        public Calendar(int year,int month)

        {

            for (int i = 1; i <= month; i++)

            {

                Calendar c = new Calendar();

                c.year = year;

                c.month = i;

                c.day = 1;

                if (c.Year % 4 == 0 && c.Year % 100 != 0 || c.Year % 400 == 0)

                {

                    c.IsRun = true;

                }

                else

                {

                    c.IsRun = false;

                }

                c.dayByAll();

            }

        }

        /// <summary>

        /// 构造方法

        /// </summary>

        /// <param name="year">年</param>

        /// <param name="month">月</param>

        /// <param name="day">日</param>

        public Calendar(int year, int month, int day)

        {

            this.year = year;

            this.month = month;

            this.day = day;

            if (this.Year % 4 == 0 && this.Year % 100 != 0 || this.Year % 400 == 0)

            {

                this.IsRun = true;

            }

            else

            {

                this.IsRun = false;

            }

            dayByAll();

        }

        private bool isRun;

        public bool IsRun

        {

            get { return isRun; }

            set { isRun = value; }

        }

        private int year;//年

        public int Year

        {

            get { return year; }

            set { year = value; }

        }

        private int month;//月

        public int Month

        {

            get { return month; }

            set { month = value; }

        }

        private int day;//日

        public int Day

        {

            get { return day; }

            set { day = value; }

        }

        private int daysByMonth;//本月天数

        public int DaysByMonth

        {

          get

          {

              return daysByMonth;

          }

          set { daysByMonth = value; }

        }

        private int daysByYears;//今年一月到本月的天数

        public int DaysByYears

        {

            get { return daysByYears; }

            set { daysByYears = value; }

        }

        private int allDays;//1900年到今年的天数

        public int AllDays

        {

            get { return allDays; }

            set { allDays = value; }

        }

        private String info;//返回相应的日历

        public String Info

        {

            get { return info; }

            set { info = value; }

        }

        /// <summary>今年1.1到本月1日的天数

        ///

        /// </summary>

        private void daysByYear(int i)

        {

            int days = 0;

            for (int j = 1; j <=i; j++)

            {

                switch (j)

                {

                    case 1:

                    case 3:

                    case 5:

                    case 7:

                    case 8:

                    case 10:

                    case 12:

                        this.DaysByMonth = 31;

                        break;

                    case 4:

                    case 9:

                    case 6:

                    case 11:

                        this.DaysByMonth = 30;

                        break;

                    case 2:

                        if (IsRun)

                        {

                            this.DaysByMonth = 29;

                        }

                        else

                        {

                            this.DaysByMonth = 28;

                        }

                        break;

                    default:

                        this.DaysByMonth = 30;

                        break;

                }

                if (i < month)

                {

                    //累加

                    days += this.DaysByMonth;

                     this.AllDays = days;

                }

            }

        }

       /// <summary> 计算1900年到今年的天数

       ///

       /// </summary>

        private String dayByAll()

        {

            String str = "";

            for(int i=1;i<this.Month;i++)

            {

                daysByYear(i);

            }

            for (int k = 1900; k < this.Year; k++)

            {

                if (k % 4 == 0 && k % 100 != 0 || k % 400 == 0)

                {

                    this.AllDays += 366;

                }

                else

                {

                    this.AllDays += 365;

                }

            }

            //Console.WriteLine("从1900年到今年有"+this.AllDays+"天");

           // Console.WriteLine("本月的天数" + this.DaysByMonth);

            str+="/t/t/t"+this.Year+"年"+this.Month+"月/t/t/t/n/n";

             str+="星期日/t星期一/t星期二/t星期三/t星期四/t星期五/t星期六/n";

            int space = (AllDays ) % 7 + 1;

            if (space == 7)

            {

                space = 0;

            }

                //本月一号是星期几?

                for (int i = 0; i < space; i++)

                {

                     str+="/t";

                }

                for (int i = 1; i <= this.DaysByMonth; i++)

                {

                    if (i == this.Day)

                    {

                        str+="【"+i+"】" + "/t";

                    }

                    else

                    {

                         str+=i + "/t";

                    }

                    if ((AllDays + i) % 7 == 6)

                    {

                        str+="/n";

                    }

                }

               Console.WriteLine(str+"/n");

               return str;

        }

    }

}