Find the last Sunday of each month: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(Moved Algol 68 to the correct place) |
||
Line 170: | Line 170: | ||
2013-11-24 |
2013-11-24 |
||
2013-12-29</pre> |
2013-12-29</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{Trans|ALGOL W}} |
|||
<lang algol68>BEGIN # find the last Sunday in each month of a year # |
|||
# returns true if year is a leap year, false otherwise # |
|||
# assumes year is in the Gregorian Calendar # |
|||
PROC is leap year = ( INT year )BOOL: |
|||
year MOD 400 = 0 OR ( year MOD 4 = 0 AND year MOD 100 /= 0 ); |
|||
# returns the day of the week of the specified date (d/m/y) # |
|||
# Sunday = 1 # |
|||
PROC day of week = ( INT d, m, y )INT: |
|||
BEGIN |
|||
INT mm := m; |
|||
INT yy := y; |
|||
IF mm <= 2 THEN |
|||
mm := mm + 12; |
|||
yy := yy - 1 |
|||
FI; |
|||
INT j = yy OVER 100; |
|||
INT k = yy MOD 100; |
|||
(d + ( ( mm + 1 ) * 26 ) OVER 10 + k + k OVER 4 + j OVER 4 + 5 * j ) MOD 7 |
|||
END # day of week # ; |
|||
# returns an array of the last Sunday of each month in year # |
|||
PROC last sundays = ( INT year )[]INT: |
|||
BEGIN |
|||
[ 1 : 12 ]INT last days := ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); |
|||
IF is leap year( year ) THEN last days[ 2 ] := 29 FI; |
|||
# for each month, determine the day number of the # |
|||
# last Sunday # |
|||
[ 1 : 12 ]INT last; |
|||
FOR m pos TO 12 DO |
|||
INT dow := day of week( last days[ m pos ], m pos, year ); |
|||
IF dow = 0 # Saturday # THEN dow := 7 FI; |
|||
# calculate the offset for the previous Sunday # |
|||
last[ m pos ] := ( last days[ m pos ] + 1 ) - dow |
|||
OD; |
|||
last |
|||
END # last sundays # ; |
|||
# test the last sundays procedure # |
|||
INT year = 2021; |
|||
[]INT last = last sundays( year ); |
|||
FOR m pos TO 12 DO |
|||
print( ( whole( year, 0 ) |
|||
, IF m pos < 10 THEN "-0" ELSE "-1" FI |
|||
, whole( m pos MOD 10, 0 ) |
|||
, "-" |
|||
, whole( last[ m pos ], 0 ) |
|||
, newline |
|||
) |
|||
) |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
2021-01-31 |
|||
2021-02-28 |
|||
2021-03-28 |
|||
2021-04-25 |
|||
2021-05-30 |
|||
2021-06-27 |
|||
2021-07-25 |
|||
2021-08-29 |
|||
2021-09-26 |
|||
2021-10-31 |
|||
2021-11-28 |
|||
2021-12-26 |
|||
</pre> |
|||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
Line 297: | Line 364: | ||
NOV 28 |
NOV 28 |
||
DEC 26 |
DEC 26 |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
{{Trans|ALGOL W}} |
|||
<lang algol68>BEGIN # find the last Sunday in each month of a year # |
|||
# returns true if year is a leap year, false otherwise # |
|||
# assumes year is in the Gregorian Calendar # |
|||
PROC is leap year = ( INT year )BOOL: |
|||
year MOD 400 = 0 OR ( year MOD 4 = 0 AND year MOD 100 /= 0 ); |
|||
# returns the day of the week of the specified date (d/m/y) # |
|||
# Sunday = 1 # |
|||
PROC day of week = ( INT d, m, y )INT: |
|||
BEGIN |
|||
INT mm := m; |
|||
INT yy := y; |
|||
IF mm <= 2 THEN |
|||
mm := mm + 12; |
|||
yy := yy - 1 |
|||
FI; |
|||
INT j = yy OVER 100; |
|||
INT k = yy MOD 100; |
|||
(d + ( ( mm + 1 ) * 26 ) OVER 10 + k + k OVER 4 + j OVER 4 + 5 * j ) MOD 7 |
|||
END # day of week # ; |
|||
# returns an array of the last Sunday of each month in year # |
|||
PROC last sundays = ( INT year )[]INT: |
|||
BEGIN |
|||
[ 1 : 12 ]INT last days := ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); |
|||
IF is leap year( year ) THEN last days[ 2 ] := 29 FI; |
|||
# for each month, determine the day number of the # |
|||
# last Sunday # |
|||
[ 1 : 12 ]INT last; |
|||
FOR m pos TO 12 DO |
|||
INT dow := day of week( last days[ m pos ], m pos, year ); |
|||
IF dow = 0 # Saturday # THEN dow := 7 FI; |
|||
# calculate the offset for the previous Sunday # |
|||
last[ m pos ] := ( last days[ m pos ] + 1 ) - dow |
|||
OD; |
|||
last |
|||
END # last sundays # ; |
|||
# test the last sundays procedure # |
|||
INT year = 2021; |
|||
[]INT last = last sundays( year ); |
|||
FOR m pos TO 12 DO |
|||
print( ( whole( year, 0 ) |
|||
, IF m pos < 10 THEN "-0" ELSE "-1" FI |
|||
, whole( m pos MOD 10, 0 ) |
|||
, "-" |
|||
, whole( last[ m pos ], 0 ) |
|||
, newline |
|||
) |
|||
) |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
2021-01-31 |
|||
2021-02-28 |
|||
2021-03-28 |
|||
2021-04-25 |
|||
2021-05-30 |
|||
2021-06-27 |
|||
2021-07-25 |
|||
2021-08-29 |
|||
2021-09-26 |
|||
2021-10-31 |
|||
2021-11-28 |
|||
2021-12-26 |
|||
</pre> |
</pre> |
||