home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************/
- /* SAMPLE PROGRAM 01: SAMPLE01.C */
- /* */
- /* COPYRIGHT: */
- /* ---------- */
- /* Copyright (C) International Business Machines Corp., 1991,1992. */
- /* */
- /* DISCLAIMER OF WARRANTIES: */
- /* ------------------------- */
- /* The following [enclosed] code is sample code created by IBM */
- /* Corporation. This sample code is not part of any standard IBM product */
- /* and is provided to you solely for the purpose of assisting you in the */
- /* development of your applications. The code is provided "AS IS", */
- /* without warranty of any kind. IBM shall not be liable for any damages */
- /* arising out of your use of the sample code, even if they have been */
- /* advised of the possibility of such damages. */
- /* */
- /******************************************************************************/
-
- /* Sample Program 01: Biorhythm */
- /* Description: Calculates biorhythm based on current */
- /* system date and birth date entered. */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
-
- #include "SAMPLE01.H" /* Constant TWOPI and
- array mon_tot are defined here */
-
- #define P_CYCLE 23.0 /* Physical cycle = 23 days */
- #define E_CYCLE 28.0 /* Emotional cycle = 28 days */
- #define I_CYCLE 33.0 /* Intellectual cycle = 33 days */
-
- int t_days(int year, int day, char birth[]);
-
- int main( void )
- {
- double physical, emotional, intellectual;
- int tot_days, year, day;
- char birth[11];
- time_t ltime;
- struct tm *newtime;
-
- printf("Enter your birthday in the form yyyy/mm/dd.\n");
- fgets(birth,sizeof(birth),stdin);
- printf(birth);
-
- time(<ime); /* Get current date */
- newtime = gmtime(<ime); /* Convert date */
- year = newtime->tm_year + 1900; /* Get current year */
- day = newtime->tm_yday + 1; /* Get current day of year */
-
- tot_days = t_days(year, day, birth);
-
- physical = sin(fmod((double) tot_days , P_CYCLE) / P_CYCLE * TWOPI);
- emotional = sin(fmod((double) tot_days , E_CYCLE) / E_CYCLE * TWOPI);
- intellectual = sin(fmod((double) tot_days , I_CYCLE) / I_CYCLE * TWOPI);
-
- printf("\n");
- printf("Total days: %d \n", tot_days);
- printf("Physical: %5.2f \n", physical);
- printf("Emotional: %5.2f \n", emotional);
- printf("Intellectual: %5.2f \n", intellectual);
-
- return 0;
- }
-
- /* Function: t_days */
- /* Purpose: Calculates the number of days from birth. */
- /* Receives: Current year, current day of year, and */
- /* array containing date of birth. */
- /* Returns: Integer number of days. */
- /* Method: Subtract birth date from current date. */
-
- int t_days(int year, int day, char birth[])
- {
- int start,
- num_days,
- birth_year,
- birth_month,
- birth_day;
- char z;
-
- /* Separate birth date into year, month, and day */
- sscanf(birth,"%d %c %d %c %d", &birth_year, &z,
- &birth_month, &z,
- &birth_day);
-
- /* Calculate total days */
- num_days = (year - birth_year) * 365 + day
- - mon_tot[birth_month-1] - birth_day;
-
- /* Adjust for leap year: */
- /* */
- /* If birth_year is a leap year, and birth_month is January or */
- /* February, add one to num_days. */
- /* */
- /* Day of current year is a Julian day, so no leap year */
- /* adjustment is required. */
- /* */
- /* Add one to num_days for all leap years between birth_year */
- /* and current year. */
-
- if (((birth_year % 4 == 0 && birth_year % 100 != 0) ||
- (birth_year % 400 == 0)) && birth_month <= 2)
- ++num_days;
-
- for (start = birth_year + 1; start < year; ++start)
- if ((start % 4 == 0 && start % 100 != 0) || start % 400 ==0)
- ++num_days;
-
- return(num_days);
- }