抱歉,你的代码风格实在是让我看不下去。这样的代码估计过几天你自己都看不懂了。函数命名和变量命名一定要有意义,虽然不一定简洁,但可读性一定要好,这样就算错了调试也方便。我自己写了一个类似的程序,你看看是不是你想要的。上代码(这网页上的排版不会搞,你复制到自己的编译环境重新排版下吧):
#include "stdafx.h" //这里面啥也没有,就作为一个预编译头
#include
#include
#include
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;
}
#include
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);
}
满意就给个采纳吧