计算年龄,给定出生日期

时间:2010-04-20 17:47:58

标签: c

鉴于出生日期,我将如何计算C中的年龄?

例如,如果今天的日期是2010年4月20日,并且出生日期是12/08/86,那么年龄将是23岁,8个月和8天。

任何建议将不胜感激。 谢谢!

8 个答案:

答案 0 :(得分:6)

仅一年(无月/日):

1)将日期格式化为yyyymmdd

2)从日期减去出生日期

3)删除最后4个数字

(语言不可知)


所以对你的例子来说;

date 20/04/2010
birth 12/08/1986

转换

date 20100420
birth 19860812

减法

20100420-19860812 = 239608

删除最后4位

23

答案 1 :(得分:4)

解决这样的问题的方法是弄清楚如何使用铅笔和纸张来做 - 然后将其形式化为程序。

对于这个特殊问题,这意味着在高水平,“从当前日期减去出生日期”。对于此减法,您​​可以使用您在小学中学习减法的相同算法的变体 - 在此处开始减去较低值列(在本例中为“天”),必要时从下一个更高值的列借用。例如,要从2010-04-10减去1986-09-15,你会这样做:

2010-04-10 -
1986-09-15
----------

10小于15,所以你必须从月份栏中借用。这意味着月份列减少了一个(到3),并且天数列增加了第3个月(3月 - 31个)的天数。您现在可以减去日期列:

2010-03-41 -
1986-09-15
----------
       -26

我们现在可以转到月份列。 3小于9,所以我们必须再次借用,这一次是从这一年开始。一年中取一个,并在当月增加12个(因为一年中总共有12个月),然后执行减法:

2009-15-41 -
1986-09-15
----------
    -06-26

我们现在可以工作多年了 - 这里从来没有必要借用(除非你试图计算将来出生的人的年龄!):

2009-15-41 -
1986-09-15
----------
  23-06-26

这意味着差异是23年,6个月,26天。

您现在可以将其转换为程序(提示:使用三个单独的整数变量,包括年,月和日)。最棘手的部分是从月份栏中借阅 - 你需要知道那个月有多少天,包括2月份的闰年。

答案 2 :(得分:3)

我假设,根据您的描述,您必须打印出完整的差异,而不仅仅是数年。

问题的一部分是正确输入日期并将其分解为组件,我假设您已经知道如何做或者获得示例代码。

您现在需要做的是计算差异。一种方法是选择参考日期(例如,1900年1月1日),并计算它在第一个日期和第二个日期之前的天数,并计算天数的差异。然后你将天数差异化,并将其分解为数年/月/天。

需要注意的两点是:

1)考虑闰年。

2)弄清楚如何将多个日期翻译成几个月,因为每个月都有不同的天数。

3)如果您不得不输入时间而不仅仅是日期,那么您可能会在一天之内离开。同样,如果输入时区。

我会告诉教练你是否可以对此做出简化的假设(例如,月份总是30,闰年会被忽略等)。我发现很难相信家庭作业要求你正确处理这些问题。

答案 3 :(得分:2)

如果您被允许使用这些库,那就不难了。查看strptimestruct tmtimelocaltime。一旦您以“细分”形式(struct tm)获得它,就可以轻松计算差异(查看tm_ydaytm_montm_yday)。

答案 4 :(得分:1)

如果今天的日期在生日之后,则年龄是出生年份与今天的年份之间的差异。如果今天的日期在生日之前,则减去1。

答案 5 :(得分:0)

/* 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);


}

答案 6 :(得分:0)

//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

答案 7 :(得分:0)

此代码的唯一问题是,如果您出生在闰日,它不会考虑因素。虽然可以有另一个if声明。

// 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);