include <stdio.h>
include <conio.h>
include <string.h>
include <stdlib.h>
#include <malloc.h>
/将数字转换成整数/
int ChangeStrToInt (char str[]){ int result = 0;
/* 下面代码中把数组名当作指针变量来使用 */
for(; * str!= '\0'; str++)
{
/*将字符表示的数字转换成对应的整数数字并拼接成对应的整数 */ result = result * 10 + ( str - 48); }
return result; }
/ 从字符串中取出对应的子串 /
/ 如:字符串"ABCD",取出第二个字符开始到第4个字符结束,得到字符串"BCD" /
char * GetSubString(char * str, int begin, int end)
{
char * result;
int i;
/ 为局部变量申请内存 */
result = (char )malloc(100); / 用空字符填充字符串 /
for(i = 0; i<100;i++) result[i] = 0;
/ 取子串 */
for(i = begin - 1;i<= end - 1;i++) result[i - begin + 1] = str[i]; return result;
}
void GetIDCodeInfo(char * idCode,int currentYear,int * year, int * month, int * day, int * age, int * gender)
{ /*GetSubString(idCode,7,10)是函数调用语句 */
/*把一个函数调用的结果作为另外一个函数调用语句的实际参数 */
/*称为函数的嵌套使用 */
- year = ChangeStrToInt(GetSubString(idCode,7,10));
- month = ChangeStrToInt(GetSubString(idCode,11,12));
- day = ChangeStrToInt(GetSubString(idCode,13,14));
- age = currentYear -* year; if(ChangeStrToInt(GetSubString(idCode,15,17)) % 2 == 0)
{ - gender = 0;
}
else
{ - gender = 1;
}
}
main()
{
char idCode[30] =""; int year,month,day,age,currentYear = 2021,gender;
year = month = day = age = gender = 0;
printf("输入18位的身份证号码[如:429004197507022369]:") ; gets(idCode); GetIDCodeInfo(idCode,currentYear,&year,&month,&day,&age,&gender); printf("\n输入身份证号码为:%s\n 当前年份为:%d\n\t 出生日期为:[%4d/2d:%2d]\n\t 年龄:%d\n\t 性别:%s", idCode,currentYear,year,month,day,age,gender == 0? "女":"男"); getch(); }