Doomsday rule: Difference between revisions
Content added Content deleted
(Added Algol W) |
(Added Algol 68 and fixed Algol W type and changed extra test case) |
||
Line 96: | Line 96: | ||
2077-02-12 -> Friday |
2077-02-12 -> Friday |
||
2101-04-02 -> Saturday |
2101-04-02 -> Saturday |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
{{TRans|ALGOL W}} |
|||
<lang algol68>BEGIN # find the day of the week of dates using John Conway's Doomsday rule # |
|||
# returns the day of the week (Sunday = 0, Monday = 1,...) for the date # |
|||
# specified by ccyy, mm and dd # |
|||
PROC dow = ( INT ccyy, mm, dd )INT: |
|||
BEGIN |
|||
INT doomsday = ( # Tuesday # 2 |
|||
+ ( 5 * ( ccyy MOD 4 ) ) |
|||
+ ( 4 * ( ccyy MOD 100 ) ) |
|||
+ ( 6 * ( ccyy MOD 400 ) ) |
|||
) |
|||
MOD 7; |
|||
BOOL is leap year = ccyy MOD 4 = 0 |
|||
AND ( ccyy MOD 100 /= 0 OR ccyy MOD 400 = 0 ); |
|||
INT anchor day = CASE mm |
|||
IN IF is leap year THEN 4 ELSE 3 FI |
|||
, IF is leap year THEN 1 ELSE 7 FI |
|||
, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 |
|||
ESAC; |
|||
( doomsday + ( dd - anchor day ) ) MOD 7 |
|||
END # dow # ; |
|||
BEGIN # task test cases # |
|||
# prints a test date and its day of the week # |
|||
PROC test dow = ( INT ccyy, mm, dd )VOID: |
|||
BEGIN |
|||
[]CHAR digit = "0123456789"[ AT 0 ]; |
|||
[]STRING day name = |
|||
[]STRING( "Sunday", "Monday", "Tuesday", "Wednesday" |
|||
, "Thursday", "Friday", "Saturday" |
|||
)[ AT 0 ]; |
|||
print( ( whole( ccyy, 0 ) |
|||
, "-", digit[ mm OVER 10 ], digit[ mm MOD 10 ] |
|||
, "-", digit[ dd OVER 10 ], digit[ dd MOD 10 ] |
|||
, ": ", day name[ dow( ccyy, mm, dd ) ] |
|||
, newline |
|||
) |
|||
) |
|||
END # test dow # ; |
|||
test dow( 1800, 1, 6 ); |
|||
test dow( 1875, 3, 29 ); |
|||
test dow( 1915, 12, 7 ); |
|||
test dow( 1970, 12, 23 ); |
|||
test dow( 2043, 5, 14 ); |
|||
test dow( 2077, 2, 12 ); |
|||
test dow( 2101, 4, 2 ); |
|||
test dow( 2022, 6, 19 ) |
|||
END |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
1800-01-06: Monday |
|||
1875-03-29: Monday |
|||
1915-12-07: Tuesday |
|||
1970-12-23: Wednesday |
|||
2043-05-14: Thursday |
|||
2077-02-12: Friday |
|||
2101-04-02: Saturday |
|||
2022-06-19: Sunday |
|||
</pre> |
</pre> |
||
Line 120: | Line 181: | ||
end dow ; |
end dow ; |
||
begin % task test cases % |
begin % task test cases % |
||
% prints a test date and its day of the |
% prints a test date and its day of the week % |
||
procedure testDow ( integer value ccyy, mm, dd ) ; |
procedure testDow ( integer value ccyy, mm, dd ) ; |
||
write( i_w := 1, s_w := 0 |
write( i_w := 1, s_w := 0 |
||
Line 139: | Line 200: | ||
testDow( 2077, 2, 12 ); |
testDow( 2077, 2, 12 ); |
||
testDow( 2101, 4, 2 ); |
testDow( 2101, 4, 2 ); |
||
testDow( 2022, 6, |
testDow( 2022, 6, 19 ) |
||
end |
end |
||
end.</lang> |
end.</lang> |
||
Line 151: | Line 212: | ||
2077-02-12: Friday |
2077-02-12: Friday |
||
2101-04-02: Saturday |
2101-04-02: Saturday |
||
2022-06- |
2022-06-19: Sunday |
||
</pre> |
</pre> |
||