namespace HW1
{
class Program
{
static void part1()
{
int first, second, third,num;
for (int i = 100; i < 1000; i++)
{
first = i / 100;
second = i / 10 % 10;
third = i % 10;
num = first * first * first + second * second * second + third * third * third;
if (i == num)
Console.WriteLine(i);
}
}
static bool part2(int year)//閏年 回傳True
{
if (((year % 400) == 0) || ((year % 4 == 0) && (year % 100 != 0))) return true;
else return false;
}
static int part3(int year, int month)
{
if (month != 2)
{
int big = 1;
if (month <= 7)
{
if (month % 2 == 0) big = 0;
}
else
if (month % 2 == 1) big = 0;
if (big == 1) return 31;
else return 30;
}
else //2月
if (Program.part2(year)) return 29;
else return 28;
}//回傳天數
static void part4(int year, int month, int day)
{
int delta;
delta = part6(1, 1, 1, year, month, day);
// delta++;
if (delta == -1) Console.WriteLine("星期1");
delta = delta % 7;
if (delta == 0)
Console.WriteLine("星期2");
if (delta == 1)
Console.WriteLine("星期3");
if (delta == 2)
Console.WriteLine("星期4");
if (delta == 3)
Console.WriteLine("星期5");
if (delta == 4)
Console.WriteLine("星期6");
if (delta == 5)
Console.WriteLine("星期天");
if (delta == 6)
Console.WriteLine("星期1");
}
static bool part5(int year, int month, int day)//有效日期判斷
{
if ((month <= 12) && (month >= 1) && ((day <= part3(year, month)) && (day > 0))) return true;
else return false;
}
static int part6(int y1, int m1, int d1,int y2, int m2, int d2)//回傳 兩日相差天數
{
int day=0;//offset
for (int i = y2; i > y1; i--)//年換算
{
if (part2(i - 1))
day += 366;
else day += 365;
}
for (int i = m2; i > 1; i--)//月換算(轉成1/1)
day += part3(y2, i - 1);
for (int i = m1; i > 1; i--)//月換算(轉成1/1)
day -= part3(y1, i - 1);
return d2 + day - d1 - 1;
}
static void Main(string[] args)
{
string cmd;
int stage,buf,y1,m1,d1,y2,m2,d2;
while(true)
{
#region input
Console.WriteLine("請輸入要測哪一題:");
cmd=Console.ReadLine();
stage = Convert.ToInt32(cmd);
#endregion
#region test1
if (stage == 1)
{
part1();
}
#endregion
#region test2
else if (stage == 2)
{
Console.WriteLine("輸入西元年份,讓我幫你算算是不是閏年:");
cmd = Console.ReadLine();
buf = Convert.ToInt32(cmd);
if (part2(buf)) Console.WriteLine("是閏年!");
else Console.WriteLine("是平年!");
}
#endregion
#region test3
else if (stage == 3)
{
Console.WriteLine("輸入西元年份及月份,讓我幫你算算該月有幾天:");
Console.WriteLine("請先輸入西元年份:");
cmd = Console.ReadLine();
y1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入月份:");
cmd = Console.ReadLine();
m1 = Convert.ToInt32(cmd);
Console.WriteLine(part3(y1, m1));
}
#endregion
#region test4
else if (stage == 4)
{
Console.WriteLine("輸入西元年份及月份和日期,讓我幫你算算是星期幾:");
Console.WriteLine("請先輸入西元年份:");
cmd = Console.ReadLine();
y1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入月份:");
cmd = Console.ReadLine();
m1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入日期:");
cmd = Console.ReadLine();
d1 = Convert.ToInt32(cmd);
part4(y1, m1, d1);
}
#endregion
#region test5
else if (stage == 5)
{
Console.WriteLine("輸入西元年份及月份和日期,讓我幫你算算是不是合法日期:");
Console.WriteLine("請先輸入西元年份:");
cmd = Console.ReadLine();
y1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入月份:");
cmd = Console.ReadLine();
m1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入日期:");
cmd = Console.ReadLine();
d1 = Convert.ToInt32(cmd);
if (part5(y1, m1, d1)) Console.WriteLine("合法!");
else
Console.WriteLine("不合法!");
}
#endregion
#region test6
else if (stage == 6)
{
Console.WriteLine("輸入兩個西元年份及月份和日期,讓我幫你算算總共差幾天:");
Console.WriteLine("請先輸入(起始)西元年份:");
cmd = Console.ReadLine();
y1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入(起始)月份:");
cmd = Console.ReadLine();
m1 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入(起始)日期:");
cmd = Console.ReadLine();
d1 = Convert.ToInt32(cmd);
Console.WriteLine("請先輸入(結束)西元年份:");
cmd = Console.ReadLine();
y2 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入(結束)月份:");
cmd = Console.ReadLine();
m2 = Convert.ToInt32(cmd);
Console.WriteLine("請輸入(結束)日期:");
cmd = Console.ReadLine();
d2 = Convert.ToInt32(cmd);
Console.WriteLine(part6(y1, m1, d1, y2, m2, d2));
}
#endregion
else Console.WriteLine("不合法的輸入= =");
}
}
}
}
2015年3月18日 星期三
[2015][Homework]Team03 - Hw01
訂閱:
張貼留言 (Atom)
重複使用變數cmd
回覆刪除cmd 是command 的簡寫,是拿來當指令暫存器的變數,所以我才會重複使用!
刪除作者已經移除這則留言。
刪除學到了學到了
刪除原來是我英文不好
if後的條件式要換行
回覆刪除34行return要換行
回覆刪除buf是什麼?>A<
回覆刪除buf 是buffer 是緩衝器的意思喔!
刪除buf 是buffer 是緩衝器的意思喔!
刪除那為什麼要用緩衝器>A<
刪除我用來儲存相同型態,但是具有不停意義的變數,因為如果變數很多種,但有一些變數是可以共用的,這樣可以節省效能喔!
刪除7.一行宣告一變數
回覆刪除87.一行宣告一變數
5~82.函式宣告名稱沒有意義
46~61.一直使用if,拖慢程式
77~81縮排有錯
我是用第幾題來命名函數,我後面有附註解喔!
刪除i know that
刪除but it is really annoy
作者已經移除這則留言。
刪除68
回覆刪除y1,m1,d1,y2,m2,d2
以上名稱可不必縮寫
大部分輸入參數的步驟,可以簡化:
回覆刪除像是91行開始:
Console.WriteLine("請輸入要測哪一題:");
cmd=Console.ReadLine();
stage = Convert.ToInt32(cmd);
這邊可以直接寫成
stage = Convert.ToInt32(Console.ReadLine());
(像第四組的做法: http://ntnumtoop2014.blogspot.tw/2015/03/2015homeworkhw01_18.html , line 138 )
如果之後要判斷使用者輸入的參數的格式是否合法,可以參考使用TryParse.
感謝您寶貴的建議!
刪除