Browse code

Rewrote in C

haywalk authored on 12/01/2022 01:42:17
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,72 @@
1
+/*
2
+ * doomsday.c
3
+ * 
4
+ * Copyright 2021 Hayden D. Walker <planethaywalk@aol.com>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
+ *
19
+ * Conway's Doomsday Rule
20
+ *
21
+ * Given a date in the Gregorian calendar, (YYYY-MM-DD), calculate the day
22
+ * of the week using John Horton Conway's Doomsday Algorithm.
23
+ *
24
+ * Program by Hayden Walker (www.haywalk.ca)
25
+ * Written 2022-01-11
26
+ *
27
+ */
28
+
29
+#include <stdio.h>
30
+
31
+int main()
32
+{
33
+	/* get date from user */
34
+	int year, month, day;
35
+	scanf("%d-%d-%d", &year, &month, &day);
36
+	
37
+	/* calculate century anchor day */
38
+	int century, century_anchor;
39
+	century = year / 100;
40
+	century_anchor = (5 * (century % 4) % 7 + 2) % 7 - 1;
41
+	
42
+	/* calculate year's anchor day */
43
+	int a, b, c, d, year_anchor;
44
+	a = (year % 100) / 12;
45
+	b = (year % 100) % 12;
46
+	c = b / 4;
47
+	d = a + b + c;
48
+	year_anchor = (century_anchor + d) % 7;
49
+	
50
+	/* doomsdays for each month in normal and leap years */
51
+	int non_leap[] = {3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5};
52
+	int leap[] = {4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5};
53
+	
54
+	/* get nearest doomsday based on month and leap year */
55
+	int nearest_doomsday;
56
+	
57
+	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
58
+		nearest_doomsday = leap[month - 1];
59
+	else
60
+		nearest_doomsday = non_leap[month - 1];
61
+	
62
+	/* get distance from doomsday */
63
+	int this_day;
64
+	this_day = (year_anchor + (day - nearest_doomsday)) % 7;
65
+	
66
+	/* get name of day */
67
+	char days[7][10] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday", "Sunday"};
68
+	
69
+	/* output result and end program */
70
+	printf("%s\n", days[this_day]);
71
+	return 0;
72
+}
0 73
\ No newline at end of file