home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS - Coast to Coast
/
simteldosarchivecoasttocoast2.iso
/
calculat
/
hrs411.zip
/
HRS411.C
next >
Wrap
C/C++ Source or Header
|
1991-07-24
|
3KB
|
115 lines
/* hrs411.c
This program calculates the time difference in fractions of hours
between ending and starting time. Enter the Time as HH:MM. However
entering 2 for 2:00 (for example) is OK. The program
will allow for entries such as ending 2:30 PM and 10:45 AM starting
time. If the time span is greater than 12 hours the time must be
entered as Military time, 8:00 PM = 20:00 for example. */
#include<stdio.h>
main()
{
char time1[7],time2[7],end[] = "ending",start[] = "starting";
int error,set;
float hr1,hr2,min1,min2,hrt,total=0;
clrscr();
set = 0;
printf("Doug Terry Ver 4.11 7/24/91 Copyright 1991 All rights Reserved \n\n");
while(1)
{
printf("\nSet %d",++set);
input(time2,end,&total,&set); /* allows for input and error check of times */
input(time1,start,&total,&set); /* & repeating input till correct format. */
convert(time2, &hr2, &min2); /* string to integers */
convert(time1, &hr1, &min1);
if(min2 < min1) /* if ending min is less than begginning subtracts
{ an hour from hr and adds 60 min to min. /*
hr2 = hr2-1;
min2 = min2 + 60;
}
if(hr2 < hr1) /* if ending time is less than beginning adds 12 */
hr2 = hr2 + 12;
hrt = hr2-hr1 + ((min2-min1)/60);
total += hrt;
printf("\nElapsed time is : %.3f, running total is %.3f\n",hrt, total);
printf("\n=========================================================\n");
}
}
/* Note that either ':' or ';' is allowed in the time format in case fumble
fingers here misses the shift key. */
/* This function checks the string for errors such as too long a string
or a wrong character such as 3L45 for 3:45 */
check(time)
char time[7];
{
int i;
if (strlen(time) > 6 )
{
printf("\Format entry error in strlen(), please reenter.\n");
return(0);
}
for (i = 0; time[i]; i++)
{
if(time[i] < '0' || time[i] > '9' && time[i] != ':' && time[i] != ';')
return(0);
else;
}
return(1);
}
/* This function converts the string characters '0' thru '9' to values. */
convert(time, hrs, min)
char time[7];
float *hrs, *min;
{
int i, flag;
*hrs = 0;
*min = 0;
flag = 0;
for(i = 0; time[i]; i++)
{
if(time[i] == ':'|| time[i] == ';')
flag = 1;
if(flag == 0)
*hrs = (*hrs * 10) + (time[i] - '0');
else if (flag == 1 && time[i] != ':'&& time[i] != ';')
*min = (*min * 10) + (time[i] - '0');
}
return(0);
}
/* This function allows for input of the data and if the function check()
detects any errors it allows repetition of the function till the time
is entered correctly. */
input(time,position,total,set)
char time[7],position[8];
float *total;
int *set;
{
do
{
printf("\nEnter %s time in form of HH:MM or 'C' to clear the running total\n"
"or 'Q' to quit the program.\n",position);
gets(time);
if(time[0] == 'C' || time[0] == 'c') /* clears to total & resets set counter. */
{
*total = 0;
*set = 1;
clrscr();
printf("Set %d\n",*set);
}
if(time[0] == 'Q' || time[0] == 'q')
exit();
check(time);
if(check(time) == 0 && time[0] != 'c' && time[0] != 'C')
printf("\nFormat error for %s time, please reenter.",position);
}
while(check(time) == 0);
return(0);
}