Created
March 15, 2020 18:52
-
-
Save elvis-epx/3b0e0e1a7b24e67db7c88bcfa295a4f6 to your computer and use it in GitHub Desktop.
Calcula idade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(void) | |
{ | |
int dia, mes, ano; | |
printf("Digite sua data de nascimento no formato dd/mm/aaaa:\n"); | |
if (scanf("%d/%d/%d", &dia, &mes, &ano) != 3) { | |
return 1; | |
} | |
printf("Data informada: %02d/%02d/%04d\n", dia, mes, ano); | |
// FIXME verifique se data informada é consistente!!! | |
time_t t; | |
time(&t); | |
struct tm *data_atual = localtime(&t); | |
int idade = data_atual->tm_year + 1900 - ano; | |
if ((data_atual->tm_mon + 1) < mes) { | |
idade -= 1; | |
} else if ((data_atual->tm_mon + 1) == mes) { | |
if ((data_atual->tm_mday) < dia) { | |
idade -= 1; | |
} else if ((data_atual->tm_mday) == dia) { | |
printf("Feliz aniversário. "); | |
} | |
} | |
printf("Você tem %d anos\n", idade); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment