有关C语言编程问题

2025-01-21 09:23:11
推荐回答(1个)
回答1:

#include
#include
//我写了两个函数func1,func2,都能实现,func1所见即所得,func2,初学的话要认真理解下哟。
void func1(int day,int year)
{
//闰年
if(year<2000||year>2999)
{
printf("年份输入有误,应介于2000-2999\n");
return;
}
if(day<1||day>366)
{
printf("天数输入有误,应介于1-366\n");
return;
}
if(!(year%400)||(!(year%4)&&(year%400)))
{
printf("闰年\n");
if(day<=31)
{
printf("%d年%d月%d日\n",year,1,day);
}
else if(day<=60)
{
printf("%d年%d月%d日\n",year,2,day-31);
}
else if(day<=91)
{
printf("%d年%d月%d日\n",year,3,day-60);
}
else if(day<=121)
{
printf("%d年%d月%d日\n",year,4,day-91);
}
else if(day<=152)
{
printf("%d年%d月%d日\n",year,5,day-121);
}
else if(day<=182)
{
printf("%d年%d月%d日\n",year,6,day-152);
}
else if(day<=213)
{
printf("%d年%d月%d日\n",year,7,day-182);
}
else if(day<=244)
{
printf("%d年%d月%d日\n",year,8,day-213);
}
else if(day<=274)
{
printf("%d年%d月%d日\n",year,9,day-244);
}
else if(day<=305)
{
printf("%d年%d月%d日\n",year,10,day-274);
}
else if(day<=335)
{
printf("%d年%d月%d日\n",year,11,day-305);
}
else
{
printf("%d年%d月%d日\n",year,12,day-335);
}
}
else
{
printf("平年\n");
if(day==366)
{
printf("这是平年,平年没有366天\n");
return;
}
if(day<=31)
{
printf("%d年%d月%d日\n",year,1,day);
}
else if(day<=59)
{
printf("%d年%d月%d日\n",year,2,day-31);
}
else if(day<=90)
{
printf("%d年%d月%d日\n",year,3,day-59);
}
else if(day<=120)
{
printf("%d年%d月%d日\n",year,4,day-90);
}
else if(day<=151)
{
printf("%d年%d月%d日\n",year,5,day-120);
}
else if(day<=181)
{
printf("%d年%d月%d日\n",year,6,day-151);
}
else if(day<=212)
{
printf("%d年%d月%d日\n",year,7,day-181);
}
else if(day<=243)
{
printf("%d年%d月%d日\n",year,8,day-212);
}
else if(day<=273)
{
printf("%d年%d月%d日\n",year,9,day-243);
}
else if(day<=304)
{
printf("%d年%d月%d日\n",year,10,day-273);
}
else if(day<=334)
{
printf("%d年%d月%d日\n",year,11,day-304);
}
else
{
printf("%d年%d月%d日\n",year,12,day-334);
}
}
}

void func2(int day,int year)
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int sum1=0,i,sum2=0;
int mm;//月
int dd;//日
if(year<2000||year>2999)
{
printf("年份输入有误,应介于2000-2999\n");
return;
}
if(day<1||day>366)
{
printf("天数输入有误,应介于1-366\n");
return;
}
if(!(year%400)||(!(year%4)&&(year%400)))//闰年
{
printf("闰年\n");
month[1]=29;
}
else if(day==366)
{
printf("这是平年,平年没有366天\n");
return;
}
else
printf("平年\n");

for(i=0;i<12;i++)
{
sum2=sum1+month[i];
if(day<=sum2)
{
mm = i+1;
dd=day-sum1;
break;
}
sum1=sum2;
}
printf("%d年%d月%d日\n",year,mm,dd);
}
void main()
{
int day,year;
printf("请输入天数,年:\n");
scanf("%d%d",&day,&year);
printf("func1输出结果:\n");
func1(day,year);
printf("\n");
printf("func2输出结果:\n");
func2(day,year);
}