jueves, 4 de marzo de 2010

d2hms.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double d2hms(const double d, double *h, double *m, double *s)
{ double foo;
foo=modf(d,h);
foo*=60;
foo=modf(foo,m);
foo*=60;
foo=modf(foo,s);
return foo;
}

int main(int argc, char* argv[], char* envp[])
{
double d,h,m,s,c;

if(argc==2) {
d=atof(argv[1]); /* better use strtod */
} else {
printf("Usage:\n%s hora.decimal\n",argv[0]);
return -1;
}

c=d2hms(d,&h,&m,&s);
printf("%f=%.0f:%02.0f:%06.3f\n", d, h, m, s+c);
return 0;
}

---
$ gcc d2hms.c -lm
$ ./a.out 12.345678
12.345678=12:20:44.441

No hay comentarios.: