2015年3月26日 星期四

[2015][Homework]Team01 - Hw03

Main
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)
        {
            MyDataTime Data = new MyDataTime();
            while (true)
            {
                Console.WriteLine("請輸入年份(西元)");
                Data.Year = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入月份");
                Data.Month = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入日期");
                Data.Day = int.Parse(Console.ReadLine());
                if (Data.IsLegal() == false)
                {
                   Console.WriteLine("{0}年{1}月{2}日是不合法,請重新輸入", Data.Year, Data.Month, Data.Day);
                   Console.ReadLine(); 
                   Console.Clear();
                   continue;
                }
                else
                {
                    Console.WriteLine("********************************************");
                    if (Data.IsLeap())
                    {
                        Console.WriteLine("{0}年是閏年", Data.Year);
                    }
                    else
                    {
                        Console.WriteLine("{0}年不是閏年", Data.Year);
                    }
                    Console.WriteLine("{0}年有{1}天", Data.Year, Data.DayOfYear());
                    Console.WriteLine("{0}月有{1}天", Data.Month, Data.DayOfMonth());
                    Console.WriteLine("{0}年{1}月{2}日為{3}", Data.Year, Data.Month, Data.Day, Data.WeekDay());
                    Console.WriteLine("**************************************************");
                    Console.WriteLine("要再繼續嗎??(Y/N)");
                    char select = Convert.ToChar(Console.ReadLine());
                    if (select != ('Y') && select != ('y'))
                    {
                        break;
                    }
                    Console.Clear();
                }
            }

            Console.WriteLine("結束程式");
            Console.ReadLine();
        }
    }
}
class MyDataTime
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW3
{
    class MyDataTime
    {
        public int Year;
        public int Month;
        public int Day;

        public bool IsLeap ()
        {
            if (Year % 400 == 0 || (Year % 100 != 0 && Year % 4 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsLegal() 
        {
            if (Month >= 13 || Month < 1 || Day > 31 || Day < 1) 
            {
                return false;
            }
            else
            {
                switch (Month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (Day <= 31)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    case 2:
                        if (IsLeap() && Day <= 29)
                        {
                            return true;
                        }
                        else if (Day <= 28)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if (Day <= 30)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    default:
                        return false;
                }
            }
        }
        public int DayOfYear() 
        {
            if (IsLeap())
            {
                return 366;
            }
            else
            {
                return 365;
            }
        }
        public int DayOfMonth() 
        {
            switch (Month) 
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 2:
                    if (IsLeap())
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                default :
                    return 30;
            }
        }
        public string WeekDay()
        {
            if (Month == 1 || Month == 2)
            {
                Year--;
                Month += 12;
            }
            int week = (Day + 1 + 2 * Month + 3 * (Month + 1) / 5 + Year + Year / 4 - Year / 100 + Year / 400) % 7;
            switch (week)
            {
                case 0:
                    return "星期日";
                case 1:
                    return "星期一";
                case 2:
                    return "星期二";
                case 3:
                    return "星期三";
                case 4:
                    return "星期四";
                case 5:
                    return "星期五";
                default:
                    return "星期六";
            }
        }
    }
}

沒有留言:

張貼留言