elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  ayuda C
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: ayuda C  (Leído 1,364 veces)
J.cE

Desconectado Desconectado

Mensajes: 13


Ver Perfil
ayuda C
« en: 21 Febrero 2014, 17:44 pm »

tengo una duda, hay alguna funcion que me convierta una fecha ingresada del tipo 12/12/2012 en  dias transcurridos desde enero ejm: 365 ?


En línea

rir3760


Desconectado Desconectado

Mensajes: 1.639


Ver Perfil
Re: ayuda C
« Respuesta #1 en: 21 Febrero 2014, 18:12 pm »

Puedes utilizar un objeto de tipo "struct tm" (definición en <time.h>) asignando manualmente los valores para el día, mes y año. A continuación ajustas los campos mediante la función mktime (prototipo en el mismo encabezado). Después de eso basta con utilizar el campo "tm_yday" de la estructura.

Un saludo


En línea

C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.
--
Kernighan & Ritchie, The C programming language
J.cE

Desconectado Desconectado

Mensajes: 13


Ver Perfil
Re: ayuda C
« Respuesta #2 en: 21 Febrero 2014, 20:04 pm »

hajajaja, bueno, mas o menos entendi, no he usado mucho la libreria time.h pero tengo esto:

Código:
typedef struct
{
    int tm_sec;   
    int tm_min;     
    int tm_hour;     
    int tm_mday;   
    int tm_mon;     
    int tm_year;     
    int tm_wday;     
    int tm_yday;     
    int tm_isdst;   
}tm;

luego se supene que ingrese los datos manualemnte algo asi?

int main()
{
    tm *fecha;
   
    printf("ingrese dia\n");
    scanf("%d",&fecha->tm_mday);
    printf("ingrese mes");
    scanf("%d",&fecha->tm_mon);
    printf("ingrese año");
    scanf("%d",&fecha->tm_year);
}

y luego que?
no se, estoy perdido XD
En línea

rir3760


Desconectado Desconectado

Mensajes: 1.639


Ver Perfil
Re: ayuda C
« Respuesta #3 en: 22 Febrero 2014, 01:44 am »

no he usado mucho la libreria time.h pero tengo esto:

Código:
typedef struct
{
    int tm_sec;   
    int tm_min;     
    int tm_hour;     
    int tm_mday;   
    int tm_mon;     
    int tm_year;     
    int tm_wday;     
    int tm_yday;     
    int tm_isdst;   
}tm;
No lo uses, en su lugar incluyes <time.h> y utilizas el tipo "struct tm". El porque se indica en la referencia:
Citar
struct tm contains members that describe various properties of the calendar time. The members shown above can occur in any order, interspersed with additional members.

y luego que?
Llamas a la funcion mktime e imprimes el valor del campo "tm_yday". Por ejemplo:
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main(void)
  6. {
  7.   struct tm d = {0};
  8.  
  9.   puts("Introduce la fecha (aaaa mm dd):");
  10.   if (scanf("%d %d %d", &d.tm_year, &d.tm_mon, &d.tm_mday) != 3)
  11.      return EXIT_FAILURE;
  12.   d.tm_year -= 1900; /* 0 == 1900, 100 == 2000, etc. */
  13.   d.tm_mon--; /* 0 == Enero, 1 == Febrero, etc. */
  14.  
  15.   if (mktime(&d) == (time_t) -1){
  16.      puts("Fecha no valida");
  17.      return EXIT_FAILURE;
  18.   }
  19.   printf("%4d/%02d/%02d ==> Dias transcurridos: %d\n",
  20.      d.tm_year + 1900, d.tm_mon + 1, d.tm_mday, d.tm_yday);
  21.  
  22.   return EXIT_SUCCESS;
  23. }

Un saludo
En línea

C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.
--
Kernighan & Ritchie, The C programming language
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines