Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsMidExam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtOutcome.Clear();
}
private void btnRun_Click(object sender, EventArgs e)
{
MyDateTime timer = new MyDateTime();
timer.Year = int.Parse(txtYear.Text);
timer.Month = int.Parse(cBoxMonth.Text);
timer.Day = int.Parse(cBoxDay.Text);
if(rBtnIsLeap.Checked)
{
timer.IndexOfFunction = 1;
timer.Print();
txtOutcome.Text = timer.Output;
}
if(rBtnDayOfMonth.Checked)
{
timer.IndexOfFunction = 2;
timer.Print();
txtOutcome.Text = timer.Output;
}
if (rBtnWeekDay.Checked)
{
timer.IndexOfFunction = 3;
timer.Print();
txtOutcome.Text = timer.Output;
}
if (rBtnMoon.Checked)
{
timer.IndexOfFunction = 4;
timer.Print();
txtOutcome.Text = timer.Output;
}
}
}
}
MyDateTime.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsMidExam
{
class MyDateTime
{
private int _year;
private int _month;
private int _day;
private int _dayOfWeek;
private int _operatorY;
private int _operatorC;
private int _operatorM;
public string Output;
public int IndexOfFunction = 1;
public int Year
{
get {return _year;}
set { _year = value; }
}
public int Month
{
get { return _month; }
set { _month = value; }
}
public int Day
{
get { return _day; }
set { _day = value; }
}
public bool IsLeap()
{
if (_year % 400 == 0 || (_year % 4 == 0 && _year %100 != 0))
{
return true;
}
else
{
return false;
}
}
public int DaysOfMonth()
{
if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
{
return _day = 31;
}
else if (_month == 4 || _month == 6 || _month ==9 || _month == 11)
{
return _day = 30;
}
else if (_month ==2)
{
if (IsLeap())
{
return _day = 29;
}
else
{
return _day = 28;
}
}
else
{
return 0;
}
}
public bool IsValid()
{
if (_year < 1 || _day > 31)
{
return false;
}
else if ((_month == 4 || _month == 6 || _month == 9 || _month == 11) && _day > 30)
{
return false;
}
else if (IsLeap() && _month == 2 && _day > 29)
{
return false;
}
else if (!IsLeap() && _month ==2 && _day > 28)
{
return false;
}
else
{
return true;
}
}
public int WeekDay()
{
if(_month == 1 || _month == 2)
{
_operatorY = _year - 1;
}
else
{
_operatorY = _year;
}
_operatorC = _operatorY / 100;
_operatorY = _operatorY % 100;
_operatorM = (((_month + 9) % 12) + 1);
_dayOfWeek = (_day + Convert.ToInt16(Math.Floor(2.6 * Convert.ToDouble(_operatorM) - 0.2)) + _operatorY + Convert.ToInt16(Math.Floor(Convert.ToDouble(_operatorY) / 4)) + Convert.ToInt16(Math.Floor(Convert.ToDouble(_operatorC) / 4)) - 2 * _operatorC) % 7;
if(_dayOfWeek < 0)
{
_dayOfWeek += 7;
}
return _dayOfWeek;
}
public string Print()
{
switch(IndexOfFunction)
{
case 1:
if (IsLeap())
{
Output = _year.ToString() + "年是閏年";
}
else
{
Output = _year.ToString() + "年是平年";
}
break;
case 2:
if (IsValid())
{
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "合法";
}
else
{
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "不合法";
}
break;
case 3:
WeekDay();
switch(_dayOfWeek)
{
case 0:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期日";
break;
case 1:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期一";
break;
case 2:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期二";
break;
case 3:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期三";
break;
case 4:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期四";
break;
case 5:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期五";
break;
case 6:
Output = _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString() + "星期六";
break;
}
break;
case 4:
Output = "\t" + "Sun" + "\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thr" + "\t" + "Fri" + "\t" + "Sat";
Output += Environment.NewLine + "\t";
_day = 1;
WeekDay();
for (int i = 0; i < _dayOfWeek; i++)
{
Output += "\t";
}
Output += "1";
if ((_dayOfWeek + 1) % 7 == 0)
{
Output += Environment.NewLine;
}
for (int i = 2; i < DaysOfMonth() +1 ; i++)
{
if((_dayOfWeek + i) % 7 ==0)
{
Output += "\t" + i.ToString() + Environment.NewLine;
}
else
{
Output += "\t" + i.ToString();
}
}
break;
}
return Output;
}
}
}
沒有留言:
張貼留言