2015年3月18日 星期三

[2015][Homework]Team05 - Hw01

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string year, month, day,select;
            int y, m, d,sel;
            while (1==1)
            {
                Console.WriteLine("輸入1~6題號");
                Console.WriteLine("輸入其他數字以結束程式");
                select=Console.ReadLine();
                sel = int.Parse(select);
                switch(sel)
                {
                    case(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 % 10;
                        b = i / 10 % 10;
                        c = i / 100 % 10;
                        sum = Math.Pow((double)a, 3) + Math.Pow((double)b, 3) + Math.Pow((double)c, 3);
                        if (sum == i)
                            Console.WriteLine("{0}", i);
                        }
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case(2):
                        //第二題 判斷閏年
                        Console.WriteLine("輸入年分");
                        year = Console.ReadLine();
                        y = int.Parse(year);
                        if (y % 4 == 0 && y % 100 != 0)
                            Console.WriteLine("這年是閏年");
                        else if (y % 400 == 0)
                            Console.WriteLine("這年是閏年");
                        else
                            Console.WriteLine("這年不是閏年");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case(3):
                        //第三題 判斷天數
                        Console.WriteLine("輸入年分");
                        year = Console.ReadLine();
                        y = int.Parse(year);
                        Console.WriteLine("輸入月分");
                        month = Console.ReadLine();
                        m = int.Parse(month);
                        if (m > 12)
                            Console.WriteLine("這不是月份!");
                        else if (m == 2)
                            if (y % 4 == 0)
                                Console.WriteLine("這個月有29天");
                            else
                                Console.WriteLine("這個月有28天");
                        else if (m % 2 == 1 || m == 8)
                            Console.WriteLine("這個月有31天");
                        else
                            Console.WriteLine("這個月有30天");
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case(4):
                        //第四題 判斷星期幾
                        Console.WriteLine("輸入年分");
                        year = Console.ReadLine();
                        y = int.Parse(year);
                        Console.WriteLine("輸入月分");
                        month = Console.ReadLine();
                        m = int.Parse(month);
                        Console.WriteLine("輸入日期");
                        day = Console.ReadLine();
                        d = int.Parse(day);
                        int W;
                        if (m == 1)
                        {
                            y = y - 1;
                            m = 13;
                        }
                        else if (m == 2)
                        {
                            y = y - 1;
                            m = 14;
                        }
                        W = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
                        switch (W)
                        {
                            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;
                        }
                        Console.ReadKey();
                        Console.Clear();
                        break;
                        
                    case(5):
                        //第五題 判斷日期合法與否
                        Console.WriteLine("輸入年分");
                        year = Console.ReadLine();
                        y = int.Parse(year);
                        Console.WriteLine("輸入月分");
                        month = Console.ReadLine();
                        m = int.Parse(month);
                        Console.WriteLine("輸入日期");
                        day = Console.ReadLine();
                        d = int.Parse(day);
                        if (d > 31)
                            Console.WriteLine("此日期不合法");
                        else if (d == 31 && m % 2 == 0)
                        {
                            if (m == 8)
                                Console.WriteLine("此日期合法");
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else if (d == 30 && m == 2)
                            Console.WriteLine("此日期不合法");
                        else if (d == 29)
                        {
                            if (y % 4 == 0)
                            {
                                if (y % 100 == 0 && y % 400 != 0)
                                    Console.WriteLine("此日期不合法");
                                else
                                    Console.WriteLine("此日期合法");
                            }
                            else
                                Console.WriteLine("此日期不合法");
                        }
                        else
                            Console.WriteLine("此日期合法");
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    case(6):
                        //第六題 判斷日期相差天數
                        int totalday1 = 0, totalday2 = 0, diff = 0, leaps, tempsum;
                        for (int times = 1; times < 3; times++)
                        {
                            Console.WriteLine("輸入第{0}次年分", times);
                            year = Console.ReadLine();
                            y = int.Parse(year);
                            Console.WriteLine("輸入第{0}次月分", times);
                            month = Console.ReadLine();
                            m = int.Parse(month);
                            Console.WriteLine("輸入第{0}次日期", times);
                            day = Console.ReadLine();
                            d = int.Parse(day);
                            leaps = y / 4 - y / 100 + y / 400;
                            switch (m)
                            {
                                case (1):
                                    tempsum = (y * 365 + leaps) + d;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (2):
                                    tempsum = (y * 365 + leaps) + 31 + d;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (3):
                                    tempsum = (y * 365 + leaps) + 59 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (4):
                                    tempsum = (y * 365 + leaps) + 90 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (5):
                                    tempsum = (y * 365 + leaps) + 120 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (6):
                                    tempsum = (y * 365 + leaps) + 151 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (7):
                                    tempsum = (y * 365 + leaps) + 181 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (8):
                                    tempsum = (y * 365 + leaps) + 212 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (9):
                                    tempsum = (y * 365 + leaps) + 243 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (10):
                                    tempsum = (y * 365 + leaps) + 273 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (11):
                                    tempsum = (y * 365 + leaps) + 304 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                                case (12):
                                    tempsum = (y * 365 + leaps) + 334 + d;
                                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                                        tempsum = tempsum + 1;
                                    totalday1 = totalday2;
                                    totalday2 = tempsum;
                                    break;
                            }
                        }
                        diff = totalday1 - totalday2;
                        if (diff < 0)
                            Console.WriteLine("此兩日相差{0}天", -(diff-1));
                        else
                            Console.WriteLine("此兩日相差{0}天", diff-1);
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    default:
                        return ;
                }
            }
            
        }
    }
}

4 則留言:

  1. 第三題判斷閏年的公式有誤!!!
    第142行開始 的 m==8 應該還要包含m==10 ,m==12
    然後一開始的宣告可以只接把year, month ,day宣告為 int ,如果先宣告string 然後再轉int 到y,m,d有點多一層。

    回覆刪除
    回覆
    1. 感謝 剛剛才發現這個case裡漏掉了100年和400年的判斷式

      刪除
  2. 作者已經移除這則留言。

    回覆刪除
  3. 171行 變數宣告依據camel case
    totalday->totalDay較佳
    difference省略過多

    回覆刪除