Given a date of birth, how would I go about calculating an age in C?
For example, if today's date is 20/04/2010 and the date of birth given is 12/08/86, then age will be 2开发者_如何学运维3 years, 8 months, and 8 days.
Any suggestions would be appreciated. Thanks!
For just the year (no months/days) :
1) Format the dates to yyyymmdd
2) Subtract the date of birth from date
3) Remove the last 4 numbers
(Language agnostic)
So for your example;
date 20/04/2010
birth 12/08/1986
convert
date 20100420
birth 19860812
subtract
20100420-19860812 = 239608
drop last 4 digits
23
I'm assuming, based on your description, that you have to print out the full difference, not just years.
One part of the problem is to input the dates correctly and break them down into components, I'll assume you already know how to do that or got sample code for that.
What you need to do now is to calculate the difference. One way to do this would be to pick a reference date (e.g., Jan 1st 1900), and calculate how many days it had been until the first date and the second date, and calculate the difference in days. Then you take the difference in days and break it back into years/months/days.
Two things to notice are:
1) Take leap years into account.
2) Figure out how to translate a number of dates into months, since each month has a different number of days.
3) If you had to input times and not just dates, you could be off by a day. Similarly if time zones are input.
I would check with the instructor whether you can make a simplifying assumption about that (E.g., months are always 30, leap years are ignored, etc.). I find it hard to believe that a homework assignment would require you to deal with these correctly.
The way to approach a problem like this is to figure out how you'd do it using a pencil and paper - then formalise that into a program.
For this particular problem, that means at a high level, "subtract birth date from current date". For this subtraction, you use a variation of the same algorithm you learn for subtraction in primary school - where you start by subtracting the lower value column (in this case, "days"), borrowing from the next-higher-value column if necessary. For example, to subtract 1986-09-15 from 2010-04-10, you would do:
2010-04-10 -
1986-09-15
----------
10 is less than 15, so you have to borrow from the months column. This means that the months column goes down by one (to 3), and the days column goes up by the number of days in month 3 (March - so 31). You can now do the subtraction of the days column:
2010-03-41 -
1986-09-15
----------
-26
We can now move on to the months column. 3 is less than 9, so we have to borrow again, this time from the year. Take one off the year, and add 12 to the month (since there are always 12 months in a year), then perform the subtraction:
2009-15-41 -
1986-09-15
----------
-06-26
We can now work on the years - there's never a need to borrow here (unless you're trying to calculate the age of someone born in the future!):
2009-15-41 -
1986-09-15
----------
23-06-26
This means the difference is 23 years, 6 months, 26 days.
You can now work on turning this into a program (hint: use three seperate integer variables for years, months and days). The trickiest part is the borrow from the months column - you need to know how many days are in that month, including leap years for February.
If you're allowed to use the libraries, it's not too hard. Look at strptime, struct tm, time, and localtime. Once you have it in "broken-down" form (struct tm
), it's easy to compute the difference (look at tm_yday
, tm_mon
, and tm_yday
).
If today's date falls after the birthday, the age is the difference between the birth year and today's year. If today's date falls before the birthday, subtract 1.
/* program for calculating the age */
/* author - nitin kumar pandey */
/* mail - nitindonpandey@gmail.com */
#include<stdio.h>
#include<conio.h>
int d1,d2,d3,m1,m2,m3,y1,y2,y3;
void year(int d1,int m1,int y1,int d2,int m2,int y2);
void main()
{
clrscr();
printf("please enter the current date \n");
printf("enter the day");
scanf("%d",&d1);
printf("enter the month");
scanf("%d",&m1);
printf("enter the year");
scanf("%d",&y1);
printf("Now thank you for your cooperation \n now please enter the date of birth");
printf("enter the day");
scanf("%d",&d2);
printf("enter the month");
scanf("%d",&m2);
printf("enter the year");
scanf("%d",&y2);
year(d1,m1,y1,d2,m2,y2);
getch();
}
void year(int d1,int m1,int y1,int d2,int m2,int y2)
{
if(d2>d1)
{
m1=m1-1;
d1=d1+30;
}
if(m2>m1)
{
y1=y1-1;
m1=m1+12;
}
if(y2>y1)
{
exit(0);
}
d3=d1-d2;
m3=m1-m2;
y3=y1-y2;
printf("current age is \n day %d \n month %d \n year %d ",d3,m3,y3);
}
//Age calculation. A simple c++ program
#include<iostream>
#include<iostream>
#include<ctime>
using namespace std;
void main()
{
system("cls");
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int currentday = aTime->tm_mday;
int currentmonth = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int currentyear = aTime->tm_year + 1900;
int birthday,birthmonth,birthyear;
cout<<"Enter birth year: ";
cin>>birthyear;
if(birthyear>currentyear)
{
cout<<"Current year: "<<currentyear<<endl
<<"Birht year : "<<birthyear<<endl
<<"Invalid...."<<endl<<endl;
system("pause");
main();
}
else if(birthyear<1900)
{
cout<<"Birht year should greater than 1900...."<<endl;
system("pause");
main();
}
cout<<"Enter birth month: ";
cin>>birthmonth;
if(birthmonth<1 || birthmonth>12)
{
cout<<"Birth month should be 1-12"<<endl;
system("pause");
main();
}
else if(birthyear==currentyear && birthmonth>currentmonth)
{
cout<<"Current Month/Year: "<<currentmonth<<"/"<<currentyear<<endl
<<"Birth Month/Year : "<<birthmonth<<"/"<<birthyear<<endl
<<"Future Birth Date. Invalid...."<<endl;
system("pause");
main();
}
cout<<"Enter birth day: ";
cin>>birthday;
if(birthday<1 || birthday>31)
{
cout<<"Birth day should 1-31"<<endl;
system("pause");
main();
}
else if(birthyear==currentyear && birthmonth==currentmonth && birthday>currentday)
{
cout<<"Current Day/Month/Year: "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl
<<"Birth Day/Month/Year : "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl
<<"Future Birth Date. Invalid...."<<endl;
system("pause");
main();
}
else if(birthyear%4==0 && birthmonth==2 && birthday>29)
{
cout<<"Febuary should be 1-29"<<endl;
system("pause");
main();
}
else if( (birthmonth==4 || birthmonth==6 || birthmonth==9 || birthmonth==11) && birthday>31)
{
cout<<"This month cannot have 31 days...."<<endl;
system("pause");
main();
}
int ageday,agemonth,ageyear;
if(birthmonth>currentmonth)
{
agemonth=currentmonth;
ageyear=currentyear-birthyear-1;
ageday=currentday;
}
else
{
agemonth=currentmonth-birthmonth;
ageyear=currentyear-birthyear;
ageday=currentday-birthday;
}
if(ageyear==0 && agemonth==0)
{
cout<<"your Date of Birth: "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl;
cout<<"Current Date : "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl;
cout<<"your Age : "<<ageday<<" days"<<endl;
}
else if(ageyear==0)
{
cout<<"your Date of Birth: "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl;
cout<<"Current Date : "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl;
cout<<"your Age : "<<agemonth<<" Months"<<ageday<<" days"<<endl;
}
else if(agemonth==0)
{
cout<<"your Date of Birth: "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl;
cout<<"Current Date : "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl;
cout<<"your Age : "<<ageyear<<" years"<<ageday<<" days"<<endl;
}
else if(ageday==0)
{
cout<<"your Date of Birth: "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl;
cout<<"Current Date : "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl;
cout<<"your Age : "<<ageyear<<" years"<<agemonth<<" Months"<<endl;
}
else
cout<<"your Date of Birth: "<<birthday<<"/"<<birthmonth<<"/"<<birthyear<<endl;
cout<<"Current Date : "<<currentday<<"/"<<currentmonth<<"/"<<currentyear<<endl;
cout<<"your Age : "<<ageyear<<" years"<<agemonth<<" Months"<<ageday<<" days"<<endl;
system("pause");
}
//AAW
The only issue with this code is that it doesn't factor in if you are born on leap day. there can be another if statement for that though.
// Age Calculator
time_t t = time(NULL);
struct tm tm = *localtime(&t);
int num_day;
if ( month == 1 )
{
num_day = 31;
}
if ( month == 2 )
{
num_day = 28;
}
if ( month == 3 )
{
num_day = 31;
}
if ( month == 4 )
{
num_day = 30;
}
if ( month == 5 )
{
num_day = 31;
}
if ( month == 6 )
{
num_day = 30;
}
if ( month == 7 )
{
num_day = 31;
}
if ( month == 8 )
{
num_day = 31;
}
if ( month == 9 )
{
num_day = 30;
}
if ( month == 10 )
{
num_day = 31;
}
if ( month == 11 )
{
num_day = 30;
}
if ( month == 12 )
{
num_day = 31;
}
int age_year = (tm.tm_year + 1900) - year;
int age_month = (tm.tm_mon + 1) - month;
int age_day = (tm.tm_mday) - day;
if ( age_month <= 0 )
{
age_year = (tm.tm_year + 1900) - year - 1;
age_month = 12 + (tm.tm_mon + 1) - month;
}
if ( age_day <= 0 )
{
age_month = 12 + (tm.tm_mon + 1) - month - 1;
age_day = num_day + (tm.tm_mday) - day;
}
printf("Your age: %d years %d months %d days\n", age_year, age_month, age_day);
精彩评论