Browse code

Rewrote in C

haywalk authored on 12/01/2022 01:42:17
Showing 3 changed files

... ...
@@ -8,6 +8,7 @@ to find the day of the week, by first calculating the 'anchor day' for the given
8 8
 then that of the given year, then finding the given day's offset from the first doomsday of
9 9
 the month, which is its offset from the year's anchor day.
10 10
 
11
-This program is written in Python 3.9. It is licensed under the
11
+This program is written in C. It is licensed under the
12 12
 [GNU GPL, version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). It was
13
-written by Hayden Walker ([www.haywalk.ca](https://www.haywalk.ca/)) on 2021-12-15. 
13
+written by Hayden Walker ([www.haywalk.ca](https://www.haywalk.ca/)),
14
+originally on 2021-12-15 in Python 3, then re-written in C on 2022-01-11.
14 15
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
1 74
deleted file mode 100644
... ...
@@ -1,116 +0,0 @@
1
-'''
2
-doomsday.py
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 2021-12-15
26
-'''
27
-
28
-def is_leap_year(year):
29
-	'''
30
-	Check if a year is a leap year
31
-	'''
32
-	return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
33
-
34
-def get_century_anchor(year):
35
-	'''
36
-	Return the century anchor day for a given year
37
-	'''
38
-	# Get century from year
39
-	century = year // 100
40
-	
41
-	# Calculate and return anchor day
42
-	century_anchor_day = (5 * (century % 4) % 7 + 2) % 7 - 1
43
-	return century_anchor_day
44
-
45
-def get_year_anchor(year):
46
-	'''
47
-	Return the anchor day for a given year
48
-	'''
49
-	# Get the century anchor day
50
-	century_anchor = get_century_anchor(year)
51
-	
52
-	# Get the last digits of the year
53
-	last_digits = year % 100
54
-	
55
-	# Calculate offset from century anchor day
56
-	a = last_digits // 12
57
-	b = last_digits % 12
58
-	c = b // 4
59
-	century_offset = a + b + c
60
-	
61
-	# Calculate and return the year anchor day
62
-	year_anchor = (century_anchor + century_offset) % 7
63
-	return year_anchor
64
-
65
-def get_day_number(year, month, day):
66
-	'''
67
-	Return the day of the week (0-6) for a given date
68
-	'''
69
-	# First doomsdays of each month for a common year
70
-	doomsdays_common = {1:3, 2:7, 3:7, 4:4, 5:2, 6:6, 7:4, 8:1,
71
-		9:5, 10:3, 11:7, 12:5}
72
-
73
-	# First doomsdays of each month for a leap year
74
-	doomsdays_leap = {1:4, 2:1, 3:7, 4:4, 5:2, 6:6, 7:4, 8:1,
75
-		9:5, 10:3, 11:7, 12:5}
76
-
77
-	# Get first doomsday for this month
78
-	if is_leap_year(year):
79
-		this_dd = doomsdays_leap[month]
80
-	else:
81
-		this_dd = doomsdays_common[month]
82
-
83
-	# Get the year anchor
84
-	year_anchor = get_year_anchor(year)
85
-	
86
-	# Calculate and return the day of the week based on distance from
87
-	# the first doomsday of the month
88
-	this_day = (year_anchor + (day - this_dd)) % 7
89
-	return this_day
90
-
91
-def get_day(year, month, day):
92
-	'''
93
-	For a given date, return the name of the day of the week
94
-	'''
95
-	# Get the day number
96
-	day_number = get_day_number(year, month, day)
97
-	
98
-	# Days of the week
99
-	days = ['Monday', 'Tuesday', 'Wednesday',
100
-		'Thursday', 'Friday', 'Saturday', 'Sunday']
101
-	
102
-	return days[day_number]
103
-
104
-# Get input from user
105
-user_input = input().split('-')
106
-
107
-# Get year, month, and day from input
108
-year = int(user_input[0])
109
-month = int(user_input[1])
110
-day = int(user_input[2])
111
-
112
-# Calculate day of the week
113
-weekday = get_day(year, month, day)
114
-
115
-# Print out result
116
-print(weekday)