如何用 Pascal 寫個年曆表

2007-12-12 3:10 am
請用 Pascal 家
Write a program to print out the calendar of a year with the input of which year and the day of week of the first day in that year.
( e.g. input year is 2004 and first day ( 1 Jan ) is Thursday )
可 send e-mail to [email protected] 標題請寫年曆。

回答 (1)

2007-12-12 3:25 am
✔ 最佳答案
Program Assignment (input, output);

{Leap year: In the Gregorian calendar, any year divisible by 4 except centenary years not divisible by 400}
{every 4th year is a leap year, e.g. 1996, 2004}
{every centenary year is not a leap year, e.g. 1700, 1800, 1900}
{every 400th year is a leap year, e.g. 1600, 2000, 2400}


type date = RECORD
day, month, year : integer;
END;
day_of_month = array [1..12] of integer;

var fdate : date;
dmonth : day_of_month;
x : boolean;
ftdate, fty, ftm : longint;
nod : integer;
i, j : integer;
dweek, pday : integer;

Function leap (year : integer) : boolean; {return True if the year is leap year}
begin
if year mod 4 = 0 then leap := True;
if year mod 100 = 0 then leap := False;
if year mod 400 = 0 then leap := True
end;


begin
dmonth[1] := 31;
dmonth[2] := 28;
dmonth[3] := 31;
dmonth[4] := 30;
dmonth[5] := 31;
dmonth[6] := 30;
dmonth[7] := 31;
dmonth[8] := 31;
dmonth[9] := 30;
dmonth[10] := 31;
dmonth[11] := 30;
dmonth[12] := 31;

{Input month and year}

writeln('Please input a date between year 0000 to 9999:-');

Repeat
x := True;
write('Date (mm yyyy) : ');
readln(fdate.month, fdate.year);
if (fdate.year < 0) or (fdate.year > 9999) then x := False;
if (fdate.month < 1) or (fdate.month > 12) then x := False;
if x = False then
Writeln('Invalid input date, please try again!')
until x = True;

{Calculate the number of day between 1/1/2006 and the input date}
{1/1/2006 is Sunday}

fty := 0;
ftm := 0;
if fdate.year <> 2006 then
begin
if fdate.year > 2006 then
begin
for i := 2006 to fdate.year - 1 do
begin
if leap(i) = True then nod := 366 else nod := 365;
fty := fty + nod
end;
end
else
begin
for i := 2006 - 1 downto fdate.year do
begin
if leap(i) = True then nod := 366 else nod := 365;
fty := fty - nod
end;
end
end;
if fdate.month <> 1 then
begin
if leap(fdate.year) = True then
dmonth[2] := 29
else
dmonth[2] := 28;
for i := 1 to fdate.month - 1 do ftm := ftm + dmonth[i]
end;
ftdate := fty + ftm;

{dweek = weekday of first of the month}

if ftdate < 0 then
dweek := 7 + (ftdate mod 7)
else
dweek := ftdate mod 7;
pday := 1;
j := 0;

{Output calender}

writeln;
writeln('YEAR : ', fdate.year, ' month : ', fdate.month);
writeln;
writeln(' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ');
writeln;
repeat
if (pday = 1) and (j < dweek) then
write(' ')
else
begin
write(' ', pday:3, ' ');
pday := pday + 1
end;
j := j + 1;
if j > 6 then
begin
j := 0;
writeln
end;
until pday > dmonth[fdate.month];
writeln




end.


收錄日期: 2021-04-13 14:41:24
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071211000051KK02816

檢視 Wayback Machine 備份