看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 2008 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 輸入日期,找出明天 餵入的資料(Input): 鍵盤輸入月,日,年 預期的正確結果(Expected Output): 跑出明天的日期 錯誤結果(Wrong Output): 無法顯示結果 程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> #include <stdlib.h> #include <cmath> using namespace std; int a,b,c,d,e; double f; void LeapYear(int c) { if(e!=f){ cout<<"It is not a leap year!\n";} } void incrementDate(int& a, int& b, int& c); void incrementDate(int& a, int& b, int& c) { d=b+1; if(a>12 || a<1){ cout<<"Wrong!\n";} else if(b<1){ cout<<"Wrong!\n";} else if(c<1){ cout<<"Wrong!\n";} else{ if(a==1 && b>31){ cout<<"Wrong!\n";} else if(a==2 && b>29){ cout<<"Wrong!\n";} else if(a==2 && b==29){ LeapYear(c);} else if(a==3 && b>31){ cout<<"Wrong!\n";} else if(a==4 && b>30){ cout<<"Wrong!\n";} else if(a==5 && b>31){ cout<<"Wrong!\n";} else if(a==6 && b>30){ cout<<"Wrong!\n";} else if(a==7 && b>31){ cout<<"Wrong!\n";} else if(a==8 && b>31){ cout<<"Wrong!\n";} else if(a==9 && b>30){ cout<<"Wrong!\n";} else if(a==10 && b>31){ cout<<"Wrong!\n";} else if(a==11 && b>30){ cout<<"Wrong!\n";} else if(a==12 && b>31){ cout<<"Wrong!\n";} } if(a==1 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==1 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==2 && d<29){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==2 && d==29){ if(e==f){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else{ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} } else if(a==2 && d==30){ if(e==f){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} } else if(a==3 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==3 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==4 && d<31){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==4 && d==31){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==5 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==5 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==6 && d<31){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==6 && d==31){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==7 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==7 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==8 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==8 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==9 && d<31){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==9 && d==31){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==10 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==10 && d==32){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==11 && d<31){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==11 && d==31){ cout<<"Tomorrow is "<<a+1<<"/1/"<<c<<"\n";} else if(a==12 && d<32){ cout<<"Tomorrow is "<<a<<"/"<<d<<"/"<<c<<"\n";} else if(a==12 && d==32){ cout<<"Tomorrow is "<<1<<"/1/"<<c+1<<"\n";} } int main() { cout<<"Please input the month in integers:\n"; cin>>a; cout<<"Please input the date:\n"; cin>>b; cout<<"Please input the year:\n"; cin>>c; e=c/4; f=(double) c/4; incrementDate(a,b,c); return 0; } 補充說明(Supplement): -- ███ ▂▄▃ ││││ ˋ Mooncat~││││ 「為什麼 ◤ ◥▏*_▂ │││ 為什麼教授這麼靠盃 ▌█ 沒天理啊…」 ▅▇*Mooncat~ ˋ█▆◤ _ ▁▄▆▇ by mooncats -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.185
james732:看到全域變數那行,我就昏了... 10/13 14:23
obelisk0114:中間只是確定輸入的日期是正確的,以及找出明天 10/13 14:24
LPH66:重點是 C 語言是你說一動他做一動 沒有先寫算式再做的 10/13 14:25
flydragon198:int a,b,c,d,e=c/4; 這行c都還沒定值就指定給e,危險 10/13 14:26
LPH66:然後函式呼叫只要寫名字和變數名即可 不用整個宣告貼上來 10/13 14:26
james732:先把 main 裡 reuturn 0 前面那行改成 10/13 14:28
james732:incrementDate(a, b, c); 10/13 14:28
shadow0326:原來在function裡定義function 這樣編的過 我上了一課 10/13 14:34
obelisk0114:哪一行有誤? 10/13 14:36
james732:http://pastie.org/2687295 至少要改31行跟161行 10/13 14:38
x000032001:你寫世界奇觀幹嘛orz 10/13 14:38
vup4jp6:看了e跟f的宣告...END 真的應該先翻課本.... 10/13 14:40
james732:他現在程式裡還沒用到e跟f,所以可以先無視 10/13 14:40
bigpigbigpig:void incrementDate(int& a, int& b, int& c); 10/13 14:42
vup4jp6:我是認為會這樣寫code 就應該先看課本了XDDDD 10/13 14:43
bigpigbigpig:上面那行是什麼?用函式 prototype 來呼叫?奇觀 :< 10/13 14:43
※ 編輯: obelisk0114 來自: 140.112.4.185 (10/13 14:45)
flydragon198:建議變數名用有意義的,例如day...用a,b,c不太好 10/13 14:48
最後完成版,編譯通過,感謝各位幫忙 ※ 編輯: obelisk0114 來自: 140.112.4.185 (10/13 15:09)
obelisk0114:以後有空會再修改 10/13 15:09
tropical72:你有學過 array 嗎? 10/13 15:17
angleevil:樓上生氣了 10/13 15:19
tropical72:.. 誤會了, 只是覺得用 array 方式去想應會簡便些. 10/13 15:22
ljhgc:array版本,雖然直接寫給你對你不好,但你還是有空看一下吧 10/13 15:23
ljhgc:註解我都寫得很清楚了,每行在作什麼事 10/13 15:23
james732:我猜他也不會再回來看這篇文章了,大家就輕鬆點吧 10/13 15:23
ljhgc:http://pastie.org/2687459 10/13 15:23
ljhgc:老實說,從IP來看原PO應該多少會"念書"才對,這種問題翻個書 10/13 15:28
ljhgc:就能解了。寫出那樣的style,恩.... 10/13 15:29
tropical72:我想起以前練習class cdate時,一堆簡單的都化很久.. 10/13 15:33
angleevil:james732講話總是直搗核心 10/13 15:42
james732:我可以當作樓上在稱讚我嗎 >///< 10/13 15:43
angleevil:那句是稱讚 10/13 15:45
priv:ljhgc會不會太認真還寫了範例... 10/13 15:58
johnlinvc:我寫不出這麼多的 else if 看來我該讓賢了 10/13 16:01
ljhgc:寫範例是還好,如果他不認真看的話,給他也是沒用 10/13 16:22
ljhgc:個人也是從網路上的source code來學各種作法的 10/13 16:23
ljhgc:有範例帶著一步步作會比看書快的多~ 10/13 16:24
lwecloud:想到達明天~現在就要啟程~ 10/13 16:32
angleevil:說實話,原po也是有來這的經驗.卻不注重之後的推文 10/13 16:49
ilway25:是不是要考慮歷史上有幾天不存在XD 10/13 17:31
Fenikso:printf("找不到明天OwQ"); 10/13 17:31
johnhmj:如果沒有明天( ̄ー ̄;) 10/13 17:36
angleevil:程式人員的明天到底在哪Orz 10/13 17:59
x000032001:a,b,c,d,e,f 誰知道在幹嘛 我是教授我一定當你 10/13 18:17
james732:通常看程式碼的不是教授啦,助教嘛都可以跑就過了 10/13 18:24
qurincess:根據NTU板#1DSbaoFJ的推文 原po可是"念c++ primer念完幾 10/13 18:38
qurincess:個章節程式就寫很快"的呢 10/13 18:38
priv:行數產生得很快是沒錯啊 10/13 19:40
firejox:LeapYear的規則是4年算100年不算400年算... 10/13 19:46
priv:其實我一開始就想問樓上的問題,不過我想算了... 10/13 19:52
priv:也許原po一開始就覺得不重要.. 10/13 19:52
ljhgc:閏年的問題我有看到,只是我懶的改~哈哈,那不是重點 10/13 20:00
ljhgc:是4的倍數,而不是100的倍數。如果剛好是400的倍數時,亦算 10/13 20:02
priv:反正如果助教的test data有考慮這個的話就會被扣分 10/13 20:02
ljhgc:我可以祝元PO被助教打槍嗎...這樣一定會對原PO有好無壞 10/13 20:05
x000032001:前面那幾章...可能while跟if學得很精通 沒學array 10/13 20:15
priv:呃,從書架上拿下來翻,array是第二章耶 10/13 20:17
priv:不過如果講C++我也沒資格笑人...有夠爛的 10/13 20:17
x000032001:wwwww 太久沒看了 10/13 20:27
priv:只是原po好像也不是在寫C++啦XD 10/13 20:30
james732:他是寫C++啊,還用了reference呢 10/13 20:34
kdjf:我看到第二頁,就眼花了..... 10/13 21:39
kdjf:他幫compiler做完space-speed tradeoff optimizations了 XD 10/13 21:40
firejox:其實用google 就可以找到許多有趣的方法.... 10/13 21:54
firejox:就像這樣 http://pastie.org/2688987 10/13 21:55
tropical72:有趣的我以為是這樣 http://pastie.org/2689009 10/13 22:01
tropical72:不過 f 大那堆 magic number 有沒有說明網頁啊 ? 10/13 22:03
firejox:wiki有 http://en.wikipedia.org/wiki/Ordinal_date 10/13 22:04
tropical72:感謝 !! 10/13 22:05
firejox:如果光是floor (30.6 m - 91.4 ) + d 二月會是30天 10/13 22:07
firejox:所以還得做調整 10/13 22:08
LPH66:我自己是找出過 30*(m-1)+(19*(m-2))<<5+d 來算... 10/14 08:08
LPH66:不過僅適用於三月到12月 然後平年要減一這樣 10/14 08:08
LPH66:不過好處是不用浮點數就是了 XD 10/14 08:08
LPH66:(其實它還適用到14月 不過我自己反而少用這樣來算就是) 10/14 08:12
angleevil:其實如果雞婆點,創立一個不負責名單.以後這種要交作業 10/14 08:42
angleevil:前幾天才匆忙來問,問完也不管後續問題的人.就不要回答 10/14 08:43
angleevil:不過說實話,早那麼大還要有人教. 我猜大家也沒那個時間 10/14 08:43
lwecloud:可見板上有多少好人,世界奇觀也認真的教學XD 10/14 09:03
angleevil:套句超哥的話,我們也是從世界奇觀走過來的.只是他們的態 10/14 09:47
angleevil:度,有時候會害死他們自己. 不過其實大家後來也沒管原po 10/14 09:48
flylover:世界奇觀XD.. 好特別的說法 10/14 10:16
priv:其實topic就很神奇了 10/14 10:30
priv:我想說用程式要怎樣找出你的明天.. 10/14 10:30
angleevil:他在用這種態度面對學習.很快就要唱如果還有明天 10/14 10:44
james732:我有說過這種話嗎XD 10/14 12:40
angleevil:世界奇觀 = 初學者. 10/14 12:44
james732:原來如此XD 10/14 13:14
purpose:世界奇觀是說他 if else 用太多層太誇張 10/14 15:02
angleevil:我知道,但是也只有初學者才有辦法搞出來 10/14 15:03
ljhgc:在想...對這種趕作業的,以後都用Macro寫給他。 看他要怎樣 10/14 15:47
ljhgc:跟教授/助教解釋作法~ 10/14 15:47
james732:有的人會想學好,有的人只想答案,我會因材施教的XD 10/14 16:06
obelisk0114:會回來看,只是不一定會回文 10/16 01:41
obelisk0114:e和f是題目說要寫一個LeapYear才生出來的 10/16 01:44
obelisk0114:LeapYear我覺得他是要我們當函式引進來用 10/16 01:45
priv:正常人的想法通常是把年份傳進去判斷是不是leap year 10/16 20:07
priv:你的想法是比較稍微跳脫常規一點 10/16 20:07
james732:既然你會回來看,那我給你的第一個建議是別濫用全域變數 10/17 18:55
james732:第二個建議是盡量減少重覆的程式碼 10/17 18:55