2个回答
展开全部
#include <stdio.h>
int main (void)
{
int i;
int year,total,total1,total2;
int mon1,mon2;
int day1,day2;
int year1[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int year2[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
total1 = total2 = 0;
printf ("请输入您要计算哪一年:");
scanf ("%d",&year);
if (year%4==0 && year%100!=0 || year%400==0)//判断此年是否是闰年,如果是闰年执行下面的语句
{
printf ("%d年是闰年.\n",year);
printf ("请输入第一个月份:");
while (scanf ("%d",&mon1) == 1)
{
if (mon1>12 || mon1<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入第一个月份:");
}
else
break;
}
printf ("请输入此月的第几天:");
while (scanf ("%d",&day1) == 1)
{
if (mon1 == 2)
{
if (day1>29 || day1<=0)//闰年二月有29天
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入此月的第几天:");
}
else
break;
}
else
break;
}
printf ("请输入第二个月份:");
while (scanf ("%d",&mon2) == 1)
{
if (mon2>12 || mon2<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入第二个月份:");
}
else
break;
}
printf ("请输入此月的第几天:");
while (scanf ("%d",&day2) == 1)
{
if (mon2 == 2)
{
if (day2>29 || day2<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入此月的第几天:");
}
else
break;
}
else
break;
}
for (i=0; i<mon1-1; i++)
total1 += year1[i];
total1 += day1;
for (i=0; i<mon2-1; i++)
total2 += year1[i];
total2 +=day2;
if (total1 > total2)
total = total1 - total2;
else
total = total2 - total1;
printf ("这两个日期共相差 %d 天!\n",total);
}
else//不是闰年的情况
{
printf ("%d年不是闰年.\n",year);
printf ("请输入第一个月份:");
while (scanf ("%d",&mon1) == 1)
{
if (mon1>12 || mon1<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入第一个月份:");
}
else
break;
}
printf ("请输入此月的第几天:");
while (scanf ("%d",&day1) == 1)
{
if (mon1 == 2)//平年二月有28天
{
if (day1>28 || day1<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入此月的第几天:");
}
else
break;
}
else
break;
}
printf ("请输入第二个月份:");
while (scanf ("%d",&mon2) == 1)
{
if (mon2>12 || mon2<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入第二个月份:");
}
else
break;
}
printf ("请输入此月的第几天:");
while (scanf ("%d",&day2) == 1)
{
if (mon2 == 2)
{
if (day2>28 || day2<=0)
{
printf ("您的输入有误,请重新输入!\n");
printf ("请输入此月的第几天:");
}
else
break;
}
else
break;
}
for (i=0; i<mon1-1; i++)
total1 += year2[i];
total1 += day1;
for (i=0; i<mon2-1; i++)
total2 += year2[i];
total2 += day2;
if (total1 > total2)
total = total1 - total2;
else
total = total2 - total1;
printf ("这两个日期共相差 %d 天!\n",total);
}
return 0;
}
这个应该不会有雷同! 而且够长
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询