根据身份证判断一个人的年龄,性别

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define MAX_SIZE 18

#define uint unsigned int 

typedef struct Date
{
 int year;
 int month;
 int day;
 int hour;
}Date;

Date get_time()
{
 struct Date date = {0,0,0,0};
 
 time_t current_time;
 struct tm *timeinfo;

 time(&current_time); //get time
 timeinfo = localtime(&current_time); // to local time
 printf("007 the current date/time is %s",asctime(timeinfo)); //asctime
 date.year  = timeinfo->tm_year+1900;
 date.month = timeinfo->tm_mon+1;
 date.day   = timeinfo->tm_mday;
 date.hour  = timeinfo->tm_hour;
 printf("current date is :%d-%d-%d\n",date.year,date.month,date.day);
 return date;
}

int count(char *s)
{

if(NULL == s)

return -1;
 int count= 0;
 while(*s++)
 {
  count++ ;
 }
 return count;
}


int main()
{
 char id[MAX_SIZE+1];
 char age[9];
 puts("intput your id card");
 gets(id);
 id[MAX_SIZE] = '\0';
 int counter = count(id);
 while(18 != counter)
 {
  puts("you input a incorrect num,input again\n");
  gets(id); 
 }
 printf("current id card num is:%s\n",id);

 //get age from id card
 memcpy(age,id+6,8*sizeof(char));
 age[8] = '\0';
 printf("you birthday is :%s\n",age);
 int tmp_age = atoi(age);//to int

 //get current time
 Date tmp_date = get_time();

 //calculate your age
 printf("your age is :%d\n",tmp_date.year-tmp_age/10000); 

 char sex[2];
 sex[0] = id[16];
 sex[1] = '\0';
 int sex_num = atoi(sex);
 if(0 == sex_num%2)
 { 
  printf("your are female\n");
 }else
 {
  printf("your are male\n");
 }
 return 0;
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值