Calculation of survival in C

Asked

Viewed 77 times

1

Researching about calculating a person’s survival days through the days difference I found a program here that served as the basis for this then added the necessary calculations to know the amount of: hours, minutes, seconds, days, months, years.

Here’s the thing, I have the following date below:

dias entre 29/03/1987 a 21/12/2017
30 anos, 8 meses 18 dias
diaspassados=11225

According to this calculator. So I do the same input in my program and it returns the following output below:

    Digite data1: 29/03/1987

    Digite data2: 21/12/2017

    30 ano(s) 9 mes(es) e 5 dia(s) de vida
    269400 horas
    16164000 minutos
    969840000 segundos
    374 mes(es)
    11225 dia(s)
    1603 semana(s)

Apparently, I believe that the calculation of months has to subtract by the days in excess, for example: in this calculator. Based on the same date as input from the above comment and from the calculator it returns me exact 368 months and my program returns 374.

I know I’d have to do the following calculation:

meses_totais((dias/30)-qtd_dias); 

Here I subtract where it will give me exactly 368 months, but if I type a date equivalent to 1 year and 3 months it gives me trouble in the calculations of days:

For example:

Digite duas datas no formato DD/MM/YYYY

Digite data1: 29/03/1987

Digite data2: 30/06/1988

1 ano(s) 3 mes(es) e 4 dia(s) de vida
11016 horas
660960 minutos
39657600 segundos
15 mes(es)
459 dia(s)
65 semana(s)

But the online calculator returns me: 1 years, 3 months, and 1 days and 459 days. I wanted to understand where he is giving this gap in the error adding up days more or my program is giving 3 days more I wanted to understand why. What is missing in the calculations to be something more precise regarding the hours, minutes, seconds, milliseconds, months, years, days and months due from the date of birthday in the amount of days.

   int dias(daysDiff(d1,d2)); // diferença entre datas em dias contando anos bisextos

   int idade(dias/365); //idade em anos

   int qtd_dias((dias%365)%30); // qtd dias adicionais a partir do dia de nascimento

   int qtd_meses(((dias%365)/30)); // qtd meses adicionais a partir do mes de nascimento

   int meses_totais(dias/30);

   //int meses_totais((dias/30)-qtd_dias); // total de meses decorridos nos dias - dias a mais 

   int qtd_semanas(dias/7); // qtd semanas total dos dias

   int horas(dias*24); // qtd horas total dos dias

   int minutos(horas*60); // qtd minutos total dos dias

   int segundos(minutos*60); // qtd segundos total dos dias

o programa é este abaixo:

#include <cmath>
#include <iostream>

//https://stackoverflow.com/questions/9987562/determining-the-difference-between-dates

    struct Date
    {
     int d, m, y;
    };

    int daysTill(int month)
    {
     int days=0;

     switch(month)
     {
      case 1: days=0; break;

      case 2: days=31; break;

      case 3: days=59; break;

      case 4: days=90; break;

      case 5: days=120; break;

      case 6: days=151; break;

      case 7: days=181; break;

      case 8: days=212; break;

      case 9: days=243; break;

      case 10:days=273; break;

      case 11:days=304; break;

      case 12:days=334; break;
     }

     return days;
    }

    int daysDiff(Date d1, Date d2)
    {
     int dd1=0;
     int dd2=0;
     int y=0;
     int yref=0;

     yref=((d1.y<d2.y) ? d1.y : d2.y);

     for(y=yref;y<d1.y;y++)
     if(__isleap(y))dd1++;

     if(__isleap(d1.y) && d1.m>2) dd1++;

     dd1+=((daysTill(d1.m) + d1.d)+((d1.y - yref) * 365));

     for(y=yref;y<d2.y;y++)
     if(__isleap(y))dd2++;

     if(__isleap(y) && d2.m>2)dd2++;

     dd2+=((daysTill(d2.m) + d2.d)+((d2.y - yref) * 365));

     return abs(dd2-dd1);
    }

int main()
{

 Date d1, d2;

   printf(
          "\n\tDigite duas datas no formato DD/MM/YYYY"
          "\n\n\tDigite data1: "
         );

   scanf("%d/%d/%d",&d1.d,&d1.m,&d1.y);

   printf(
          "\n\tDigite data2: "
         );

   scanf("%d/%d/%d",&d2.d,&d2.m,&d2.y);


   int dias(daysDiff(d1,d2)); // diferença entre datas em dias contando anos bisextos

   int idade(dias/365); //idade em anos

   int qtd_dias((dias%365)%30); // qtd dias adicionais a partir do dia de nascimento

   int qtd_meses(((dias%365)/30)); // qtd meses adicionais a partir do mes de nascimento

   int meses_totais(dias/30);

   //int meses_totais((dias/30)-qtd_dias); // total de meses decorridos nos dias - dias a mais 

   int qtd_semanas(dias/7); // qtd semanas total dos dias

   int horas(dias*24); // qtd horas total dos dias

   int minutos(horas*60); // qtd minutos total dos dias

   int segundos(minutos*60); // qtd segundos total dos dias

   /*
   int segs(dias*86400);
   int horas(segs/3600);
   int minutos((segs-(horas*3600))/60);
   int segundos((segs - (horas * 3600)) - (minutos * 60));
    */

    printf(
           "\n\t%d ano(s) %d mes(es) e %d dia(s) de vida"
           "\n\t%d horas"
           "\n\t%d minutos"
           "\n\t%d segundos"
           "\n\t%d mes(es)"
           "\n\t%d dia(s)"
           "\n\t%d semana(s)\n\n",
           idade,
           qtd_meses,
           qtd_dias,
           horas,
           minutos,
           segundos,
           meses_totais,
           dias,
           qtd_semanas
          );
  return 0;
}

1 answer

-1

To increase the accuracy I suggest you use variables of type float or double instead of int that only stores 4 bytes in memory.

  • yes but first I need to understand what is missing in the calculations what I have to increment to let out use double variables...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.