mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-13 13:37:38 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
7
95_Weekday/README.md
Normal file
7
95_Weekday/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### Weekday
|
||||
|
||||
As published in Basic Computer Games (1978)
|
||||
https://www.atariarchives.org/basicgames/showpage.php?page=179
|
||||
|
||||
Downloaded from Vintage Basic at
|
||||
http://www.vintage-basic.net/games.html
|
||||
3
95_Weekday/csharp/README.md
Normal file
3
95_Weekday/csharp/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/)
|
||||
3
95_Weekday/java/README.md
Normal file
3
95_Weekday/java/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
280
95_Weekday/java/Weekday.java
Normal file
280
95_Weekday/java/Weekday.java
Normal file
@@ -0,0 +1,280 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* WEEKDAY
|
||||
*
|
||||
* Converted from BASIC to Java by Aldrin Misquitta (@aldrinm)
|
||||
*
|
||||
*/
|
||||
public class Weekday {
|
||||
|
||||
//TABLE OF VALUES FOR THE MONTHS TO BE USED IN CALCULATIONS.
|
||||
//Dummy value added at index 0, so we can reference directly by the month number value
|
||||
private final static int[] t = new int[]{-1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
|
||||
|
||||
public static void main(String[] args) {
|
||||
printIntro();
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ");
|
||||
DateStruct todaysDate = readDate(scanner);
|
||||
|
||||
System.out.print("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST) ");
|
||||
DateStruct dateOfInterest = readDate(scanner);
|
||||
|
||||
int I1 = (dateOfInterest.year - 1500) / 100;
|
||||
//TEST FOR DATE BEFORE CURRENT CALENDAR.
|
||||
if ((dateOfInterest.year - 1582) >= 0) {
|
||||
int A = I1 * 5 + (I1 + 3) / 4;
|
||||
int I2 = (A - b(A) * 7);
|
||||
int Y2 = (dateOfInterest.year / 100);
|
||||
int Y3 = (dateOfInterest.year - Y2 * 100);
|
||||
A = Y3 / 4 + Y3 + dateOfInterest.day + t[dateOfInterest.month] + I2;
|
||||
calculateAndPrintDayOfWeek(I1, A, todaysDate, dateOfInterest, Y3);
|
||||
|
||||
if ((todaysDate.year * 12 + todaysDate.month) * 31 + todaysDate.day
|
||||
== (dateOfInterest.year * 12 + dateOfInterest.month) * 31 + dateOfInterest.day) {
|
||||
return; //stop the program
|
||||
}
|
||||
|
||||
int I5 = todaysDate.year - dateOfInterest.year;
|
||||
System.out.print("\n");
|
||||
int I6 = todaysDate.month - dateOfInterest.month;
|
||||
int I7 = todaysDate.day - dateOfInterest.day;
|
||||
if (I7 < 0) {
|
||||
I6 = I6 - 1;
|
||||
I7 = I7 + 30;
|
||||
}
|
||||
if (I6 < 0) {
|
||||
I5 = I5 - 1;
|
||||
I6 = I6 + 12;
|
||||
}
|
||||
if (I5 < 0) {
|
||||
return; //do nothing. end the program
|
||||
} else {
|
||||
if (I7 != 0) {
|
||||
printHeadersAndAge(I5, I6, I7);
|
||||
} else {
|
||||
if (I6 != 0) {
|
||||
printHeadersAndAge(I5, I6, I7);
|
||||
} else {
|
||||
System.out.println("***HAPPY BIRTHDAY***");
|
||||
printHeadersAndAge(I5, I6, I7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int A8 = (I5 * 365) + (I6 * 30) + I7 + (I6 / 2);
|
||||
int K5 = I5;
|
||||
int K6 = I6;
|
||||
int K7 = I7;
|
||||
//CALCULATE RETIREMENT DATE.
|
||||
int E = dateOfInterest.year + 65;
|
||||
// CALCULATE TIME SPENT IN THE FOLLOWING FUNCTIONS.
|
||||
float F = 0.35f;
|
||||
System.out.printf("%-28s", "YOU HAVE SLEPT");
|
||||
DateStruct scratchDate = new DateStruct(K6, K7, K5); //K5 is a temp year, K6 is month, K7 is day
|
||||
printStatisticRow(F, A8, scratchDate);
|
||||
K5 = scratchDate.year;
|
||||
K6 = scratchDate.month;
|
||||
K7 = scratchDate.day;
|
||||
|
||||
F = 0.17f;
|
||||
System.out.printf("%-28s", "YOU HAVE EATEN");
|
||||
|
||||
scratchDate = new DateStruct(K6, K7, K5);
|
||||
printStatisticRow(F, A8, scratchDate);
|
||||
K5 = scratchDate.year;
|
||||
K6 = scratchDate.month;
|
||||
K7 = scratchDate.day;
|
||||
|
||||
F = 0.23f;
|
||||
if (K5 > 3) {
|
||||
if (K5 > 9) {
|
||||
System.out.printf("%-28s", "YOU HAVE WORKED/PLAYED");
|
||||
} else {
|
||||
System.out.printf("%-28s", "YOU HAVE PLAYED/STUDIED");
|
||||
}
|
||||
} else {
|
||||
System.out.printf("%-28s", "YOU HAVE PLAYED");
|
||||
}
|
||||
|
||||
scratchDate = new DateStruct(K6, K7, K5);
|
||||
printStatisticRow(F, A8, scratchDate);
|
||||
K5 = scratchDate.year;
|
||||
K6 = scratchDate.month;
|
||||
K7 = scratchDate.day;
|
||||
|
||||
if (K6 == 12) {
|
||||
K5 = K5 + 1;
|
||||
K6 = 0;
|
||||
}
|
||||
System.out.printf("%-28s%14s%14s%14s%n", "YOU HAVE RELAXED", K5, K6, K7);
|
||||
System.out.printf("%16s*** YOU MAY RETIRE IN %s ***%n", " ", E);
|
||||
System.out.printf("%n%n%n%n%n");
|
||||
} else {
|
||||
System.out.println("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void printStatisticRow(float F, int A8, DateStruct scratchDate) {
|
||||
int K1 = (int) (F * A8);
|
||||
int I5 = K1 / 365;
|
||||
K1 = K1 - (I5 * 365);
|
||||
int I6 = K1 / 30;
|
||||
int I7 = K1 - (I6 * 30);
|
||||
int K5 = scratchDate.year - I5;
|
||||
int K6 = scratchDate.month - I6;
|
||||
int K7 = scratchDate.day - I7;
|
||||
if (K7 < 0) {
|
||||
K7 = K7 + 30;
|
||||
K6 = K6 - 1;
|
||||
}
|
||||
if (K6 <= 0) {
|
||||
K6 = K6 + 12;
|
||||
K5 = K5 - 1;
|
||||
}
|
||||
//to return the updated values of K5, K6, K7 we send them through the scratchDate
|
||||
scratchDate.year = K5;
|
||||
scratchDate.month = K6;
|
||||
scratchDate.day = K7;
|
||||
System.out.printf("%14s%14s%14s%n", I5, I6, I7);
|
||||
}
|
||||
|
||||
private static void printHeadersAndAge(int I5, int I6, int I7) {
|
||||
System.out.printf("%14s%14s%14s%14s%14s%n", " ", " ", "YEARS", "MONTHS", "DAYS");
|
||||
System.out.printf("%14s%14s%14s%14s%14s%n", " ", " ", "-----", "------", "----");
|
||||
System.out.printf("%-28s%14s%14s%14s%n", "YOUR AGE (IF BIRTHDATE)", I5, I6, I7);
|
||||
}
|
||||
|
||||
private static void calculateAndPrintDayOfWeek(int i1, int a, DateStruct dateStruct, DateStruct dateOfInterest, int y3) {
|
||||
int b = (a - b(a) * 7) + 1;
|
||||
if (dateOfInterest.month > 2) {
|
||||
printDayOfWeek(dateStruct, dateOfInterest, b);
|
||||
} else {
|
||||
if (y3 == 0) {
|
||||
int aa = i1 - 1;
|
||||
int t1 = aa - a(aa) * 4;
|
||||
if (t1 == 0) {
|
||||
if (b != 0) {
|
||||
b = b - 1;
|
||||
printDayOfWeek(dateStruct, dateOfInterest, b);
|
||||
} else {
|
||||
b = 6;
|
||||
b = b - 1;
|
||||
printDayOfWeek(dateStruct, dateOfInterest, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PRINT THE DAY OF THE WEEK THE DATE FALLS ON.
|
||||
*/
|
||||
private static void printDayOfWeek(DateStruct dateStruct, DateStruct dateOfInterest, int b) {
|
||||
if (b == 0) {
|
||||
b = 7;
|
||||
}
|
||||
if ((dateStruct.year * 12 + dateStruct.month) * 31
|
||||
+ dateStruct.day
|
||||
<
|
||||
(dateOfInterest.year * 12
|
||||
+ dateOfInterest.month) * 31 + dateOfInterest.day) {
|
||||
System.out.printf("%s / %s / %s WILL BE A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
|
||||
} else if ((dateStruct.year * 12 + dateStruct.month) * 31
|
||||
+ dateStruct.day == (dateOfInterest.year * 12 + dateOfInterest.month)
|
||||
* 31 + dateOfInterest.day) {
|
||||
System.out.printf("%s / %s / %s IS A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
|
||||
} else {
|
||||
System.out.printf("%s / %s / %s WAS A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
|
||||
}
|
||||
switch (b) {
|
||||
case 1:
|
||||
System.out.println("SUNDAY.");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("MONDAY.");
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("TUESDAY.");
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("WEDNESDAY.");
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("THURSDAY.");
|
||||
break;
|
||||
case 6:
|
||||
if (dateOfInterest.day == 13) {
|
||||
System.out.println("FRIDAY THE THIRTEENTH---BEWARE!");
|
||||
} else {
|
||||
System.out.println("FRIDAY.");
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
System.out.println("SATURDAY.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static int a(int a) {
|
||||
return a / 4;
|
||||
}
|
||||
|
||||
private static int b(int a) {
|
||||
return a / 7;
|
||||
}
|
||||
|
||||
|
||||
private static void printIntro() {
|
||||
System.out.println(" WEEKDAY");
|
||||
System.out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n\n");
|
||||
System.out.println("WEEKDAY IS A COMPUTER DEMONSTRATION THAT");
|
||||
System.out.println("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.");
|
||||
System.out.println("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read user input for a date, do some validation and return a simple date structure
|
||||
*/
|
||||
private static DateStruct readDate(Scanner scanner) {
|
||||
boolean done = false;
|
||||
int mm = 0, dd = 0, yyyy = 0;
|
||||
while (!done) {
|
||||
String input = scanner.next();
|
||||
String[] tokens = input.split(",");
|
||||
if (tokens.length < 3) {
|
||||
System.out.println("DATE EXPECTED IN FORM: 3,24,1979 - RETRY INPUT LINE");
|
||||
} else {
|
||||
try {
|
||||
mm = Integer.parseInt(tokens[0]);
|
||||
dd = Integer.parseInt(tokens[1]);
|
||||
yyyy = Integer.parseInt(tokens[2]);
|
||||
done = true;
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.out.println("NUMBER EXPECTED - RETRY INPUT LINE");
|
||||
}
|
||||
}
|
||||
}
|
||||
return new DateStruct(mm, dd, yyyy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience date structure to hold user date input
|
||||
*/
|
||||
private static class DateStruct {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
|
||||
public DateStruct(int month, int day, int year) {
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
this.year = year;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
3
95_Weekday/javascript/README.md
Normal file
3
95_Weekday/javascript/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells)
|
||||
9
95_Weekday/javascript/weekday.html
Normal file
9
95_Weekday/javascript/weekday.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>WEEKDAY</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="weekday.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
208
95_Weekday/javascript/weekday.js
Normal file
208
95_Weekday/javascript/weekday.js
Normal file
@@ -0,0 +1,208 @@
|
||||
// WEEKDAY
|
||||
//
|
||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||
//
|
||||
|
||||
function print(str)
|
||||
{
|
||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||
}
|
||||
|
||||
function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
document.getElementById("output").appendChild(input_element);
|
||||
input_element.focus();
|
||||
input_str = undefined;
|
||||
input_element.addEventListener("keydown", function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
input_str = input_element.value;
|
||||
document.getElementById("output").removeChild(input_element);
|
||||
print(input_str);
|
||||
print("\n");
|
||||
resolve(input_str);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tab(space)
|
||||
{
|
||||
var str = "";
|
||||
while (space-- > 0)
|
||||
str += " ";
|
||||
return str;
|
||||
}
|
||||
|
||||
function fna(arg) {
|
||||
return Math.floor(arg / 4);
|
||||
}
|
||||
|
||||
function fnb(arg) {
|
||||
return Math.floor(arg / 7);
|
||||
}
|
||||
|
||||
var t = [, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
|
||||
|
||||
var k5;
|
||||
var k6;
|
||||
var k7;
|
||||
|
||||
function time_spent(f, a8)
|
||||
{
|
||||
k1 = Math.floor(f * a8);
|
||||
i5 = Math.floor(k1 / 365);
|
||||
k1 -= i5 * 365;
|
||||
i6 = Math.floor(k1 / 30);
|
||||
i7 = k1 - (i6 * 30);
|
||||
k5 -= i5;
|
||||
k6 -= i6;
|
||||
k7 -= i7;
|
||||
if (k7 < 0) {
|
||||
k7 += 30;
|
||||
k6--;
|
||||
}
|
||||
if (k6 <= 0) {
|
||||
k6 += 12;
|
||||
k5--;
|
||||
}
|
||||
print(i5 + "\t" + i6 + "\t" + i7 + "\n");
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(32) + "WEEKDAY\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("WEEKDAY IS A COMPUTER DEMONSTRATION THAT\n");
|
||||
print("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.\n");
|
||||
print("\n");
|
||||
print("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ");
|
||||
str = await input();
|
||||
m1 = parseInt(str);
|
||||
d1 = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
y1 = parseInt(str.substr(str.lastIndexOf(",") + 1));
|
||||
// This program determines the day of the week
|
||||
// for a date after 1582
|
||||
print("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST)");
|
||||
str = await input();
|
||||
m = parseInt(str);
|
||||
d = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
y = parseInt(str.substr(str.lastIndexOf(",") + 1));
|
||||
print("\n");
|
||||
i1 = Math.floor((y - 1500) / 100);
|
||||
// Test for date before current calendar.
|
||||
if (y - 1582 < 0) {
|
||||
print("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.\n");
|
||||
} else {
|
||||
a = i1 * 5 + (i1 + 3) / 4;
|
||||
i2 = Math.floor(a - fnb(a) * 7);
|
||||
y2 = Math.floor(y / 100);
|
||||
y3 = Math.floor(y - y2 * 100);
|
||||
a = y3 / 4 + y3 + d + t[m] + i2;
|
||||
b = Math.floor(a - fnb(a) * 7) + 1;
|
||||
if (m <= 2) {
|
||||
if (y3 != 0) {
|
||||
t1 = Math.floor(y - fna(y) * 4);
|
||||
} else {
|
||||
a = i1 - 1;
|
||||
t1 = Math.floor(a - fna(a) * 4);
|
||||
}
|
||||
if (t1 == 0) {
|
||||
if (b == 0)
|
||||
b = 6;
|
||||
b--;
|
||||
}
|
||||
}
|
||||
if (b == 0)
|
||||
b = 7;
|
||||
if ((y1 * 12 + m1) * 31 + d1 < (y * 12 + m) * 31 + d) {
|
||||
print(m + "/" + d + "/" + y + " WILL BE A ");
|
||||
} else if ((y1 * 12 + m1) * 31 + d1 == (y * 12 + m) * 31 + d) {
|
||||
print(m + "/" + d + "/" + y + " IS A ");
|
||||
} else {
|
||||
print(m + "/" + d + "/" + y + " WAS A ");
|
||||
}
|
||||
switch (b) {
|
||||
case 1: print("SUNDAY.\n"); break;
|
||||
case 2: print("MONDAY.\n"); break;
|
||||
case 3: print("TUESDAY.\n"); break;
|
||||
case 4: print("WEDNESDAY.\n"); break;
|
||||
case 5: print("THURSDAY.\n"); break;
|
||||
case 6:
|
||||
if (d == 13) {
|
||||
print("FRIDAY THE THIRTEENTH---BEWARE!\n");
|
||||
} else {
|
||||
print("FRIDAY.\n");
|
||||
}
|
||||
break;
|
||||
case 7: print("SATURDAY.\n"); break;
|
||||
}
|
||||
if ((y1 * 12 + m1) * 31 + d1 != (y * 12 + m) * 31 + d) {
|
||||
i5 = y1 - y;
|
||||
print("\n");
|
||||
i6 = m1 - m;
|
||||
i7 = d1 - d;
|
||||
if (i7 < 0) {
|
||||
i6--;
|
||||
i7 += 30;
|
||||
}
|
||||
if (i6 < 0) {
|
||||
i5--;
|
||||
i6 += 12;
|
||||
}
|
||||
if (i5 >= 0) {
|
||||
if (i7 == 0 && i6 == 0)
|
||||
print("***HAPPY BIRTHDAY***\n");
|
||||
print(" \tYEARS\tMONTHS\tDAYS\n");
|
||||
print(" \t-----\t------\t----\n");
|
||||
print("YOUR AGE (IF BIRTHDATE) \t" + i5 + "\t" + i6 + "\t" + i7 + "\n");
|
||||
a8 = (i5 * 365) + (i6 * 30) + i7 + Math.floor(i6 / 2);
|
||||
k5 = i5;
|
||||
k6 = i6;
|
||||
k7 = i7;
|
||||
// Calculate retirement date.
|
||||
e = y + 65;
|
||||
// Calculate time spent in the following functions.
|
||||
print("YOU HAVE SLEPT \t\t\t");
|
||||
time_spent(0.35, a8);
|
||||
print("YOU HAVE EATEN \t\t\t");
|
||||
time_spent(0.17, a8);
|
||||
if (k5 <= 3) {
|
||||
print("YOU HAVE PLAYED \t\t\t");
|
||||
} else if (k5 <= 9) {
|
||||
print("YOU HAVE PLAYED/STUDIED \t\t");
|
||||
} else {
|
||||
print("YOU HAVE WORKED/PLAYED \t\t");
|
||||
}
|
||||
time_spent(0.23, a8);
|
||||
if (k6 == 12) {
|
||||
k5++;
|
||||
k6 = 0;
|
||||
}
|
||||
print("YOU HAVE RELAXED \t\t" + k5 + "\t" + k6 + "\t" + k7 + "\n");
|
||||
print("\n");
|
||||
print(tab(16) + "*** YOU MAY RETIRE IN " + e + " ***\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
|
||||
main();
|
||||
3
95_Weekday/pascal/README.md
Normal file
3
95_Weekday/pascal/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))
|
||||
3
95_Weekday/perl/README.md
Normal file
3
95_Weekday/perl/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Perl](https://www.perl.org/)
|
||||
3
95_Weekday/python/README.md
Normal file
3
95_Weekday/python/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Python](https://www.python.org/about/)
|
||||
288
95_Weekday/python/weekday.py
Normal file
288
95_Weekday/python/weekday.py
Normal file
@@ -0,0 +1,288 @@
|
||||
"""
|
||||
WEEKDAY
|
||||
|
||||
Calculates which weekday an entered date is.
|
||||
|
||||
Also estimates how long a person has done certain activities, if they
|
||||
entered their birthday.
|
||||
|
||||
Also calculates the year of retirement, assuming retiring at age 65.
|
||||
|
||||
Ported by Dave LeCompte.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
|
||||
GET_TODAY_FROM_SYSTEM = True
|
||||
|
||||
|
||||
def print_with_tab(space_count, s):
|
||||
if space_count > 0:
|
||||
spaces = " " * space_count
|
||||
else:
|
||||
spaces = ""
|
||||
print(spaces + s)
|
||||
|
||||
|
||||
def get_date_from_user(prompt):
|
||||
while True:
|
||||
print(prompt)
|
||||
date_str = input()
|
||||
try:
|
||||
month_num, day_num, year_num = [int(x) for x in date_str.split(",")]
|
||||
except Exception as e:
|
||||
print("I COULDN'T UNDERSTAND THAT. TRY AGAIN.")
|
||||
return month_num, day_num, year_num
|
||||
|
||||
|
||||
def get_date_from_system():
|
||||
dt = datetime.datetime.today()
|
||||
return dt.month, dt.day, dt.year
|
||||
|
||||
|
||||
def get_day_of_week(weekday_index, day):
|
||||
day_names = {
|
||||
1: "SUNDAY",
|
||||
2: "MONDAY",
|
||||
3: "TUESDAY",
|
||||
4: "WEDNESDAY",
|
||||
5: "THURSDAY",
|
||||
6: "FRIDAY",
|
||||
7: "SATURDAY",
|
||||
}
|
||||
|
||||
if weekday_index == 6 and day == 13:
|
||||
return "FRIDAY THE THIRTEENTH---BEWARE!"
|
||||
return day_names[weekday_index]
|
||||
|
||||
|
||||
def previous_day(b):
|
||||
if b == 0:
|
||||
b = 6
|
||||
return b - 1
|
||||
|
||||
|
||||
def is_leap_year(year):
|
||||
if (year % 4) != 0:
|
||||
return False
|
||||
if (year % 100) != 0:
|
||||
return True
|
||||
if (year % 400) != 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def adjust_day_for_leap_year(b, year):
|
||||
if is_leap_year(year):
|
||||
b = previous_day(b)
|
||||
return b
|
||||
|
||||
|
||||
def adjust_weekday(b, month, year):
|
||||
if month <= 2:
|
||||
b = adjust_day_for_leap_year(b, year)
|
||||
if b == 0:
|
||||
b = 7
|
||||
return b
|
||||
|
||||
|
||||
def calc_day_value(year, month, day):
|
||||
return (year * 12 + month) * 31 + day
|
||||
|
||||
|
||||
def deduct_time(frac, days, years_remain, months_remain, days_remain):
|
||||
# CALCULATE TIME IN YEARS, MONTHS, AND DAYS
|
||||
days_available = int(frac * days)
|
||||
years_used = int(days_available / 365)
|
||||
days_available -= years_used * 365
|
||||
months_used = int(days_available / 30)
|
||||
days_used = days_available - (months_used * 30)
|
||||
years_remain = years_remain - years_used
|
||||
months_remain = months_remain - months_used
|
||||
days_remain = days_remain - days_used
|
||||
|
||||
while days_remain < 0:
|
||||
days_remain += 30
|
||||
months_remain -= 1
|
||||
|
||||
while months_remain < 0 and years_remain > 0:
|
||||
months_remain += 12
|
||||
years_remain -= 1
|
||||
return years_remain, months_remain, days_remain, years_used, months_used, days_used
|
||||
|
||||
|
||||
def time_report(msg, years, months, days):
|
||||
leading_spaces = 23 - len(msg)
|
||||
print_with_tab(leading_spaces, msg + f"\t{years}\t{months}\t{days}")
|
||||
|
||||
|
||||
def make_occupation_label(years):
|
||||
if years <= 3:
|
||||
return "PLAYED"
|
||||
elif years <= 9:
|
||||
return "PLAYED/STUDIED"
|
||||
else:
|
||||
return "WORKED/PLAYED"
|
||||
|
||||
|
||||
def calculate_day_of_week(year, month, day):
|
||||
# Initial values for months
|
||||
month_table = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5]
|
||||
|
||||
i1 = int((year - 1500) / 100)
|
||||
a = i1 * 5 + (i1 + 3) / 4
|
||||
i2 = int(a - int(a / 7) * 7)
|
||||
y2 = int(year / 100)
|
||||
y3 = int(year - y2 * 100)
|
||||
a = y3 / 4 + y3 + day + month_table[month - 1] + i2
|
||||
b = int(a - int(a / 7) * 7) + 1
|
||||
b = adjust_weekday(b, month, year)
|
||||
|
||||
return b
|
||||
|
||||
|
||||
def end():
|
||||
for i in range(5):
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
print_with_tab(32, "WEEKDAY")
|
||||
print_with_tab(15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print("WEEKDAY IS A COMPUTER DEMONSTRATION THAT")
|
||||
print("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.")
|
||||
print()
|
||||
|
||||
if GET_TODAY_FROM_SYSTEM:
|
||||
month_today, day_today, year_today = get_date_from_system()
|
||||
else:
|
||||
month_today, day_today, year_today = get_date_from_user(
|
||||
"ENTER TODAY'S DATE IN THE FORM: 3,24,1979"
|
||||
)
|
||||
|
||||
# This program determines the day of the week
|
||||
# for a date after 1582
|
||||
|
||||
print()
|
||||
|
||||
month, day, year = get_date_from_user(
|
||||
"ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST) (like MM,DD,YYYY)"
|
||||
)
|
||||
|
||||
print()
|
||||
|
||||
# Test for date before current calendar
|
||||
if year < 1582:
|
||||
print("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.")
|
||||
end()
|
||||
return
|
||||
|
||||
b = calculate_day_of_week(year, month, day)
|
||||
|
||||
today_day_value = calc_day_value(year_today, month_today, day_today)
|
||||
target_day_value = calc_day_value(year, month, day)
|
||||
|
||||
is_today = False
|
||||
is_future = False
|
||||
|
||||
if today_day_value < target_day_value:
|
||||
label = "WILL BE A"
|
||||
is_future = False
|
||||
elif today_day_value == target_day_value:
|
||||
label = "IS A"
|
||||
is_today = True
|
||||
else:
|
||||
label = "WAS A"
|
||||
|
||||
day_name = get_day_of_week(b, day)
|
||||
|
||||
# print the day of the week the date falls on.
|
||||
print(f"{month}/{day}/{year} {label} {day_name}.")
|
||||
|
||||
if is_today:
|
||||
# nothing to report for today
|
||||
end()
|
||||
return
|
||||
|
||||
print()
|
||||
|
||||
el_years = year_today - year
|
||||
el_months = month_today - month
|
||||
el_days = day_today - day
|
||||
|
||||
if el_days < 0:
|
||||
el_months = el_months - 1
|
||||
el_days = el_days + 30
|
||||
if el_months < 0:
|
||||
el_years = el_years - 1
|
||||
el_months = el_months + 12
|
||||
if el_years < 0:
|
||||
# target date is in the future
|
||||
end()
|
||||
return
|
||||
|
||||
if (el_months == 0) and (el_days == 0):
|
||||
print("***HAPPY BIRTHDAY***")
|
||||
|
||||
# print report
|
||||
print_with_tab(23, "\tYEARS\tMONTHS\tDAYS")
|
||||
print_with_tab(23, "\t-----\t------\t----")
|
||||
print(f"YOUR AGE (IF BIRTHDATE)\t{el_years}\t{el_months}\t{el_days}")
|
||||
|
||||
life_days = (el_years * 365) + (el_months * 30) + el_days + int(el_months / 2)
|
||||
rem_years = el_years
|
||||
rem_months = el_months
|
||||
rem_days = el_days
|
||||
|
||||
rem_years, rem_months, rem_days, used_years, used_months, used_days = deduct_time(
|
||||
0.35, life_days, rem_years, rem_months, rem_days
|
||||
)
|
||||
time_report("YOU HAVE SLEPT", used_years, used_months, used_days)
|
||||
rem_years, rem_months, rem_days, used_years, used_months, used_days = deduct_time(
|
||||
0.17, life_days, rem_years, rem_months, rem_days
|
||||
)
|
||||
time_report("YOU HAVE EATEN", used_years, used_months, used_days)
|
||||
|
||||
label = make_occupation_label(rem_years)
|
||||
rem_years, rem_months, rem_days, used_years, used_months, used_days = deduct_time(
|
||||
0.23, life_days, rem_years, rem_months, rem_days
|
||||
)
|
||||
time_report("YOU HAVE " + label, used_years, used_months, used_days)
|
||||
time_report("YOU HAVE RELAXED", rem_years, rem_months, rem_days)
|
||||
|
||||
print()
|
||||
|
||||
# Calculate retirement date
|
||||
e = year + 65
|
||||
print_with_tab(16, f"*** YOU MAY RETIRE IN {e} ***")
|
||||
end()
|
||||
|
||||
|
||||
def test_weekday_calc(year, month, day):
|
||||
dt = datetime.date(year, month, day)
|
||||
python_weekday = dt.weekday() # Monday = 0, Sunday = 6
|
||||
|
||||
basic_weekday = calculate_day_of_week(year, month, day) # Sunday = 1, Saturday = 7
|
||||
|
||||
test = ((python_weekday + 2) % 7) == (basic_weekday % 7)
|
||||
if test == False:
|
||||
print(f"testing yr {year} month {month} day {day}")
|
||||
print(f"python says {python_weekday}")
|
||||
print(f"BASIC says {basic_weekday}")
|
||||
assert False
|
||||
|
||||
|
||||
def test_harness():
|
||||
for yr in range(1600, 2021):
|
||||
for m in range(1, 12):
|
||||
for d in range(1, 28):
|
||||
test_weekday_calc(yr, m, d)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
# test_harness()
|
||||
3
95_Weekday/ruby/README.md
Normal file
3
95_Weekday/ruby/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Ruby](https://www.ruby-lang.org/en/)
|
||||
3
95_Weekday/vbnet/README.md
Normal file
3
95_Weekday/vbnet/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original BASIC source [downloaded from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Visual Basic .NET](https://en.wikipedia.org/wiki/Visual_Basic_.NET)
|
||||
150
95_Weekday/weekday.bas
Normal file
150
95_Weekday/weekday.bas
Normal file
@@ -0,0 +1,150 @@
|
||||
10 PRINT TAB(32);"WEEKDAY"
|
||||
20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
30 PRINT:PRINT:PRINT
|
||||
100 PRINT "WEEKDAY IS A COMPUTER DEMONSTRATION THAT"
|
||||
110 PRINT"GIVES FACTS ABOUT A DATE OF INTEREST TO YOU."
|
||||
120 PRINT
|
||||
130 PRINT "ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ";
|
||||
140 INPUT M1,D1,Y1
|
||||
150 REM THIS PROGRAM DETERMINES THE DAY OF THE WEEK
|
||||
160 REM FOR A DATE AFTER 1582
|
||||
170 DEF FNA(A)=INT(A/4)
|
||||
180 DIM T(12)
|
||||
190 DEF FNB(A)=INT(A/7)
|
||||
200 REM SPACE OUTPUT AND READ IN INITIAL VALUES FOR MONTHS.
|
||||
210 FOR I= 1 TO 12
|
||||
220 READ T(I)
|
||||
230 NEXT I
|
||||
240 PRINT"ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST)";
|
||||
250 INPUT M,D,Y
|
||||
260 PRINT
|
||||
270 LET I1 = INT((Y-1500)/100)
|
||||
280 REM TEST FOR DATE BEFORE CURRENT CALENDAR.
|
||||
290 IF Y-1582 <0 THEN 1300
|
||||
300 LET A = I1*5+(I1+3)/4
|
||||
310 LET I2=INT(A-FNB(A)*7)
|
||||
320 LET Y2=INT(Y/100)
|
||||
330 LET Y3 =INT(Y-Y2*100)
|
||||
340 LET A =Y3/4+Y3+D+T(M)+I2
|
||||
350 LET B=INT(A-FNB(A)*7)+1
|
||||
360 IF M > 2 THEN 470
|
||||
370 IF Y3 = 0 THEN 440
|
||||
380 LET T1=INT(Y-FNA(Y)*4)
|
||||
390 IF T1 <> 0 THEN 470
|
||||
400 IF B<>0 THEN 420
|
||||
410 LET B=6
|
||||
420 LET B = B-1
|
||||
430 GOTO 470
|
||||
440 LET A = I1-1
|
||||
450 LET T1=INT(A-FNA(A)*4)
|
||||
460 IF T1 = 0 THEN 400
|
||||
470 IF B <>0 THEN 490
|
||||
480 LET B = 7
|
||||
490 IF (Y1*12+M1)*31+D1<(Y*12+M)*31+D THEN 550
|
||||
500 IF (Y1*12+M1)*31+D1=(Y*12+M)*31+D THEN 530
|
||||
510 PRINT M;"/";D;"/";Y;" WAS A ";
|
||||
520 GOTO 570
|
||||
530 PRINT M;"/";D;"/";Y;" IS A ";
|
||||
540 GOTO 570
|
||||
550 PRINT M;"/";D;"/";Y;" WILL BE A ";
|
||||
560 REM PRINT THE DAY OF THE WEEK THE DATE FALLS ON.
|
||||
570 IF B <>1 THEN 590
|
||||
580 PRINT "SUNDAY."
|
||||
590 IF B<>2 THEN 610
|
||||
600 PRINT "MONDAY."
|
||||
610 IF B<>3 THEN 630
|
||||
620 PRINT "TUESDAY."
|
||||
630 IF B<>4 THEN 650
|
||||
640 PRINT "WEDNESDAY."
|
||||
650 IF B<>5 THEN 670
|
||||
660 PRINT "THURSDAY."
|
||||
670 IF B<>6 THEN 690
|
||||
680 GOTO 1250
|
||||
690 IF B<>7 THEN 710
|
||||
700 PRINT "SATURDAY."
|
||||
710 IF (Y1*12+M1)*31+D1=(Y*12+M)*31+D THEN 1120
|
||||
720 LET I5=Y1-Y
|
||||
730 PRINT
|
||||
740 LET I6=M1-M
|
||||
750 LET I7=D1-D
|
||||
760 IF I7>=0 THEN 790
|
||||
770 LET I6= I6-1
|
||||
780 LET I7=I7+30
|
||||
790 IF I6>=0 THEN 820
|
||||
800 LET I5=I5-1
|
||||
810 LET I6=I6+12
|
||||
820 IF I5<0 THEN 1310
|
||||
830 IF I7 <> 0 THEN 850
|
||||
835 IF I6 <> 0 THEN 850
|
||||
840 PRINT"***HAPPY BIRTHDAY***"
|
||||
850 PRINT " "," ","YEARS","MONTHS","DAYS"
|
||||
855 PRINT " "," ","-----","------","----"
|
||||
860 PRINT "YOUR AGE (IF BIRTHDATE) ",I5,I6,I7
|
||||
870 LET A8 = (I5*365)+(I6*30)+I7+INT(I6/2)
|
||||
880 LET K5 = I5
|
||||
890 LET K6 = I6
|
||||
900 LET K7 = I7
|
||||
910 REM CALCULATE RETIREMENT DATE.
|
||||
920 LET E = Y+65
|
||||
930 REM CALCULATE TIME SPENT IN THE FOLLOWING FUNCTIONS.
|
||||
940 LET F = .35
|
||||
950 PRINT "YOU HAVE SLEPT ",
|
||||
960 GOSUB 1370
|
||||
970 LET F = .17
|
||||
980 PRINT "YOU HAVE EATEN ",
|
||||
990 GOSUB 1370
|
||||
1000 LET F = .23
|
||||
1010 IF K5 > 3 THEN 1040
|
||||
1020 PRINT "YOU HAVE PLAYED",
|
||||
1030 GOTO 1080
|
||||
1040 IF K5 > 9 THEN 1070
|
||||
1050 PRINT "YOU HAVE PLAYED/STUDIED",
|
||||
1060 GOTO 1080
|
||||
1070 PRINT "YOU HAVE WORKED/PLAYED",
|
||||
1080 GOSUB 1370
|
||||
1085 GOTO 1530
|
||||
1090 PRINT "YOU HAVE RELAXED ",K5,K6,K7
|
||||
1100 PRINT
|
||||
1110 PRINT TAB(16);"*** YOU MAY RETIRE IN";E;" ***"
|
||||
1120 PRINT
|
||||
1140 PRINT
|
||||
1200 PRINT
|
||||
1210 PRINT
|
||||
1220 PRINT
|
||||
1230 PRINT
|
||||
1240 END
|
||||
1250 IF D=13 THEN 1280
|
||||
1260 PRINT "FRIDAY."
|
||||
1270 GOTO 710
|
||||
1280 PRINT "FRIDAY THE THIRTEENTH---BEWARE!"
|
||||
1290 GOTO 710
|
||||
1300 PRINT "NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII. "
|
||||
1310 GOTO 1140
|
||||
1320 REM TABLE OF VALUES FOR THE MONTHS TO BE USED IN CALCULATIONS.
|
||||
1330 DATA 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5
|
||||
1340 REM THIS IS THE CURRENT DATE USED IN THE CALCULATIONS.
|
||||
1350 REM THIS IS THE DATE TO BE CALCULATED ON.
|
||||
1360 REM CALCULATE TIME IN YEARS, MONTHS, AND DAYS
|
||||
1370 LET K1=INT(F*A8)
|
||||
1380 LET I5 = INT(K1/365)
|
||||
1390 LET K1 = K1- (I5*365)
|
||||
1400 LET I6 = INT(K1/30)
|
||||
1410 LET I7 = K1 -(I6*30)
|
||||
1420 LET K5 = K5-I5
|
||||
1430 LET K6 =K6-I6
|
||||
1440 LET K7 = K7-I7
|
||||
1450 IF K7>=0 THEN 1480
|
||||
1460 LET K7=K7+30
|
||||
1470 LET K6=K6-1
|
||||
1480 IF K6>0 THEN 1510
|
||||
1490 LET K6=K6+12
|
||||
1500 LET K5=K5-1
|
||||
1510 PRINT I5,I6,I7
|
||||
1520 RETURN
|
||||
1530 IF K6=12 THEN 1550
|
||||
1540 GOTO 1090
|
||||
1550 LET K5=K5+1
|
||||
1560 LET K6=0
|
||||
1570 GOTO 1090
|
||||
1580 REM
|
||||
1590 END
|
||||
Reference in New Issue
Block a user