類別程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW2
{
class MyDateTime
{
//field
public int Year;
public int Month;
public int Day;
//Method
public bool IsLeap()
{
if (Year % 4 == 0 && Year % 100 != 0)
return true;
else if (Year % 400 == 0)
return true;
else
return false;
}
public void Print()
{
Console.WriteLine("{0}/{1}/{2}", Year, Month, Day);
}
public int DaysOfYear()
{
if(IsLeap () == true)
return 366;
else
return 365;
}
public int DaysOfMonth()
{
if (Month == 2)
{
if (IsLeap() == true)
{
return 29;
}
else
return 28;
}
else if (Month % 2 == 0 && Month >= 8)
return 31;
else if (Month % 2 == 1 && Month <= 7)
return 31;
else
return 30;
}
public bool isLegal()
{
if (Day > 31 || Month > 12)
return false;
else if (Day == 31 && Month % 2 == 0)
{
if ((Month == 8) || (Month == 10) || (Month == 12))
return false;
else
return false;
}
else if (Day == 31 && Month % 2 == 1)
{
if ((Month == 9) || (Month == 11))
return false;
else
return true;
}
else if (Day >= 30 && Month == 2)
return false;
else if (Day == 29 && Month == 2)
{
if (Year % 4 == 0)
{
if (Year % 100 == 0 && Year % 400 != 0)
return false;
else
return true;
}
else
return false;
}
else
return true;
}
public string WeekDay()
{
if (Month == 1 || this.Month == 2)
{
Month += 12;
Year--;
}
int week = (Day + 2 * Month + 3 * (Month + 1) / 5 + Year + Year / 4 - Year / 100 + Year / 400 + 1) % 7;
switch (week)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 0:
return "Sunday";
default:
return "false";
}
}
}
}
主程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW2
{
class Program
{
static void Main(string[] args)
{
MyDateTime date = new MyDateTime();
while (true)
{
Console.WriteLine("Input Year");
date.Year = int.Parse(Console.ReadLine());
Console.WriteLine("Input Month");
date.Month = int.Parse(Console.ReadLine());
Console.WriteLine("Input Day");
date.Day = int.Parse(Console.ReadLine());
if (date.isLegal() == false)
{
Console.WriteLine("{0},{1},{2} is not a legal date!", date.Year, date.Month, date.Day);
Console.WriteLine("Input Again!");
Console.ReadLine();
Console.Clear();
continue;
}
Console.WriteLine("************************************************************");
switch (date.IsLeap())
{
case true:
Console.WriteLine("Year {0} is a leap year!", date.Year);
break;
case false:
Console.WriteLine("Year {0} is not a leap year!", date.Year);
break;
}
Console.WriteLine("Days of year {0} = {1}", date.Year, date.DaysOfYear());
Console.WriteLine("Days of {0}/{1} = {2}", date.Year, date.Month, date.DaysOfMonth());
Console.WriteLine("{0}/{1}/{2} is {3}", date.Year, date.Month, date.Day, date.WeekDay());
Console.WriteLine("************************************************************");
Console.WriteLine("Again? Y/N");
char select = Convert.ToChar(Console.ReadLine());
if (select != ('y') && select != ('Y'))
{
break;
}
Console.Clear();
}
Console.WriteLine("Program end!");
Console.ReadLine();
}
}
}
沒有留言:
張貼留言