C语言,请输入年月日,分别判断已经定义的数组“year_table”里面的年份是否为闰年,如果是闰

C语言,请输入年月日,分别判断已经定义的数组“year_table”里面的年份是否为闰年,如果是闰年,计算今天距离数组中当前的年份的国庆节已过去多少天,并分别输出;如果不是闰年,计算今天距离数组中当前的年份的儿童节已过去多少天,并分别输出。

#include <iostream>

int rn=0;
int t_year,t_month,t_day;
int arr[8]={1949,1952,1966,1972,1983,1995,2003,2012};

int getmonth(int x)
{
if(rn==1)
{
switch(x)
{
case 1:
return 31;
case 2:
return 60;
case 3:
return 91;
case 4:
return 121;
case 5:
return 152;
case 6:
return 182;
case 7:
return 213;
case 8:
return 244;
case 9:
return 274;
case 10:
return 305;
case 11:
return 335;
case 12:
return 366;
}
}
else if(rn==0)
{
switch(x)
{
case 1:
return 31;
case 2:
return 59;
case 3:
return 90;
case 4:
return 120;
case 5:
return 151;
case 6:
return 181;
case 7:
return 212;
case 8:
return 243;
case 9:
return 273;
case 10:
return 304;
case 11:
return 334;
case 12:
return 365;
}
}
}

int isrn(int x)
{
rn=0;
if(x%400==0 || x%4==0)
{
rn=1;
return 1;
}
else
{
rn=0;
return 0;
}
}

int main(int argc, char** argv) 
{
int t_year,t_month,t_day,i,j,re;
printf("\n请输入年:");
scanf("%d",&t_year);
printf("\n请输入月:");
scanf("%d",&t_month);
printf("\n请输入日:");
scanf("%d",&t_day);
for(i=0;i<8;i++)
{
int count=0;
for(j=arr[i]+1;j<=t_year;j++)
{
if(isrn(j)==1)
{
count++;
}
}
re=isrn(arr[i]);
if(re==1)
{
printf("\n%-5d%-13s",arr[i],"年是闰年");
long sum_x,sum_today;
sum_x=arr[i]*365+getmonth(10)+1;
sum_today=t_year*365+getmonth(t_month)+t_day;
printf("%-28s %-5d 天","今天距离那年的国庆节已经过去",sum_today-sum_x+count);
}
else if(re==0)
{
printf("\n%-5d%-13s",arr[i],"年不是闰年");
long sum_x,sum_today;
sum_x=arr[i]*365+getmonth(6)+1;
sum_today=t_year*365+getmonth(t_month)+t_day;
printf("%-28s %-5d 天","今天距离那年的儿童节已经过去",sum_today-sum_x-1+count);
}
}
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-29
#include <stdio.h>
bool isLeapYear(int year)
{
if ((year%4==0&& year%100!=0) || year%400==0)
return true;
else return false;
}
int days_per_month(int year, int month)
{
switch(month)
{
case 1:return 31;
case 2: if (isLeapYear(year))return 29; else return 28;
case 3:return 31;
case 4:return 30;
case 5:return 31;
case 6:return 30;
case 7:return 31;
case 8:return 31;
case 9:return 30;
case 10:return 31;
case 11:return 30;
case 12:return 31;
default: return 0;
}
}
int days_per_year(int year)
{
if (isLeapYear(year)) return 366;
return 365;
}
void subOneDay(int &year,int &month, int &day)
{
day--;
if(day==0)
{
month--;
if(month==0){year--;month=12;}
day=days_per_month(year,month);
}
return;
}

int main()
{
int year,month,day;
int year_table[]={1949,1952,1966,1972,1983,1995,2003,2012};
printf("Input year,month,day:\n");
scanf("%d %d %d",&year,&month,&day);

for(int i=0;i<8;i++)
{
int temp_year=year,temp_month=month,temp_day=day,sum=0;
if (isLeapYear(year_table[i]))
{
while (1)
{
subOneDay(temp_year,temp_month,temp_day);
sum++;
if(temp_year==year_table[i]&&temp_month==10&&temp_day==1) break;
};
printf("%d年是闰年,今天离那年国庆节%d天:\n",year_table[i],sum);
}
else
{
while (1)
{
subOneDay(temp_year,temp_month,temp_day);
sum++;
if(temp_year==year_table[i]&&temp_month==6&&temp_day==1) break;
};
printf("%d年不是闰年,今天离那年儿童节%d天:\n",year_table[i],sum);
}
}
return 0;
}

 

本回答被网友采纳
相似回答