本帖最後由 謝承志 於 2020-4-25 17:49 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int year,month,day,days;
- cout<<"***歡迎來到天數計算器***"<<endl;
- cout<<endl;
-
- re:
-
- cout<<"請輸入年: ";
- cin>>year;
- cout<<"請輸入月: ";
- cin>>month;
- cout<<"請輸入日: ";
- cin>>day;
-
- switch(month)
- {
- case 1:
- day<=31;
- days=day;
- break;
- case 2:
- days<=29;
- days=31+day;
- break;
- case 3:
- day<=31;
- days=59+day;
- break;
- case 4:
- day<=30;
- days=90+day;
- break;
- case 5:
- day<=31;
- days=120+day;
- break;
- case 6:
- day<=30;
- days=151+day;
- break;
- case 7:
- day<=31;
- days=181+day;
- break;
- case 8:
- day<=31;
- days=212+day;
- break;
- case 9:
- day<=30;
- days=243+day;
- break;
- case 10:
- day<=31;
- days=273+day;
- break;
- case 11:
- day<=30;
- days=304+day;
- break;
- case 12:
- day<=31;
- days=334+day;
- break;
- default:
- cout<<""<<endl;
- }
- if(year%1600==0 && day<=28 && month<=12 && month>=1)
- cout<<"此日期之天數為: 第"<<days<<"天"<<endl;
- else if(month<=2 && day<=29 && year%4==0 && year%100!=0 && month>=1 || month<=2 && day<=29 && year%400==0 && month>=1 )
- cout<<"此日期之天數為: 第"<<days<<"天"<<endl;
- else if(month>=3 && day<=29 && year%4==0 && year%100!=0 && month<=12 && month>=1 || month>=3 && day<=29 && year%400==0 && month<=12 && month>=1)
- cout<<"此日期之天數為: 第"<<days+1<<"天"<<endl;
- else if(day<=28 && year%100==0 && year%400!=0 && month<=12 && month>=1 || day<=28 && year%4!=0 && month<=12 && month>=1)
- cout<<"此日期之天數為: 第"<<days<<"天"<<endl;
- else
- cout<<"輸入錯誤!"<<endl;
- cout<<"----------------------------------------------------"<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |