標題:
這是第幾天?
[打印本頁]
作者:
王瑞喻
時間:
2020-4-16 19:39
標題:
這是第幾天?
[attach]11997[/attach]
作者:
謝承志
時間:
2020-4-16 21:08
本帖最後由 謝承志 於 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;
}
複製代碼
作者:
may
時間:
2021-9-6 08:34
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int y,m,d,total=0,i;
int db[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
cout<<"請輸入年月日並以空格分隔:";
cin>>y>>m>>d;
if((y%4==0&&y%100!=0)||(y%400==0))
{
cout<<y<<"年是閏年"<<endl;
for(i=0;i<m-1;i++)
{
total+=db[1][i];
}
}else
{
cout<<y<<"年是平年"<<endl;
for(i=0;i<m-1;i++)
{
total+=db[0][i];
}
}
total+=d;
cout<<"您輸入的日期"<<y<<"年"<<m<<"月"<<d<<"日,"<<"是這一年中的第"<<total<<"天\n";
system("pause");
return 0;
}
複製代碼
作者:
洪勻蓁
時間:
2021-9-7 16:24
本帖最後由 洪勻蓁 於 2021-9-7 16:37 編輯
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int sum=0;
int db[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
int year, month, date;
cout<<"請輸入年月日並以空格隔開: ";
cin>>year>>month>>date;
if(year%4==0 && year%100!=0 || year%400==0)
{
cout<<year<<"年為閏年"<<endl;
for(int i=0;i<month-1;i++)
{
sum+=db[0][i];
}
sum+=date;
}
else
{
cout<<year<<"年為平年"<<endl;
for(int i=0;i<month-1;i++)
{
sum+=db[1][i];
}
sum+=date;
}
cout<<"您輸入的日期為"<<year<<"年"<<month<<"月"<<date<<"日"<<",是這一年中第"<<sum<<"天"<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2