2015年3月26日 星期四

[2015][Homework]Team02 - Hw01 (Revised)

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

namespace Hw1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int year; 
            int month; 
            int day;
            int select;
            while (true)
            {
                Console.WriteLine("請輸入題號1~6來執行以下題目");
                Console.WriteLine("第1題 阿姆斯壯數");
                Console.WriteLine("第2題 判斷閏年");
                Console.WriteLine("第3題 判斷天數");
                Console.WriteLine("第4題 判斷星期幾");
                Console.WriteLine("第5題 判斷日期是否合法");
                Console.WriteLine("第6題 判斷日期相差天數");
                Console.WriteLine("請輸入非1~6的數字來結束程式");
                select = int.Parse(Console.ReadLine());
                switch (select)
                {
                    case 1:
                        //第1題 阿姆斯壯數
                        int a = 0;
                        int b = 0;
                        int c = 0;
                        double sum = 0;
                        Console.WriteLine("顯示100~999間的阿姆斯壯數");
                        for (int i = 100; i <= 999; i++)
                        {
                            a = i / 100;
                            b = i / 10 % 10;
                            c = i % 10;
                            sum = Math.Pow((double)a, 3) + Math.Pow((double)b, 3) + Math.Pow((double)c, 3);
                            if (sum == i)
                                Console.WriteLine("{0}", i);
                        }
                        
                        break;
                    case 2:
                        //第2題 判斷閏年
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        if (year % 4 == 0 && year % 100 != 0)
                            Console.WriteLine("{0}年是閏年", year);
                        else if (year % 400 == 0)
                            Console.WriteLine("{0}年是閏年", year);
                        else
                            Console.WriteLine("{0}這年不是閏年", year);
                        
                        break;
                    case 3:
                        //第3題 判斷天數
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        if (month > 12)
                            Console.WriteLine("這不是月份,呆呆!");
                        else if (month == 2)//閏年2月有29天
                        {
                            if (year % 4 == 0 && year % 100 != 0)
                                Console.WriteLine("{0}年的{1}月有29天", year, month);
                            else if (year % 400 == 0)
                                Console.WriteLine("{0}年的{1}月有29天", year, month);
                            else
                                Console.WriteLine("{0}年的{1}月有28天", year, month);
                        }
                        else if (month % 2 == 0 && month >= 8)
                            Console.WriteLine("這個月有31天");
                        else if (month % 2 == 1 && month <= 7)//基數月有31天
                            Console.WriteLine("這個月有31天");
                        else
                            Console.WriteLine("這個月有30天");
                       
                        break;

                    case 4:
                        //第4題 判斷星期幾
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入日期");
                        day = int.Parse(Console.ReadLine());
                        int week;
                        //基姆拉爾森公式,需將1月和2月看成是上一年的13月和14月
                        if (month == 1 || month == 2)
                        {
                            month += 12;
                            year--;
                        }
                        week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7;
                        switch (week)
                        {
                            case 1:
                                Console.WriteLine("這天星期一");
                                break;
                            case 2:
                                Console.WriteLine("這天星期二");
                                break;
                            case 3:
                                Console.WriteLine("這天星期三");
                                break;
                            case 4:
                                Console.WriteLine("這天星期四");
                                break;
                            case 5:
                                Console.WriteLine("這天星期五");
                                break;
                            case 6:
                                Console.WriteLine("這天星期六");
                                break;
                            case 0:
                                Console.WriteLine("這天星期天");
                                break;
                        }
                        
                        break;

                    case 5:
                        //第5題 判斷日期合法與否
                        Console.WriteLine("請輸入年分");
                        year = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入月分");
                        month = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入日期");
                        day = int.Parse(Console.ReadLine());
                        if (day > 31 || month > 12)
                            Console.WriteLine("此日期不合法");
                        else if (day == 31 && month % 2 == 0)
                        {
                            if ((month == 8) || (month == 10) || (month == 12))
                                Console.WriteLine("此日期合法");
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else if (day == 31 && month % 2 == 1)
                        {
                            if ((month == 9) || (month == 11))
                                Console.WriteLine("此日期不合法");
                            else
                                Console.WriteLine("此日期合法");
                        }
                        else if (day >= 30 && month == 2)
                            Console.WriteLine("此日期不合法");
                        else if (day == 29 && month == 2)
                        {
                            if (year % 4 == 0)
                            {
                                if (year % 100 == 0 && year % 400 != 0)
                                    Console.WriteLine("此日期不合法");
                                else
                                    Console.WriteLine("此日期合法");
                            }
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else
                            Console.WriteLine("此日期合法");
                       
                        break;

                    case 6:
                        //第6題 判斷日期相差天數
                        int sumYear = 0;
                        int sumDay1 = 0;
                        int sumDay2 = 0;
                        int day1;
                        int day2;
                        int monthDay;
                        int leapYear = 0;
                        Console.WriteLine("請輸入第一個日期:範例:20150210");
                        int inputDate1 = int.Parse(Console.ReadLine());
                        Console.WriteLine("請輸入第二個日期:範例:20150416");
                        int inputDate2 = int.Parse(Console.ReadLine());
                        int inputYear1 = inputDate1 / 10000;
                        int inputMonth1 = inputDate1 % 10000 / 100;
                        int inputDay1 = inputDate1 % 100;
                        int inputYear2 = inputDate2 / 10000;
                        int inputMonth2 = inputDate2 % 10000 / 100;
                        int inputDay2 = inputDate2 % 100;
                        for (day1 = 1; day1 < inputDay1; day1++) ;
                        for (day2 = 1; day2 < inputDay2; day2++) ;
                        //date1
                        if ((inputYear1 % 4 == 0 && inputYear1 % 100 != 0) || (inputYear1 % 400 == 0))//潤
                        {
                            for (month = 1; month < inputMonth1; month++)
                            {
                                if (month == 2)
                                    monthDay = 29;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay1 = sumDay1 + monthDay;
                            }

                        }
                        else
                        {
                            for (month = 1; month < inputMonth1; month++)
                            {
                                if (month == 2)
                                    monthDay = 28;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay1 = sumDay1 + monthDay;
                            }
                        }
                        sumDay1 = sumDay1 + day1;
                        //date2
                        if ((inputYear2 % 4 == 0 && inputYear2 % 100 != 0) || (inputYear2 % 400 == 0))//潤
                        {
                            for (month = 1; month < inputMonth2; month++)
                            {
                                if (month == 2)
                                    monthDay = 29;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay2 = sumDay2 + monthDay;
                            }

                        }
                        else
                        {
                            for (month = 1; month < inputMonth2; month++)
                            {
                                if (month == 2)
                                    monthDay = 28;
                                else if (month <= 8 && month % 2 == 1)
                                    monthDay = 31;
                                else if (month <= 8 && month % 2 == 0)
                                    monthDay = 30;
                                else if (month > 8 && month % 2 == 0)
                                    monthDay = 30;
                                else
                                    monthDay = 31;
                                sumDay2 = sumDay2 + monthDay;
                            }
                        }
                        sumDay2 = sumDay2 + day2;
                        //year
                        for (year = inputYear1; year < inputYear2; year++)
                        {

                            if (year % 4 == 0 && year % 100 != 0)//潤
                            {

                                leapYear++;

                            }
                            else if (year % 400 == 0)
                            {

                                leapYear++;
                            }
                            else
                                sumYear++;

                        }
                        sumYear = sumYear * 365 + leapYear * 366;
                        Console.WriteLine("{0}天", sumYear - sumDay1 + sumDay2 - 1);

                        break;
                    default:
                        return;
                }
                Console.ReadKey();
                Console.Clear();
            }

        }
    }
}

2 則留言: