Doomsday rule: Difference between revisions

Add C# implementation
(Inserted a Fortran Version of the algorithm)
(Add C# implementation)
 
(2 intermediate revisions by 2 users not shown)
Line 426:
February 12, 2077 will be on a Friday.
April 2, 2101 will be on a Saturday.</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
class Doom {
public static void Main(string[] args) {
Date[] dates = {
new Date(1800,1,6),
new Date(1875,3,29),
new Date(1915,12,7),
new Date(1970,12,23),
new Date(2043,5,14),
new Date(2077,2,12),
new Date(2101,4,2)
};
foreach (Date d in dates)
Console.WriteLine($"{d.Format()}: {d.Weekday()}");
}
}
 
class Date {
private int year, month, day;
private static readonly int[] leapDoom = {4,1,7,4,2,6,4,1,5,3,7,5};
private static readonly int[] normDoom = {3,7,7,4,2,6,4,1,5,3,7,5};
public static readonly string[] weekdays = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public bool IsLeapYear() {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public string Format() {
return $"{month:00}/{day:00}/{year:0000}";
}
public string Weekday() {
int c = year / 100;
int r = year % 100;
int s = r / 12;
int t = r % 12;
int cAnchor = (5 * (c % 4) + 2) % 7;
int doom = (s + t + t / 4 + cAnchor) % 7;
int anchor =
IsLeapYear() ? leapDoom[month - 1] : normDoom[month - 1];
return weekdays[(doom + day - anchor + 7) % 7];
}
}
</syntaxhighlight>
{{out}}
<pre>
01/06/1800: Monday
03/29/1875: Monday
12/07/1915: Tuesday
12/23/1970: Wednesday
05/14/2043: Thursday
02/12/2077: Friday
04/02/2101: Saturday
 
</pre>
 
=={{header|C++}}==
Line 730 ⟶ 804:
M= 4 D= 2 Y= 2101 DAY=SATURDAY</pre>
 
=={{header|FORTRANFortran}}==
<syntaxhighlight lang="FORTRAN">
program someday ! Calculates the day that a week falls on
Line 951 ⟶ 1,025:
2101-04-02 (April 2, 2101) : Saturday
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim shared as ubyte fdoom(0 to 1, 1 to 12) = {_
Line 1,792 ⟶ 1,867:
2077-02-12: Friday
2101-04-02: Saturday
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object Doom extends App {
val dates = Array(
new Date(1800, 1, 6),
new Date(1875, 3, 29),
new Date(1915, 12, 7),
new Date(1970, 12, 23),
new Date(2043, 5, 14),
new Date(2077, 2, 12),
new Date(2101, 4, 2)
)
 
dates.foreach(d => println(s"${d.format}: ${d.weekday}"))
}
 
class Date(val year: Int, val month: Int, val day: Int) {
import Date._
 
def isLeapYear: Boolean = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
 
def format: String = f"$month%02d/$day%02d/$year%04d"
 
def weekday: String = {
val c = year / 100
val r = year % 100
val s = r / 12
val t = r % 12
 
val cAnchor = (5 * (c % 4) + 2) % 7
val doom = (s + t + t / 4 + cAnchor) % 7
val anchor = if (isLeapYear) leapdoom(month - 1) else normdoom(month - 1)
 
weekdays((doom + day - anchor + 7) % 7)
}
}
 
object Date {
private val leapdoom = Array(4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5)
private val normdoom = Array(3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5)
val weekdays = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
}
</syntaxhighlight>
{{out}}
<pre>
01/06/1800: Monday
03/29/1875: Monday
12/07/1915: Tuesday
12/23/1970: Wednesday
05/14/2043: Thursday
02/12/2077: Friday
04/02/2101: Saturday
 
</pre>
 
338

edits