Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Faqs and RFCs
FileSearching
Google
Google Groups
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
biorhythm.c
/* BIORHYTH.C by Michael J. Chappell June 29, 2015 A fairly direct port of BIORHYTHM.BAS Copyright 1979 by Phil Feldman and Tom Rugg. The code for dealing with the dates wasn't working as expected. So I rewrote date2Julian and julian2Date. I rewrote those sections based on julian Day Calculations (Gregorian Calendar) at http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html . See source for notes. */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include "biorhythm.h" #include "version.h" int l; /* counter of number of lines on screen */ int k; /* loop and work variable */ float t; /* num of chars on one side of the center of the chart */ float n; /* number of days between birth and current chart date */ float v; /* number of days in current biorhythm cycle (23, 28 or 33) */ float m; /* month */ float d; /* day */ float y; /* year (100 or greater) */ char c$[80]; /* string with date in month/day/year format */ char line[80]; /* string with one line of the biorhythm chart */ char reply; /* reply from operator after screen fills up */ char a$[80]; /* work variable */ float p; /* pi */ float jb; /* birth date in julian format */ float jd; /* julian date calculated in subroutine */ float jc; /* chart start date in julian format */ float w, b; /* work variables */ float r; /* remain of n/v (number of days into cycle */ char temp[255]; int main(int argc, char **argv) { time_t seconds; struct tm tm; int i; int NOW = 0; /* if not 0, use today as start date */ int DOBSET = 0; /* if not 0, the argument index for the date */ int STARTSET = 0; /* if not 0, the argument index for the date */ int NOPAGE = 0; /* if not 0, exit after 1 page */ int numberoflines = 0; char dobstring[128]; if (argc>1) { for (i=1; i<argc; i++) { if (argv[i][0]=='-') { switch(argv[i][1]) { case 'b': DOBSET = ++i; if (i==argc) { puts("Must include date with \'-b\'\n"); return EXIT_FAILURE; } break; case 'd': STARTSET = ++i; if (i==argc) { puts("Must include date with \'-d\'\n"); return EXIT_FAILURE; } break; case 'l': ++i; if (i==argc) { puts("Must indicate number of lines with \'-l\'\n"); return EXIT_FAILURE; } numberoflines = atoi(argv[i]); if (numberoflines<1) { puts("Number of lines must be greater than 0\n"); return EXIT_FAILURE; } break; case 'n': NOW = 1; break; case 'h': help(); return EXIT_SUCCESS; break; case 'q': NOPAGE = 1; break; case 'v': version(); return EXIT_SUCCESS; break; default: help(); return EXIT_FAILURE; break; } } else { /* if only arg is date, assume it's dob and start at today */ NOW = 1; DOBSET = i; } } } Initialize(); l=0; t=25; p=3.1415926535; if (!numberoflines) numberoflines = NUMOFLINES; Cls(); fprintf(stdout, " "); fprintf(stdout, "%s","BIORHYTHM\n"); fprintf(stdout, "%s","\n"); for (;;) { if (DOBSET) { if (getDateFromArg(argv[DOBSET])!=0) { printf("\n\'%s\' is not a valid date.\n", argv[DOBSET]); return EXIT_FAILURE; } } else { fprintf(stdout, "%s","\nEnter birth date"); getDate(); } sprintf(dobstring, "%02d/%02d/%04d", (int)m, (int)d, (int)y); date2Julian(); jb = jd; if (STARTSET) { if (getDateFromArg(argv[STARTSET])!=0) { printf("\n\'%s\' is not a valid date.\n", argv[STARTSET]); return EXIT_FAILURE; } } else { if (!NOW) { fprintf(stdout, "%s","\nEnter start date for chart"); getDate(); } else { seconds = time(NULL); tm = *localtime(&seconds); y = (float)tm.tm_year+1900.0; m = (float)tm.tm_mon+1.0; d = (float)tm.tm_mday; } } date2Julian(); jc = jd; if ((jc<jb) && DOBSET && (NOW || STARTSET)) { fprintf(stdout, "%s","\nChart date can't be earlier than"); fprintf(stdout, "%s","\nbirth date.\n"); return EXIT_FAILURE; } if (jc<jb) { fprintf(stdout, "%s","\nChart date can't be earlier than"); fprintf(stdout, "%s","\nbirth date. Try again."); continue; } else break; } do { header(dobstring); do { n = jc-jb; v = 23; remain(); plot(); v = 28; remain(); plot(); v = 33; remain(); plot(); julian2Date(); sprintf(temp,"%s%s\n", c$, line); fprintf(stdout, "%s",temp); jc = jc+1; l++; } while (l<numberoflines); if (NOPAGE) break; l = 0; fprintf(stdout, "%s","Press 'Q' to end, ENTER to continue "); do { reply=getchar(); } while (!reply); } while (reply!='Q' && reply!='q'); if (!NOPAGE) while (!getchar()) ; /* strip trailing chars and '\n' */ return EXIT_SUCCESS; } /* main */ void Initialize(void) { /* InitializeTRS80(0);*/ return; } /* Initialize */ void getDate(void) { /* sets m, d, and y (month, day, and year) */ char temp[255]; fputc('\n', stdout); do { fprintf(stdout, "%s","Month (1 to 12)? "); scanf(" %f", &m); } while ((m<1.0) || (m>12.0)); do { fprintf(stdout, "%s","Day (1 to 31)? "); scanf(" %f", &d); } while ((d<1.0) || (m>31.0)); do { fprintf(stdout, "%s","Year? "); scanf(" %f", &y); } while (y<0.0); if (y<100) { y+=1900; sprintf(temp, "\n%4.0f Assumed.", y); fprintf(stdout, "%s",temp); } while (getchar()!='\n') ; } /* getDate */ void remain(void) { /* sets r based on n/v */ w = floor(n/v); r = n-(w*v); } /* remain */ void plot(void) { /* plot a point on line based on v and r */ char c; if (v==23) { for (k=0; k<2*t+1; k++) line[k]=' '; line[k]='\0'; line[(int)t-1]='|'; } if (v==23) c = 'P'; else if (v==28) c = 'E'; else c = 'I'; /* v==33 */ w = 2*p*(r/v); w = t*sin(w)+t+0.5; w = floor(w); if (line[(int)w]=='P' || line[(int)w]=='E' || line[(int)w]=='*') c='*'; line[(int)w] = c; } /* plot */ void header(char *dobstring) { /* clear screen and display headings */ Cls(); fprintf(stdout, "%s D.o.B. %s\n", " BIORHYTHM", dobstring); fprintf(stdout, "%s", "---DATE--- D O W N 0 U P\n"); fprintf(stdout, " "); for (k=0; k<2*t+1; k++) fputc('=', stdout); fprintf(stdout, "%s","\n"); } /* header */ void date2Julian(void) { /* sets jd based on m, d, y */ /* direct translation (hopefully) w = floor((m-14)/12); jd = floor(1461*(y+4800+w)/4); b = floor(367*(m-2-w*12)/12); jd = jd+b; b = floor(floor(3*(y+4900+w))/4); jd = jd+d-32075-b; */ /* replacement with trusted code 1) Express the date as Y M D, where Y is the year, M is the month number (Jan = 1, Feb = 2, etc.), and D is the day in the month. 2) If the month is January or February, subtract 1 from the year to get a new Y, and add 12 to the month to get a new M. (Thus, we are thinking of January and February as being the 13th and 14th month of the previous year). 3) Dropping the fractional part of all results of all multiplications and divisions, let A = Y/100 B = A/4 C = 2-A+B E = 365.25x(Y+4716) F = 30.6001x(M+1) JD= C+D+E+F-1524.5 This is the Julian Day Number for the beginning of the date in question at 0 hours, Greenwich time. Note that this always gives you a half day extra. That is because the Julian Day begins at noon, Greenwich time. */ float A, B, C, E, F, JD, Y, M, D; Y=y; M=m; D=d; if (m<3) { M=m+12; Y=y-1; } A=floor(Y/100); B=floor(A/4); C=2-A+B; E=floor(365.25*(Y+4716)); F=floor(30.6001*(M+1)); JD=C+D+E+F-1524.5; jd=JD; } /* date2Julian */ void julian2Date(void) { /* sets c$ (and m, d, y) based on jc */ /* code not working as expected w=jc+68569; r=floor(4*w/146097); w=w-floor((146097*r*3)/4); y=floor(4000*(w+1)/1461001); w=w-floor(1461*y/4)+31; m=floor(80*y/2447); d=w-floor(2447*m/80); w=floor(m/11); m=m+2-12*w; y=100+(r-49)+y+w; */ /* replacement with trusted code To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so that it ends in 0.5). Do the following calculations, again dropping the fractional part of all multiplicatons and divisions. Note: This method will not give dates accurately on the Gregorian Proleptic Calendar, i.e., the calendar you get by extending the Gregorian calendar backwards to years earlier than 1582. using the Gregorian leap year rules. In particular, the method fails if Y<400. Q = JD+0.5 Z = Integer part of Q W = (Z - 1867216.25)/36524.25 X = W/4 A = Z+1+W-X B = A+1524 C = (B-122.1)/365.25 D = 365.25xC E = (B-D)/30.6001 F = 30.6001xE Day of month = B-D-F+(Q-Z) Month = E-1 or E-13 (must get number less than or equal to 12) Year = C-4715 (if Month is January or February) or C-4716 (otherwise) */ float Q, Z, W, X, A, B, C, D, E, F, JD; JD=jc; Q = JD+0.5; Z = floor(Q); W = floor((Z-1867216.25)/36524.25); X = floor(W/4); A = Z+1+W-X; B = A+1524; C = floor((B-122.1)/365.25); D = floor(365.25*C); E = floor((B-D)/30.6001); F = floor(30.6001*E); d = floor(B-D-F+(Q-Z)); m = E-1; y = C-4716; if (m>12) { m = m-12; y = y+1; } sprintf(c$, "%02d/%02d/%04d ", (int)m, (int)d, (int)y); } /* julian2Date */ void version(void) { puts( "biorhythm version " VERSION " Copyright " DATE " Michael J. Chappell\n" ); return; } /* version */ int getDateFromArg(char *datestring) { /* sets global floats m, d, and y based on datestring */ int month, day, year; int invalid = 0; int daysinmonth = 0;; n = sscanf(datestring, "%d/%d/%d", &month, &day, &year); if ((n==0)||(n==EOF)) return 1; m = (float)month; if (month<1) invalid = 1; if (month>12) invalid = 1; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysinmonth = 31; break; case 4: case 6: case 9: case 11: daysinmonth = 30; break; case 2: daysinmonth = 29; break; default: break; } d = (float)day; if (day<1) invalid = 1; if (day>daysinmonth) invalid = 1; if (year<1) invalid = 1; /* formatting relies on 4 digit years and in the unlikely event that the human race makes it to the year 10,000 A.D. I doubt anyone will really care this program won't accept the year. Wondering what your biorhythmic cycle at 10,000 A.D. is rather optimistic. */ if (year>9999) invalid = 1; y = (year>99)?(float)year:(float)year+1900.0; /* printf("\nmonth %f, day %f, year %f\n", m, d, y); */ return invalid; } /* getDateFromArg */
This source has been viewed 18 times.
Friday, 6 December 2024
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org