2015年3月19日 星期四

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

Hw01 (Revised) 變數命名更改,增加變數註解。



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

namespace HW1
{
    class HW1
    {
        static void Main(string[] args)
        {          
            int questionNum;
            while (true)
            {
                Console.Clear();
                Console.WriteLine("請輸入題號(1)~(6),離開請按0");
                Console.WriteLine("(1)三位數符合阿姆斯壯數");
                Console.WriteLine("(2)輸入一個西元年份,判斷其為閏年或平年。");
                Console.WriteLine("(3)輸入一個西元年份及月份,印出該月的天數。");
                Console.WriteLine("(4)輸入年、月、日,印出該日是星期幾。");
                Console.WriteLine("(5)輸入年、月、日,判斷該日是否合法。");
                Console.WriteLine("(6)輸入起始年、月、日以及結束年、月、日,印出這兩個日期之間有多少日。");
                questionNum = int.Parse(Console.ReadLine());
                Console.WriteLine("--------------------");
                switch (questionNum)
                {
                    case 0:
                        break;
                    case 1:
                        HW1.Question_1();
                        break;
                    case 2:
                        HW1.Question_2();
                        break;
                    case 3:
                        HW1.Question_3();
                        break;
                    case 4:
                        HW1.Question_4();
                        break;
                    case 5:
                        HW1.Question_5();
                        break;
                    case 6:
                        HW1.Question_6();
                        break;
                    default:
                        Console.WriteLine("輸入錯誤");
                        break;
                }
                Console.ReadKey();
                
            }
        }




        static void Question_1()
        {
            int a;
            int b;
            int c;
            for (int i = 100; i <= 999; i++)
            {
                c = i % 10;//個位數
                b = (i % 100) / 10;
                a = i / 100;
                if (a * a * a + b * b * b + c * c * c == i)
                {
                    Console.WriteLine("{0}\n", i);
                }
            }
        }
        static void Question_2()
        {
            int year;
            while (true)
            {
                Console.WriteLine("請輸入年分:");
                year = int.Parse(Console.ReadLine());
                if (year < 0)
                    break;
                else
                {
                    if (year % 4 != 0 || ((year % 100 == 0) && (year % 400 != 0)))
                        Console.WriteLine("{0}是平年\n", year);
                    else
                        Console.WriteLine("{0}是閏年\n", year);
                }
            }
        }
        static void Question_3()
        {
            int year;
            int month;
            Console.WriteLine("題目:輸入一個西元年份及月份,印出該月的天數。");
            Console.WriteLine("輸入西元年份:");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("輸入月份:");
            month = int.Parse(Console.ReadLine());
            switch (month)
            { 
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine("西元{0}年{1}月有 31 天。", year, month);
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    Console.WriteLine("西元{0}年{1}月有 30 天。", year, month);
                    break;
                case 2:
                    if (year % 4 != 0 || ((year % 100 == 0) && (year % 400 != 0)))
                        Console.WriteLine("西元{0}年{1}月有 28 天。\n", year, month);  //平年
                    else
                        Console.WriteLine("西元{0}年{1}月有 29 天。\n", year, month); //閏年
                    break;
                default:
                    Console.WriteLine("輸入錯誤");
                    break;           
            }
        }
        static void Question_4()
        {
            int centry; 
            int year;
            int month;
            int day;
            int NumOfDate;
            int yearOf2;

            while (true)
            {
                Console.WriteLine("題目:輸入年、月、日,印出該日是星期幾。(日期須為1582年10月15日之後)");
   
                Console.WriteLine("輸入年份");
                year = int.Parse(Console.ReadLine());
                if (year < 1593)
                {
                    Console.WriteLine("輸入錯誤");
                    continue;
                }

                Console.WriteLine("輸入月份:");
                month = int.Parse(Console.ReadLine());            
                if (month > 12 || month < 1)
                {
                    Console.WriteLine("輸入錯誤");
                    continue;
                }
               
                Console.WriteLine("請輸日:");
                day = int.Parse(Console.ReadLine());
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if (day > 31||day<0)
                        {
                            Console.WriteLine("輸入錯誤");
                            continue;
                        }
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                         if (day > 30||day<0) 
                        {
                            Console.WriteLine("輸入錯誤");
                            continue;
                        }
                        break;
                    case 2:
                        if (year % 4 != 0 || ((year % 100 == 0) && (year % 400 != 0)))//平年
                        {
                            if (day > 28||day<0)//平年
                            {
                                Console.WriteLine("輸入錯誤");
                                continue;
                            }
                            break;
                        }
                        else
                        {
                            if (day > 29||day<0)//閏年
                            {
                                Console.WriteLine("輸入錯誤");
                                continue;
                            }
                            break;
                        }                                                 
                    default:
                        Console.WriteLine("輸入錯誤");
                        break;       
                }

                if (month == 1 || month == 2)
                {
                    month = month + 12;
                    year = year - 1;
                }    
           
                //yearOf2 是年份的末兩位(ex:19XX)。
                // NumOfDate 用來計算星期幾的參數。
                //centry 是年份的前兩位(ex:XX38)。
                //計算星期幾的公式。
                centry = (year / 100);
                yearOf2 = year%100;
                NumOfDate = yearOf2 + (yearOf2 / 4) + (centry / 4) - (2 * centry) + (26 * (month + 1) / 10) + day - 1;
                if (month == 13 || month == 14)
                {
                    month = month - 12;
                    year = year + 1;
                }
                switch(NumOfDate%7)
                {
                    case 1: 
                        Console.WriteLine("{0}年{1}月{2}日為星期一",year,month,day);
                        break;
                    case 2:
                        Console.WriteLine("{0}年{1}月{2}日為星期二", year, month, day);
                        break;
                    case 3:
                        Console.WriteLine("{0}年{1}月{2}日為星期三", year, month, day);
                        break;
                    case 4:
                        Console.WriteLine("{0}年{1}月{2}日為星期四", year, month, day);
                        break;
                    case 5:
                        Console.WriteLine("{0}年{1}月{2}日為星期五", year, month, day);
                        break;
                    case 6:
                        Console.WriteLine("{0}年{1}月{2}日為星期六", year, month, day);
                        break;
                    case 7:
                        Console.WriteLine("{0}年{1}月{2}日為星期日", year, month, day);
                        break;
                }
                break;
            }
           
        }
        static void Question_5()
        {
            int year;
            int month;
            int day;

            Console.WriteLine("題目:輸入年、月、日,判斷該日是否合法,");
            Console.WriteLine("輸入年份");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("輸入月份:");
            month = int.Parse(Console.ReadLine());
          
            Console.WriteLine("請輸日:");
            day = int.Parse(Console.ReadLine());
            if (month > 12 || month < 0||year<0)
                Console.WriteLine("{0}年{1}月{2}日為不合法日子",year ,month,day);
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if (day > 31 || day < 0)                  
                        Console.WriteLine("{0}年{1}月{2}日為不合法日子", year, month, day);
                    else
                        Console.WriteLine("{0}年{1}月{2}日為合法日子", year, month, day);               
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if (day > 30 || day < 0)
                        Console.WriteLine("{0}年{1}月{2}日為不合法日子", year, month, day);
                    else
                        Console.WriteLine("{0}年{1}月{2}日為合法日子", year, month, day);                    
                    break;
                case 2:
                    if (year % 4 != 0 || ((year % 100 == 0) && (year % 400 != 0)))//平年
                    {
                        if (day > 28 || day < 0)//平年                       
                            Console.WriteLine("{0}年{1}月{2}日為不合法日子", year, month, day);
                        else
                            Console.WriteLine("{0}年{1}月{2}日為合法日子", year, month, day);
                        break;
                    }
                    else
                    {
                        if (day > 29 || day < 0)//閏年                        
                            Console.WriteLine("{0}年{1}月{2}日為不合法日子", year, month, day);
                        else
                            Console.WriteLine("{0}年{1}月{2}日為合法日子", year, month, day);
                        break;
                    }
                default:
                    Console.WriteLine("{0}年{1}月{2}日為不合法日子", year, month, day);
                    break;       
            }                 
        }

        static void Question_6()
        {
            //用來暫存的年月日。
            int temptYear;
            int temptMonth;
            int temptDay;
            //起始的年月日。
            int startYear;
            int startMonth;
            int startDay;
            //終止的年月日。
            int endYear;
            int endMonth;
            int endDay;
            int countDays= 0;

            Console.WriteLine("計算兩個日期之間有多少天。");
            Console.WriteLine("請輸入起始年分:");
            startYear = int.Parse(Console.ReadLine());
            temptYear = startYear;
            Console.WriteLine("請輸入起始月分:");
            startMonth = int.Parse(Console.ReadLine());
            temptMonth = startMonth;
            Console.WriteLine("請輸入起始日:");
            startDay = int.Parse(Console.ReadLine());
            temptDay = startDay;
            Console.WriteLine("請輸入終止年分:");
            endYear = int.Parse(Console.ReadLine());
            Console.WriteLine("請輸入終止月分:");
            endMonth = int.Parse(Console.ReadLine());
            Console.WriteLine("請輸入終止日:");
            endDay = int.Parse(Console.ReadLine());

            while (true)
            {
                //如果起始日大於終止日
                //此方法沒有判斷不合法日子
                if (startYear > endYear || (startYear == endYear && startMonth > endMonth)||(startYear == endYear && startMonth == endMonth && startDay > endDay))
                {
                    Console.WriteLine("輸入錯誤");
                    break;
                }              
                temptDay++;
                if (temptMonth == 1 || temptMonth == 3 || temptMonth == 5 || temptMonth == 7 || temptMonth == 8 || temptMonth == 10 || temptMonth == 12)
                {
                    if (temptDay > 31)
                    {
                        temptDay = 1;
                        temptMonth++;
                    }
                }
                else if (temptMonth == 4 || temptMonth == 6 || temptMonth == 9 || temptMonth == 11)
                {
                    if (temptDay > 30)
                    {
                        temptDay = 1;
                        temptMonth++;
                    }
                }
                else if (temptMonth == 2)
                {
                    //平年
                    if (temptYear % 4 != 0 || ((temptYear % 100 == 0) && (temptYear % 400 != 0)))
                        //Console.WriteLine("{0}是平年\n", year);
                        if (temptDay > 28)
                        {
                            temptDay = 1;
                            temptMonth++;
                        }
                        else
                            //閏年
                            //Console.WriteLine("{0}是閏年\n", year);
                            if (temptDay > 29)
                            {
                                temptDay = 1;
                                temptMonth++;
                            }
                }
                if (temptMonth > 12)
                {
                    temptMonth = 1;
                    temptYear++;
                }
                if (temptYear == endYear && temptMonth == endMonth && temptDay == endDay)
                    break;
                countDays++;                                                       
            }
            Console.WriteLine("{0}/{1}/{2}~~~{3}/{4}/{5}有{6}天", startYear, startMonth, startDay, endYear, endMonth, endDay, countDays);
            Console.ReadKey();        
        }

    }
}


1 則留言:

  1. 1. NumOfDate ==>numOfDate
    2. yearOf2 命名有點怪
    3. day>31 || day <1 是不合法的判斷 應該可以拿到 case switch 外判斷

    回覆刪除