2015年3月18日 星期三

[2015][Homework]Team04 - Hw01



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, b, 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, 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, year, month, day,NumOfDate,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;
                }
                //計算星期幾的公式。
                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, month, 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, TemptMonth, TemptDay;
            int StartYear,StartMonth,StartDay;
            int EndYear, EndMonth, EndDay;
            int DAYS= 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;
                DAYS++;                                                       
            }
            Console.WriteLine("{0}/{1}/{2}~~~{3}/{4}/{5}有{6}天", StartYear, StartMonth, StartDay, EndYear, EndMonth, EndDay, DAYS);
            Console.ReadKey();        
        }

    }
}

8 則留言:

  1. Team3:

    =======
    line 306
    line 307
    line 308
    line 309 應當用camel case
    ======================
    line 60
    94
    131
    246
    306
    307
    308
    盡量換行
    ==============

    回覆刪除
  2. 定義變數會讓人看不懂
    NumOfDate,yearOf2

    回覆刪除
  3. 您為變數取的名字,大致上都有符合Pascal和camel 除了309行的DAYS,非常專業!但是有一些變數用Pascal 一些用camel如果能統一,會更好喔!

    回覆刪除
    回覆
    1. 謝謝!!
      基本上我都用Pascal當作命名標準,至於第六題我是從起始的第一天累計天數到截止日,所以用DAYS當作經過的天數,基本上英文單字當作大寫不是太大問題,但我會在revise後更改為CountDays。

      刪除
  4. 您為變數取的名字,大致上都有符合Pascal和camel 除了309行的DAYS,非常專業!但是有一些變數用Pascal 一些用camel如果能統一,會更好喔!

    回覆刪除