帮我用c语音写一个可以按身份证号计算年龄的程序
时间: 2023-05-17 15:05:38 浏览: 91
好的,以下是可以按身份证号计算年龄的 C 语言程序:
```c
#include <stdio.h>
#include <time.h>
int main() {
char id[19];
printf("请输入身份证号码:");
scanf("%s", id);
struct tm birth;
birth.tm_year = (id[6] - '0') * 1000 + (id[7] - '0') * 100 + (id[8] - '0') * 10 + (id[9] - '0') - 1900;
birth.tm_mon = (id[10] - '0') * 10 + (id[11] - '0') - 1;
birth.tm_mday = (id[12] - '0') * 10 + (id[13] - '0');
birth.tm_hour = 0;
birth.tm_min = 0;
birth.tm_sec = 0;
time_t now = time(NULL);
struct tm *ptm = localtime(&now);
int age = ptm->tm_year - birth.tm_year;
if (ptm->tm_mon < birth.tm_mon || (ptm->tm_mon == birth.tm_mon && ptm->tm_mday < birth.tm_mday)) {
age--;
}
printf("年龄为:%d\n", age);
return 0;
}
```
输入身份证号码后,程序会计算出出生日期,并根据当前日期计算出年龄。