home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / samples / sam01l / sample01.c__ / SAMPLE01.C
Encoding:
C/C++ Source or Header  |  1992-04-01  |  5.1 KB  |  116 lines

  1. /******************************************************************************/
  2. /* SAMPLE PROGRAM 01: SAMPLE01.C                                              */
  3. /*                                                                            */
  4. /* COPYRIGHT:                                                                 */
  5. /* ----------                                                                 */
  6. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  7. /*                                                                            */
  8. /* DISCLAIMER OF WARRANTIES:                                                  */
  9. /* -------------------------                                                  */
  10. /* The following [enclosed] code is sample code created by IBM                */
  11. /* Corporation.  This sample code is not part of any standard IBM product     */
  12. /* and is provided to you solely for the purpose of assisting you in the      */
  13. /* development of your applications.  The code is provided "AS IS",           */
  14. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  15. /* arising out of your use of the sample code, even if they have been         */
  16. /* advised of the possibility of such damages.                                */
  17. /*                                                                            */
  18. /******************************************************************************/
  19.  
  20. /* Sample Program 01: Biorhythm                          */
  21. /* Description: Calculates biorhythm based on current    */
  22. /*               system date and birth date entered.     */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <time.h>
  28.  
  29. #include "SAMPLE01.H"   /* Constant TWOPI and
  30.                            array mon_tot are defined here */
  31.  
  32. #define P_CYCLE 23.0    /* Physical cycle = 23 days     */
  33. #define E_CYCLE 28.0    /* Emotional cycle = 28 days    */
  34. #define I_CYCLE 33.0    /* Intellectual cycle = 33 days */
  35.  
  36. int t_days(int year, int day, char birth[]);
  37.  
  38. int main( void )
  39. {
  40.    double      physical, emotional, intellectual;
  41.    int         tot_days, year, day;
  42.    char        birth[11];
  43.    time_t      ltime;
  44.    struct tm   *newtime;
  45.  
  46.    printf("Enter your birthday in the form yyyy/mm/dd.\n");
  47.    fgets(birth,sizeof(birth),stdin);
  48.    printf(birth);
  49.  
  50.    time(<ime);                           /* Get current date         */
  51.    newtime = gmtime(<ime);               /* Convert date             */
  52.    year = newtime->tm_year + 1900;         /* Get current year         */
  53.    day = newtime->tm_yday + 1;             /* Get current day of year  */
  54.  
  55.    tot_days = t_days(year, day, birth);
  56.  
  57.    physical     = sin(fmod((double) tot_days , P_CYCLE) / P_CYCLE * TWOPI);
  58.    emotional    = sin(fmod((double) tot_days , E_CYCLE) / E_CYCLE * TWOPI);
  59.    intellectual = sin(fmod((double) tot_days , I_CYCLE) / I_CYCLE * TWOPI);
  60.  
  61.    printf("\n");
  62.    printf("Total days:     %d    \n", tot_days);
  63.    printf("Physical:       %5.2f \n", physical);
  64.    printf("Emotional:      %5.2f \n", emotional);
  65.    printf("Intellectual:   %5.2f \n", intellectual);
  66.  
  67.    return 0;
  68. }
  69.  
  70. /* Function:  t_days                                       */
  71. /* Purpose:   Calculates the number of days from birth.    */
  72. /* Receives:  Current year, current day of year, and       */
  73. /*            array containing date of birth.              */
  74. /* Returns:   Integer number of days.                      */
  75. /* Method:    Subtract birth date from current date.       */
  76.  
  77. int t_days(int year, int day, char birth[])
  78. {
  79.    int  start,
  80.         num_days,
  81.         birth_year,
  82.         birth_month,
  83.         birth_day;
  84.    char z;
  85.  
  86.          /* Separate birth date into year, month, and day */
  87.    sscanf(birth,"%d %c %d %c %d", &birth_year, &z,
  88.                                   &birth_month, &z,
  89.                                   &birth_day);
  90.  
  91.                                   /* Calculate total days */
  92.    num_days = (year - birth_year) * 365 + day
  93.               - mon_tot[birth_month-1] - birth_day;
  94.  
  95.    /* Adjust for leap year:                                           */
  96.    /*                                                                 */
  97.    /* If birth_year is a leap year, and birth_month is January or     */
  98.    /* February, add one to num_days.                                  */
  99.    /*                                                                 */
  100.    /* Day of current year is a Julian day, so no leap year            */
  101.    /* adjustment is required.                                         */
  102.    /*                                                                 */
  103.    /* Add one to num_days for all leap years between birth_year       */
  104.    /* and current year.                                               */
  105.  
  106.    if (((birth_year % 4 == 0 && birth_year % 100 != 0) ||
  107.         (birth_year % 400 == 0)) && birth_month <= 2)
  108.       ++num_days;
  109.  
  110.    for (start = birth_year + 1; start < year; ++start)
  111.       if ((start % 4 == 0 && start % 100 != 0) || start % 400 ==0)
  112.          ++num_days;
  113.  
  114.    return(num_days);
  115. }
  116.