请教日历的算法(30分)

牛龙

Unregistered / Unconfirmed
GUEST, unregistred user!
我想根据年份和月份来算对应月份的天数?
请那位大侠能帮小弟提供一下算法?
听说被4整除的年份不一定是闰年?对吗?
 
比较容易的方法是: 取下一个月的1号作为计算日期,
如计算1999年1月, 取B:=1999年2月1日,
将B:=B-1,
取B的day值,
1900年就不是闰年, 但2000年是闰年,
有一种规定, 00结尾的一般不是闰年, 除非
头两位能被4整除,(如2000, 1600)
 
我原来的一个程序.
function getdays(const iyear, imonth:word ) :word;
var
b: Tdatetime;
y,m,d : word;
begin
d:=1;
if imonth=12 then begin
y:= iyear+1
m:= 1
end else begin
y:=iyear
m:=imonth
end;
b:=EncodeDate(y,m,d)-1;
DecodeDate(B,y,m,d);
result:=D

end
 
错了一句:
m:=imonth

应为
m:= imonth+1;
 
闰年
function dateLeapYear(D: TDateTime): Boolean;
var
Year,Month,Day: Word;
begin
DecodeDate(D,Year,Month,Day);
Result:=(Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;


function dateDaysInMonth(D: TDateTime): Integer;
const
DaysPerMonth: array[1..12] of Byte= (31,28,31,30,31,30,31,31,30,31,30,31);
var
Month: Integer;
begin
Month:=dateMonth(D);
Result:=DaysPerMonth[Month];
if (Month=2) and dateLeapYear(D) then Inc(Result);
end;
 
能被4整除,不能被100整除,但又能被400整除为闰年。
 
多人接受答案了。
 
思路如下
输入年份year
if year能被100整除
if year能被400整除
是(闰年)
else
不是
endif
else
if year能被4整除

else
不是
end if
end if

没办法,只能写思路,我才从vb转学delphi不久,大概写delphi的话会出错的。
 
顶部