c语言按年龄查找函数 c语言年龄计算问题

年龄计算,解释一下例子里的C语言函数

这是递归运算,当运行到age(n-1)时,会将n-1的值传递到age函数中再执行此函数,此时age(int n)中的n就等于n-1;下面的依次循环,直到n==1,执行顺序如下:

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比迁安网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式迁安网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖迁安地区。费用合理售后完善,十余年实体公司更值得信赖。

n=5时,运行到age(4)+2,此时调用age(4),

n=4,运行到age(3)+2,此时调用age(3),

n=3,运行到age(2)+2,此时调用age(2),

n=2,运行到age(1)+2,此时调用age(1),

n=1时,c=10,return c 即返回10.现在将10返回到调用age(2)时的age(1)+2,即c=age(1)+2

c=12,再return 12,返回到调用age(3)时的age(2)+1,c=age(2)+2,此时c=14,····这样一直返回到n=5的情况,此时c=18

}

c语言用函数输入一个年龄判断是否属羊?

调用 CheckShengXiaoByAge 函数,参数1为年龄,参数2为需要判断的生肖,返回值:返回 0 表示年龄与生肖相符,否则不符。

GetCurrentYear 函数,用于获取当前年份

#include stdio.h

#include stdlib.h

#include string.h

#include time.h

typedef enum ShengXiao{

HOU,JI,GOU,ZHU,

SHU,NIU,HU,TU,

LONG,SHE,MA,YANG

}SX;

//获取当前年份

int GetCurrentYear(){

time_t tTime=0;//距离1900年1月1日的秒数

char str[64]={0};

struct tm* stTim=NULL;//时间结构

time(tTime);

stTim = localtime(tTime);

strftime(str,sizeof(str),"%Y",stTim);//格式化时间

return atoi(str);

}

int CheckShengXiaoByAge(unsigned int Age,const char* ShengXiaoName)

{

unsigned int BirthYear=GetCurrentYear()-Age;

char G_ShengXiaoList[12][4]={

[HOU]="猴",[JI]="鸡",[GOU]="狗",[ZHU]="猪",

[SHU]="鼠",[NIU]="牛",[HU]="虎",[TU]="兔",

[LONG]="龙",[SHE]="蛇",[MA]="马",[YANG]="羊"

};

return strcmp(G_ShengXiaoList[BirthYear%12],ShengXiaoName);

}

int main(void)

{

if(!CheckShengXiaoByAge(25,"牛"))

{

puts("Yes");

}

else

{

puts("No");

}

return 0;

}

用C语言函数解决年龄问题

#include"stdio.h"

int f(int n)

{

int t;

if(n==4)

t=15;

else

t=f(n+1)+2;

return t;

}

main()

{

int n;

printf("第一个的年龄为:%d",f(1));

}

递归实现

用C语言编写一个计算自己年龄的函数。

struct Date{

int year;

int month;

int day;

};

Date * fun(Date *date){

Date today;

today.year = 2013;

today.month = 7;

today.day = 1;

Date age;

age.year = today.year - date-year;

age.month = today.month - date-month;

age.day = today.day - date-day;

return age;

}

int main(){

Date me;

me.year = 1980;

me.month = 6;

me.day = 12;

Date * age = fun(me);

printf("%d%d%d",age-year,age-month,age-day);

return 0;

}

C语言,根据年龄判断老年,中年,青年,少年以及数量?

#include "stdio.h"

int main()

{

int buf[10] = {},i;

int num1=0,num2=0,num3=0,num4=0;

printf("输入10个人的年龄:");

for(i=0;i10;i++)

{

scanf("%d",buf[i]);

if(buf[i]=11buf[i]=15) num1++;

else if(buf[i]15buf[i]=35) num2++;

else if(buf[i]35buf[i]60) num3++;

else if(buf[i]=60) num4++;

}

printf("少年的人数: %d\n",num1);

printf("青年的人数: %d\n",num2);

printf("中年的人数: %d\n",num3);

printf("老年的人数: %d\n",num4);

return 0;

}


新闻标题:c语言按年龄查找函数 c语言年龄计算问题
网站URL:http://myzitong.com/article/docdgsi.html