Last Friday of each month: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, commented out prompt_number() to make it p2js compatible) |
No edit summary |
||
Line 2,902: | Line 2,902: | ||
[2012, 11, 30] |
[2012, 11, 30] |
||
[2012, 12, 28]</pre> |
[2012, 12, 28]</pre> |
||
=={{header|Pascal}}== |
|||
Using Free Pascal's DateUtils library would simplify the coding (see the Delphi example) but for older Pascal implementations the needed routines are the programmer's responsibility! |
|||
<lang Pascal> |
|||
program LastFriday; |
|||
{$mode objfpc}{$H+} |
|||
uses |
|||
SysUtils; |
|||
type |
|||
weekdays = (Sun,Mon,Tue,Wed,Thu,Fri,Sat); |
|||
var |
|||
m, d, y : integer; |
|||
function IsLeapYear(Year : integer) : boolean; |
|||
begin |
|||
if Year mod 4 <> 0 { quick exit in most likely case } |
|||
then IsLeapYear := false |
|||
else if Year mod 400 = 0 |
|||
then IsLeapYear := true |
|||
else if Year mod 100 = 0 |
|||
then IsLeapYear := false |
|||
else { non-century year and divisible by 4 } |
|||
IsLeapYear := true; |
|||
end; |
|||
function DaysInMonth(Month, Year : integer) : integer; |
|||
const |
|||
LastDay : array[1..12] of integer = |
|||
(31,28,31,30,31,30,31,31,30,31,30,31); |
|||
begin |
|||
if (Month = 2) and (IsLeapYear(Year)) then |
|||
DaysInMonth := 29 |
|||
else |
|||
DaysInMonth := LastDay[Month]; |
|||
end; |
|||
{ return day of week (Sun = 0, Mon = 1, etc.) for a } |
|||
{ given mo, da, and yr using Zeller's congruence } |
|||
function DayOfWeek(mo, da, yr : integer) : weekdays; |
|||
var |
|||
y, c, z : integer; |
|||
begin |
|||
if mo < 3 then |
|||
begin |
|||
mo := mo + 10; |
|||
yr := yr - 1 |
|||
end |
|||
else mo := mo - 2; |
|||
y := yr mod 100; |
|||
c := yr div 100; |
|||
z := (26 * mo - 2) div 10; |
|||
z := z + da + y + (y div 4) + (c div 4) - 2 * c + 777; |
|||
DayOfWeek := weekdays(z mod 7); |
|||
end; |
|||
{ return the calendar day of the last specified weekday } |
|||
{ in a given month and year } |
|||
function LastWeekday(k : weekdays; m, y : integer) : integer; |
|||
var |
|||
d : integer; |
|||
w : weekdays; |
|||
begin |
|||
{ determine weekday for the last day of the month } |
|||
d := DaysInMonth(m, y); |
|||
w := DayOfWeek(m, d, y); |
|||
{ find immediately prior occurance of weekday k } |
|||
if w >= k then |
|||
d := d - (ord(w) - ord(k)) |
|||
else |
|||
d := d - (7 - ord(k)) - ord(w); |
|||
LastWeekday := d; |
|||
end; |
|||
begin { main program } |
|||
write('Find last Fridays in what year? '); |
|||
readln(y); |
|||
writeln; |
|||
writeln('Month Last Fri'); |
|||
for m := 1 to 12 do |
|||
begin |
|||
d := LastWeekday(Fri, m, y); |
|||
writeln(m:5,' ',d:5, ' '); |
|||
end; |
|||
end. |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Find last Fridays in what year? 2020 |
|||
Month Last Fri |
|||
1 31 |
|||
2 28 |
|||
3 27 |
|||
4 24 |
|||
5 29 |
|||
6 26 |
|||
7 31 |
|||
8 28 |
|||
9 25 |
|||
10 30 |
|||
11 27 |
|||
12 25 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |