这是我写的c语言计算两个日期之间的天数的程序,求指教怎么错的?

#include <stdio.h>
#include <stdlib.h>
int leap_year( int );
int year_days( int );
int days(int,int,int);
int leap_year( int year )
{ return ( (year%4==0 && year%100!=0) || year%400==0 ) ? 1 : 0;
}

int year_days(int year)
{ return leap_year( year ) ? 366 : 365;
}

int days( int year, int month, int day )
{ int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}, i;

if ( leap_year( year ) && month >2 )
day++;

for ( i=1; i<month; i++ )
day += months[i];

return day;
}
int main()
{
int year,month,day,a,b,c,m,n,x,y,z,p,q;
scanf("%d%d%d%d%d%d",&year,&month,&day,&a,&b,&c);
m=days(year,month,day);
n=days(a,b,c);
x=n-m;
y=a-year;
z=leap_year(year);
p=leap_year(a);
q=leap_year(year-1);
switch(y%4){
case 0:
printf("%d days\n",x+1461*y/4);
break;
case 1:
if(z==1||p==1) printf("%d days\n",x+1461*(y-1)/4+366);
else printf("%d days\n",x+1461*(y-1)/4+365);
break;
case 2:
if(z==1||p==1) printf("%d days\n",x+1461*(y-2)/4+731);
else printf("%d days\n",x+1461*(y-2)/4+730);
break;
case 3:
if(q==1) printf("%d days\n",x+1461*(y-3)/4+1095);
else printf("%d days\n",x+1461*(y-3)/4+1096);
break;
}
system ("pause");
return 0;
}

  抱歉,你的代码风格实在是让我看不下去。这样的代码估计过几天你自己都看不懂了。函数命名和变量命名一定要有意义,虽然不一定简洁,但可读性一定要好,这样就算错了调试也方便。我自己写了一个类似的程序,你看看是不是你想要的。上代码(这网页上的排版不会搞,你复制到自己的编译环境重新排版下吧):

#include "stdafx.h" //这里面啥也没有,就作为一个预编译头
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

bool IsLeapYear(UINT uYear)
{
    return (0 == uYear % 4 && 0 != uYear % 100) || (0 == uYear % 400);
}

UINT GetTotalDayOfMonth(UINT uYear, UINT uMonth)
{
     if (uMonth > 12 || uMonth < 1)
     {
          return 0;
     }
     UINT uDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     return uDays[uMonth - 1] + (IsLeapYear    (uYear) && 2 == uMonth ? 1 : 0);
}
bool IsCorrectInput(UINT uYear, UINT uMonth, UINT uDay)
{
 UINT uTotalDayOfMonth = GetTotalDayOfMonth(uYear, uMonth);
 return (0 != uTotalDayOfMonth && uDay > 0 && uDay <= uTotalDayOfMonth);
}
UINT GetDaysOfYear(UINT uYear, UINT uMonth, UINT uDay)
{
 if (0 == uMonth && 0 == uDay)
 {
  return IsLeapYear(uYear) ? 366 : 365;
 }
 UINT uResult = 0;
 if (uMonth >= 2 && IsLeapYear(uYear))
 {
  ++uResult;
 }
 while (uMonth <= 12)
 {
  uResult += GetTotalDayOfMonth(uYear, uMonth);
  ++uMonth;
 }
 uResult -= uDay;
 return uResult;
}
int main()
{
 UINT uBeginYear = 0;
 UINT uBeginMonth = 0;
 UINT uBeginDay = 0;
 do 
 {
  printf("请输入起始年 月 日:");
  scanf("%u%u%u", &uBeginYear, &uBeginMonth, &uBeginDay);
 } while (!IsCorrectInput(uBeginYear, uBeginMonth, uBeginDay));
 UINT uEndYear = 0;
 UINT uEndMonth = 0;
 UINT uEndDay = 0;
 do 
 {
  printf("请输入终止年 月 日:");
  scanf("%u%u%u", &uEndYear, &uEndMonth, &uEndDay);
 } while (!IsCorrectInput(uEndYear, uEndMonth, uEndDay));
 
 LONG lResult = 0;
 UINT uSmallYear = min(uBeginYear, uEndYear);
 UINT uBigYear = max(uBeginYear, uEndYear);
 while (uSmallYear != uBigYear)
 {
  lResult += (GetDaysOfYear(uSmallYear, 0, 0));
  ++uSmallYear;
 }
 if (uBeginYear > uEndYear)
 {
  lResult *= -1;
 }
 lResult += (GetDaysOfYear(uBigYear, uBeginMonth, uBeginDay) - GetDaysOfYear(uBigYear, uEndMonth, uEndDay));
 printf("相差天数为:%d", lResult);
 system("pause");
 
 return 0;
}

   

追问

求大神指教我的怎么错的。。是不是有什么特别的日期?

追答

额,说实话没看完你写的东西(...有点点难看)。你研究研究我们两个的代码,应该自己就能找到错误了。也可以调试一下。这么简单的程序稍微调试一下就知道错在哪里了。

追问

其实这是我的作业题,调试过了六个,就差一个保密的调试没过,我找了半天都没找到错在哪里,怎么办

其实这是我的作业题,调试过了六个,就差一个保密的调试没过,我找了半天都没找到错在哪里,怎么办

追答

最明显的错误就是 switch(y%4)这里,结果可能是负数啊,你case没有这个值,就不计算了,跳过去了。其他的你自己看看吧,我也该睡觉啦。晚安^_^

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-17
#include <stdio.h>

void main()
{
    printf("请输入起始日期的年,月,日\n");
    int byear, bmonth, bday;
    scanf("%d%d%d", &byear, &bmonth, &bday);
    printf("请输入终止日期的年,月,日\n");
    int eyear, emonth, eday;
    scanf("%d%d%d", &eyear, &emonth, &eday);

    int days = 0;
    if (byear == eyear)
    {
        days = days(eyear, emonth, eday) - days(byear, bmonth,bday);
    }
    else 
    {
        int i;
        days = year_days(byear) - days(byear, bmonth, bday);
        for (i = byear + 1; i < eyear; ++i)
        {
            days += year_days(i);
        }
        days += days(eyear, emonth, eday);
    }
    printf("终止日期与起始日期之间的天数为%d\n", days);
}

满意就给个采纳吧

追问

求大神指教我的怎么错的。。是不是有什么特别的日期?

追答

楼主,既然你已经采纳了,我也没什么好说了,我真是服了你了,采纳程序之前你就不能先运行一下,不要以为程序越长就越好。

相似回答