C++ 输入一个日期,计算这一天是本年的第几天。 求大神帮助
展开全部
#include <iostream>
using namespace std;
struct datetype{
int year;
int month;
int day;
}date;
int main()
{
int i,day_sum;
static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"请输入年、月、日:"<<endl;
cin>>date.year>>date.month>>date.day;
day_sum=0;
for(i=1;i<date.month;i++)day_sum+=day_tab[i];
day_sum+=date.day;
if((date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 ==0) && date.month>2 )
day_sum+=1;
cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<day_sum<<"天"<<endl;
return 0;
}
using namespace std;
struct datetype{
int year;
int month;
int day;
}date;
int main()
{
int i,day_sum;
static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"请输入年、月、日:"<<endl;
cin>>date.year>>date.month>>date.day;
day_sum=0;
for(i=1;i<date.month;i++)day_sum+=day_tab[i];
day_sum+=date.day;
if((date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 ==0) && date.month>2 )
day_sum+=1;
cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<day_sum<<"天"<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
const int array[][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
//判断是否闰年
int leap(int year)//此函数判断是否闰年
{
if( year%400 ==0 || year%100 !=0 && year%4 ==0)
return 1;
else
return 0;
}
int count(int year,int month,int day)//此函数计算天数
{
int result=day;
int i=leap(year);
for(int j=1;j<month;j++)
{
result+=array[i][j];
}
return result;
}
int main()
{
int year,month,day;
cout<<"按次序输入年月日";
cin>>year>>month>>day;
//此处应该增加判断非法输入,比如月份>12,日期大于 31
if((month>12&&month<0)&&(day<0&&day>array[leap(year)][month+1]))
{
cout<<"输入有误!!"<<endl;
return 1;
}else{
int result=count(year,month,day);
cout<<year<<"年"<<month<<"月"<<day<<"日 ";
cout<<"是该年度第 "<<result <<" 天"<<endl;
return 1;
}
}
using namespace std;
const int array[][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
//判断是否闰年
int leap(int year)//此函数判断是否闰年
{
if( year%400 ==0 || year%100 !=0 && year%4 ==0)
return 1;
else
return 0;
}
int count(int year,int month,int day)//此函数计算天数
{
int result=day;
int i=leap(year);
for(int j=1;j<month;j++)
{
result+=array[i][j];
}
return result;
}
int main()
{
int year,month,day;
cout<<"按次序输入年月日";
cin>>year>>month>>day;
//此处应该增加判断非法输入,比如月份>12,日期大于 31
if((month>12&&month<0)&&(day<0&&day>array[leap(year)][month+1]))
{
cout<<"输入有误!!"<<endl;
return 1;
}else{
int result=count(year,month,day);
cout<<year<<"年"<<month<<"月"<<day<<"日 ";
cout<<"是该年度第 "<<result <<" 天"<<endl;
return 1;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
scanf("%d-%d-%d", &year, &month, &day);
tian[1] = tian[3] ..... = tian[12] = 31;
tian[4] = tian[6] ... = tian[11] = 30;
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) tian[2] = 29;
else tian[2] = 28;
tot = 0;
for(int i = 1; i < month; i++) tot+=tian[i];
tot += day;
printf("%d\n", tot);
tian[1] = tian[3] ..... = tian[12] = 31;
tian[4] = tian[6] ... = tian[11] = 30;
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) tian[2] = 29;
else tian[2] = 28;
tot = 0;
for(int i = 1; i < month; i++) tot+=tian[i];
tot += day;
printf("%d\n", tot);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询