c语言 结构体:计算输入日期是该年的第几天 要多组测试
#include<stdio.h>structdate{intyear;intmonth;intday;}date;intmain(void){intsum=0,leap...
#include<stdio.h>
struct date
{
int year;
int month;
int day;
}date;
int main(void)
{
int sum=0,leap;
while(scanf("%d-%d-%d",&date.year,&date.month,&date.day)!=EOF)
{
switch(date.month)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+date.day;
if((date.year%400==0)||(date.year%4==0&&date.year%100!=0))
leap=1;
else
leap=0;
if(leap=1&&date.month>2)
sum=sum+1;
printf("%d\n",sum);
}
return 0;
}
哪儿错了呀?貌似闰年那儿加不上一天呀 展开
struct date
{
int year;
int month;
int day;
}date;
int main(void)
{
int sum=0,leap;
while(scanf("%d-%d-%d",&date.year,&date.month,&date.day)!=EOF)
{
switch(date.month)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+date.day;
if((date.year%400==0)||(date.year%4==0&&date.year%100!=0))
leap=1;
else
leap=0;
if(leap=1&&date.month>2)
sum=sum+1;
printf("%d\n",sum);
}
return 0;
}
哪儿错了呀?貌似闰年那儿加不上一天呀 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询