home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / calculat / hrs411.zip / HRS411.C next >
C/C++ Source or Header  |  1991-07-24  |  3KB  |  115 lines

  1. /* hrs411.c
  2.     This program calculates the time difference in fractions of hours
  3.     between ending and starting time.  Enter the Time as HH:MM. However
  4.     entering 2 for 2:00 (for example) is OK. The program
  5.     will allow for entries such as ending 2:30 PM and 10:45 AM starting
  6.     time.  If the time span is greater than 12 hours the time must be
  7.     entered as Military time, 8:00 PM = 20:00 for example. */
  8.  
  9. #include<stdio.h>
  10. main()
  11. {
  12. char time1[7],time2[7],end[] = "ending",start[] = "starting";
  13. int error,set;
  14. float hr1,hr2,min1,min2,hrt,total=0;
  15. clrscr();
  16. set = 0;
  17. printf("Doug Terry  Ver 4.11  7/24/91  Copyright 1991 All rights Reserved \n\n");
  18. while(1)
  19.     {
  20.     printf("\nSet %d",++set);
  21.     input(time2,end,&total,&set); /* allows for input and error check of times */
  22.     input(time1,start,&total,&set); /* & repeating input till correct format. */
  23.     convert(time2, &hr2, &min2);   /* string to integers */
  24.     convert(time1, &hr1, &min1);
  25.     if(min2 < min1)          /* if ending min is less than begginning subtracts
  26.         {                        an hour from hr and adds 60 min to min. /*
  27.         hr2 = hr2-1;
  28.         min2 = min2 + 60;
  29.         }
  30.         if(hr2 < hr1)          /* if ending time is less than beginning adds 12 */
  31.             hr2 = hr2 + 12;
  32.         hrt = hr2-hr1 + ((min2-min1)/60);
  33.         total += hrt;
  34.         printf("\nElapsed time is : %.3f, running total is %.3f\n",hrt, total);
  35.         printf("\n=========================================================\n");
  36.     }
  37. }
  38.  
  39. /* Note that either ':' or ';' is allowed in the time format in case fumble
  40.     fingers here misses the shift key.  */
  41.  
  42. /*  This function checks the string for errors such as too long a string
  43.      or a wrong character such as 3L45 for 3:45 */
  44.  
  45. check(time)
  46. char time[7];
  47. {
  48. int i;
  49. if (strlen(time) > 6 )
  50.     {
  51.     printf("\Format entry error in strlen(), please reenter.\n");
  52.     return(0);
  53.     }
  54. for (i = 0; time[i]; i++)
  55.     {
  56.     if(time[i] < '0' || time[i] > '9' && time[i] != ':' && time[i] != ';')
  57.         return(0);
  58.     else;
  59.     }
  60.     return(1);
  61. }
  62.  
  63. /* This function converts the string characters '0' thru '9' to values. */
  64.  
  65. convert(time, hrs, min)
  66. char time[7];
  67. float *hrs, *min;
  68. {
  69. int i, flag;
  70. *hrs = 0;
  71. *min = 0;
  72. flag = 0;
  73. for(i = 0; time[i]; i++)
  74.     {
  75.     if(time[i] == ':'|| time[i] == ';')
  76.         flag = 1;
  77.     if(flag == 0)
  78.         *hrs = (*hrs * 10) + (time[i] - '0');
  79.     else if (flag == 1 && time[i] != ':'&& time[i] != ';')
  80.         *min = (*min * 10) + (time[i] - '0');
  81.     }
  82.     return(0);
  83. }
  84.  
  85.  
  86. /* This function allows for input of the data and if the function check()
  87.     detects any errors it allows repetition of the function till the time
  88.     is entered correctly.  */
  89.  
  90. input(time,position,total,set)
  91. char time[7],position[8];
  92. float *total;
  93. int *set;
  94. {
  95. do
  96.     {
  97.     printf("\nEnter %s time in form of HH:MM or 'C' to clear the running total\n"
  98.     "or 'Q' to quit the program.\n",position);
  99.     gets(time);
  100.     if(time[0] == 'C' || time[0] == 'c') /* clears to total & resets set counter. */
  101.         {
  102.         *total = 0;
  103.         *set  = 1;
  104.         clrscr();
  105.         printf("Set %d\n",*set);
  106.         }
  107.     if(time[0] == 'Q' || time[0] == 'q')
  108.         exit();
  109.     check(time);
  110.     if(check(time) == 0 && time[0] != 'c' && time[0] != 'C')
  111.         printf("\nFormat error for %s time, please reenter.",position);
  112.     }
  113. while(check(time) == 0);
  114. return(0);
  115. }