2015年4月23日 星期四

[2015][Quiz]MidExam_20150423_60373006H

namespace MyDateTime
{
    class MyDateTime
    {
        private int _year;
        private int _month;
        private int _day;

        public int Year
        {
            set { _year = value; }
            get { return _year; }
        }
        public int Month
        {
            set { _month = value; }
            get { return _month; }
        }
        public int Day
        {
            set { _day = value; }
            get { return _day; }
        }
        public TextBox Calender;

        public MyDateTime()
        {
            Calender = new TextBox();
            _year = 0;
            _month = 0;
            _day = 0;
        }
        public MyDateTime(int year, int month, int day)
        {
            Calender = new TextBox();
            _year = year;
            _month = month;
            _day = day;
        }

        public bool IsLeap()
        {
            if ((_year % 4 == 0) && (_year % 100) != 0 || (_year % 400) == 0)
                return true;
            else
                return false;
        }
        public int DaysofMonth()
        {
            if ((1 <= _month && _month <= 12) && _month != 2)
            {
                if (_month <= 7)
                {
                    if ((_month % 2) == 1)
                        return 31;
                    else
                        return 30;
                }
                else
                {
                    if ((_month % 2) == 1)
                        return 30;
                    else
                        return 31;
                }
            }
            else
            {
                if (IsLeap() == true)
                    return 29;
                else
                    return 28;
            }
        }
        public bool IsValid()
        {
            if (_day <= DaysofMonth())
                return true;
            else
                return false;
        }
        public int WeekDay()
        {
            double weekDay;
            int month = _month;
            int year = _year;
            int day = _day;
            if (month == 1)
            {
                month = 13;
                year -= 1;
            }
            else if (month == 2)
            {
                month = 14;
                year -= 1;
            }
            int frontOfYear = year / 100;
            int behindOfYear = year % 100;
            weekDay = behindOfYear + Math.Floor((double)behindOfYear / 4) + Math.Floor((double)frontOfYear / 4) -
                      (2 * frontOfYear) + ((26 * (month + 1)) / 10) + day - 1;
            weekDay %= 7;
            if (weekDay < 0)
                weekDay = (weekDay % 7 + 7) % 7;        
            return (int)weekDay; 
        }
        public string Print()
        {
            string output = "";
            output += _year + "/" + _month + "/" + _day;
            return output;
        }
        public int WeekDay(int inputday)
        {
            double weekDay;
            int month = _month;
            int year = _year;
            int day = _day;
            if (month == 1)
            {
                month = 13;
                year -= 1;
            }
            else if (month == 2)
            {
                month = 14;
                year -= 1;
            }
            int frontOfYear = year / 100;
            int behindOfYear = year % 100;
            weekDay = behindOfYear + Math.Floor((double)behindOfYear / 4) + Math.Floor((double)frontOfYear / 4) -
                      (2 * frontOfYear) + ((26 * (month + 1)) / 10) + inputday - 1;
            weekDay %= 7;
            if (weekDay < 0)
                weekDay = (weekDay % 7 + 7) % 7;
            return (int)weekDay; 
        }
        public void PrintCalender()
        {
            Calender.AppendText(_year + "/" + _month + "\n");
            Calender.AppendText("日\t一\t二\t三\t四\t五\t六\n");
            for (int i = 0; i < WeekDay(1); i++)
            {
                Calender.AppendText(" \t");
            }
            for (int i = 1; i <= DaysofMonth(); i++)
            {
                Calender.AppendText(i.ToString() + "\t");
                if ((WeekDay(i) % 6) == 6)
                    Calender.AppendText("\n");
            }
        }
    }
}
namespace MyDateTime
{
    public partial class Form1 : Form
    {
        MyDateTime date;
        public Form1()
        {
            date = new MyDateTime();
            InitializeComponent();
        }

        private void btn_Excute_Click(object sender, EventArgs e)
        {
            txt_PrintRuselt.Clear();
            date.Year = Convert.ToInt16(txt_Year.Text);
            date.Month = Convert.ToInt16(cbx_Month.SelectedItem) ;
            date.Day = Convert.ToInt16(cbx_Day.SelectedItem);
            if (rad_IsLeapYear.Checked)
            {
                txt_PrintRuselt.Text += date.Print();
                if (date.IsLeap() == true)
                    txt_PrintRuselt.Text += "閏年";
                else
                    txt_PrintRuselt.Text += "非閏年";
            }
            else if (rad_IsValid.Checked)
            {
                txt_PrintRuselt.Text += date.Print();
                if (date.IsValid() == true)
                    txt_PrintRuselt.Text += "合法";
                else
                    txt_PrintRuselt.Text += "不合法";
            }
            else if (rad_WeekDay.Checked)
            {
                txt_PrintRuselt.Text += date.Print();
                if (date.WeekDay() == 0)
                    txt_PrintRuselt.Text += " 星期日";
                else
                    txt_PrintRuselt.Text += " 星期" + date.WeekDay();
                
            }
            else if (rad_Calender.Checked)
            {
                date.Calender = txt_PrintRuselt;
                date.PrintCalender();
            }
        }
    }
}

沒有留言:

張貼留言