'C语言 定义一个包含年月日的结构体变量,任意输入一天,计算该日是本年的第几天
展开全部
#include <stdio.h>
#include<string.h>
struct date
{
int year, mouth, day;
};
int main()
{
struct date d;
int i, k=0, nday=0;
printf("请输入当前年月日: ");
scanf("%d%d%d", &d.year, &d.mouth, &d.day);
///先判断是否是闰年,符合条件就是闰年,k置为1
if(d.year%4==0 && d.year%100 != 0 || d.year%400 == 0)
k = 1;
for(i=1; i<d.mouth; i++)
{
//如果是2月并且是闰年,加上29天
if(i == 2 && k == 1)
nday += 29;
else if(i == 2)
nday += 28;
else if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
nday += 31;
else
nday += 30;
}
nday += d.day;
printf("今天是今年的第 %d 天\n", nday);
return 0;
}
展开全部
#include <stdio.h>
void main()
{
int year,month,day,days=0,i;
printf("请输入年月日:");
scanf("%d%d%d",&year,&month,&day);
if(year<1 || month>12 || month<1 || day>31)
printf("日期输入错误!!!\n");
else
{
if(month>1)
for(i=1;i<month;i++)
if(i==2)
days+=28;
else if(i==4|| i==6||i==9||i==11)
days+=30;
else
days+=31;
days+=day;
}
if((month>2) && ((year%400==0)||(year%4==0 && year%100!=0)))
days+=1;
printf("%d年%d月%d日是%d年%d天!\n",year,month,day,year,days);
}
void main()
{
int year,month,day,days=0,i;
printf("请输入年月日:");
scanf("%d%d%d",&year,&month,&day);
if(year<1 || month>12 || month<1 || day>31)
printf("日期输入错误!!!\n");
else
{
if(month>1)
for(i=1;i<month;i++)
if(i==2)
days+=28;
else if(i==4|| i==6||i==9||i==11)
days+=30;
else
days+=31;
days+=day;
}
if((month>2) && ((year%400==0)||(year%4==0 && year%100!=0)))
days+=1;
printf("%d年%d月%d日是%d年%d天!\n",year,month,day,year,days);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include<string.h>
struct date
{
int year,mouth,day;
};
int main()
{
struct date d;
int i,k=0,nday=0;
scanf("%d%d%d", &d.year,&d.mouth,&d.day);
if(d.year%4==0&& d.year%100!=0|| d.year%400==0)
k=1;
for(i=1;i<d.mouth;i++)
{
if(i==2&&k==1)
nday+=29;
else if(i==2)
nday+=28;
else if(i==1||i==3 ||i==5||i==7||i==8 ||i==10||i==12)
nday+=31;
else
nday+=30;
}
nday+=d.day;
printf("%d",nday);
return 0;
}
#include<string.h>
struct date
{
int year,mouth,day;
};
int main()
{
struct date d;
int i,k=0,nday=0;
scanf("%d%d%d", &d.year,&d.mouth,&d.day);
if(d.year%4==0&& d.year%100!=0|| d.year%400==0)
k=1;
for(i=1;i<d.mouth;i++)
{
if(i==2&&k==1)
nday+=29;
else if(i==2)
nday+=28;
else if(i==1||i==3 ||i==5||i==7||i==8 ||i==10||i==12)
nday+=31;
else
nday+=30;
}
nday+=d.day;
printf("%d",nday);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询