2015年3月23日 星期一

[2015][Homework]Team04 - Hw03

main


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2015OOP_HW3
{
    class HW03
    {
        static void Main(string[] args)
        {
            MyDateTime date = new MyDateTime();
            char select;
            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 date");
                date.day = int.Parse(Console.ReadLine());
                if (!date.isLegal())
                {
                    Console.WriteLine("{0}is not legal data!!",date.year);
                    Console.WriteLine("plz input again");
                    continue;
                }
                Console.WriteLine("======================");
                if (date.isLeap())
                {                  
                    Console.WriteLine(" {0}is a leap year!",date.year);
                }
                else
                {                    
                    Console.WriteLine("{0}is not a leap year!",date.year);
                }
                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?");
                select = Convert.ToChar(Console.ReadLine());
                if (select != 'y' && select != 'Y')
                {
                    break;
                }
            }
            Console.WriteLine("Program end!");
            Console.Read();
        }
    }
}



MyDateTime

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2015OOP_HW3
{
    class MyDateTime
    {
        public int year;
        public int month;
        public int day;

        public bool isLeap()
        {
            if (year % 4 != 0 || ((year % 100 == 0) && (year % 400 != 0)))
                return false;
            else
                return true;
        }
        public int daysOfYear()
        {
            if (isLeap())
                return 366;
            else
                return 365;
        }
        public int daysOfMonth()
        {
            switch (month)
            { 
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                case 2:
                    if (isLeap())
                        return 29;
                    else
                        return 28;
                default:
                    return 0;
            }
        }
        public bool isLegal()
        {
            if (month>12||month<1)
                return false;
            if(day<1||day>daysOfMonth())
                return false;
            return true;
        }
        public string weekDay()
        {
            int centry;  // 年份的前兩位(ex:XX15)。
            int yearOf2; //  是年份的末兩位(ex:19XX)。           
            int tempMonth;
            int tempYear;
            int date;
            tempMonth = month;
            tempYear = year;
            if (month == 1 || month == 2)
            {
                tempMonth = tempMonth+ 12;
                tempYear = tempYear - 1;
            }
            centry = tempYear / 100;
            yearOf2 = tempYear % 100;
            //蔡式公式
            date = (int)(yearOf2 + Math.Floor(yearOf2 / 4.0) + Math.Floor(centry/4.0) - (2 * centry) + Math.Floor(26 * (tempMonth + 1) / 10.0) + day - 1);
            switch (date % 7)
            {
                case 1:
                    return "Monday";
                case 2:
                    return "Tuesday";
                case 3:
                    return "wednesday";
                case 4:
                    return "Thrusday";
                case 5:
                    return "Friday";
                case 6:
                    return "Saturday";
                case 7:
                    return "Sunday";
                default:
                    return "wrong";
            }
        }
    }
}

沒有留言:

張貼留言