main program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw3
{
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();
}
return;
}
}
}
MyDateTime
using System;
namespace hw3
{
class MyDateTime
{
public int year;
public int month;
public int day;
public bool isLeap()
{
if (year % 4 == 0 && year % 100 != 0)
return true;
else if (year % 400 == 0)
return true;
else
return false;
}
public bool isLegal()
{
if (day > 31)
return false;
else if (day == 31 && month % 2 == 0)
{
if (month == 8 || month == 10 || month == 12)
return true;
else
return false;
}
else if (day == 30 && month == 2)
return false;
else if (day == 29)
{
if (isLeap())
return true;
else
return false;
}
else
return true;
}
public void print()
{
Console.WriteLine("{0}/{1}/{2}", year,month, day);
}
public int daysOfYear()
{
if (isLeap())
return 366;
else
return 365;
}
public int daysOfMonth()
{
if (month == 2)
if (isLeap())
return 29;
else
return 28;
else if (month % 2 == 1 || month == 8)
return 31;
else
return 30;
}
public string weekDay()
{
if (month == 1 || month == 2 )
{
year = year - 1;
month = month + 12;
}
switch ((day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7)
{
case (0):
return "Sunday";
case (1):
return "Monday";
case (2):
return "Tuesday";
case (3):
return "Wednesday";
case (4):
return "Thursday";
case (5):
return "Friday";
case (6):
return "Saturday";
default:
return "";
}
}
}
}
沒有留言:
張貼留言