0
点赞
收藏
分享

微信扫一扫

输入一个日期,求是这一年中的第几天


#include<bits/stdc++.h>
using namespace std;
bool is_leap_year(int year)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
{
return true;
}
else
return false;
}
int main()
{
int year,month,date;
int day;
bool flag = false;
int sum=0;
do
{
flag=false;
cout<<"input: year month date"<<endl;
cin>>year>>month>>date;
if(year<0)
{
cout<<"输入年份错误,请重新输入";
flag=true;
}
if(month<1||month>12)
{
cout<<"输入月份错误,请重新输入";
flag=true;
}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(date>31||date<1)
{
cout<<"日期输入有误,请重新输入"<<endl;
flag=true;
}
}
if(month==4||month==6||month==9||month==11)
{
if(date>30||date<1)
{
cout<<"日期输入有误,请重新输入"<<endl;
flag=true;
}
}
if(is_leap_year(year)&&month==2)
{
if(date>29||date<1)
{
cout<<"本月是闰月,请从1-29重新输入日期"<<endl;
flag=true;
}
}
if(!is_leap_year(year)&&month==2)
{
if(date>28||date<1)
{
cout<<"本月是二月,请从1-28重新输入日期"<<endl;
flag=true;
}
}
}
while(flag);
for(int i=1; i<month; i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=31;
sum+=day;
break;
case 4:
case 6:
case 9:
case 11:
day=30;
sum+=day;
case 2:
if(is_leap_year(year))
{
day=29;
sum=sum+day;
break;
}
else
{
day=28;
sum=sum+day;
break;
}
}
}
sum+=date;
cout<<year<<"年"<<month<<"月"<<date<<"日是这一年的第"<<sum<< "天."<<endl;
}


举报

相关推荐

0 条评论