當前位置:編程學習大全網 - 編程語言 - 這是我寫的c語言計算兩個日期之間的天數的程序,求指教怎麽錯的?

這是我寫的c語言計算兩個日期之間的天數的程序,求指教怎麽錯的?

抱歉,妳的代碼風格實在是讓我看不下去。這樣的代碼估計過幾天妳自己都看不懂了。函數命名和變量命名壹定要有意義,雖然不壹定簡潔,但可讀性壹定要好,這樣就算錯了調試也方便。我自己寫了壹個類似的程序,妳看看是不是妳想要的。上代碼(這網頁上的排版不會搞,妳復制到自己的編譯環境重新排版下吧):

#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?==?uMonth1?:?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;

}

  • 上一篇:編程適合什麽樣的人學?
  • 下一篇:關於後端程序員寫前端用什麽框架更好
  • copyright 2024編程學習大全網