根据身份证号码生成生日和性别

本文介绍如何通过身份证号码提取个人的出生日期和性别信息。身份证号码的第7位到第14位代表出生日期,第17位(奇数位)决定性别,奇数为男性,偶数为女性。详细解析过程将帮助你理解身份证号码的结构并进行相关计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 /*modify bzl 2007-07-12根据身份证号码生成生日和性别*/
 String   ls_date,ls_id,ls_sex,ls_month,ls_day,ls_year
 Date   ld_birthday
 ls_id 
= data
  If Len(ls_id) 
= 15 Then   
    ls_date 
= Mid(ls_id,7,6)
    ls_sex 
= MID(ls_id,15,1)
    ls_year 
= Mid(ls_id,7,2)
        ls_month 
= Mid(ls_id,9,2)
    ls_day 
=Mid(ls_id,11,2)
    ls_year 
= '19' + ls_year
 ElseIf Len(ls_id) 
= 18 Then
   ls_date 
= Mid(ls_id,7,8)
   ls_sex 
= Mid(ls_id,17,1)
   ls_year 
= Mid(ls_id,7,4)
   ls_month 
= Mid(ls_id,11,2)
     ls_day 
=Mid(ls_id,13,2)
 Else
  MessageBox(
"错误","身份证号码只能为15位或18位,请重新输入!^_^' ")
  
return 2
 End   If
 
 
if not (((Integer(ls_year))>=1900) and ((Integer(ls_year))<=2999)) then
  messagebox(
'提示:','对不起,你输入的身份证号码年份不对,请检查')
  
return 2  
 end 
if
  
if not (((Integer(ls_month))>=01) and ((integer(ls_month))<=12)) then
  messagebox(
'提示:','对不起,你输入的身份证号码月份不对,请检查')
  
return 2
  end 
if
 
if not (((Integer(ls_day))>=01) and ((Integer(ls_day))<=31)) then
  messagebox(
'提示:','对不起,你输入的日期份证号码不对,请检查')
  
return 2  
 end 
if
 ls_date 
= Left(ls_date, Len(ls_date) - 4)+"-"+Mid(ls_date,Len(ls_date)-4+1,2)+ "-" + Right(ls_date,2)   
 ld_birthday 
= Date(ls_date)
 
if Mod(integer(ls_sex),2)=0 then
  ls_sex 
= ''
 
else
  ls_sex 
= ''
 end 
if 
在C语言中生成正确的中国居民身份证号码涉及到复杂的数据结构、算法以及校验规则。身份证号码由18位数字组成,分为前6位的地区码、接下来8位的出生日期码、7位顺序码(奇数分配给男性,偶数分配给女性),最后一位是校验码。为了生成一个有效的身份证,你需要: 1. 确定地区编码(根据省市自治区编码规则) 2. 设置出生日期,注意月份范围(1-12)日期(1-31) 3. 根据性别计算顺序码(男单女双) 4. 计算校验码,通常采用刘辉算法(也叫11位校验码算法) 以下是简化版的步骤示例,实际应用中需要更严谨的处理: ```c #include <stdio.h> #include <stdlib.h> // 模拟地区编码,实际情况更复杂 int get_area_code(int province, int city) { // 这里仅做示意,需要实际查找表或者函数 return province * 100 + city; } // 假设我们有固定的生日性别数据 void set_date_and_gender(int* id, int birth_year, int birth_month, int birth_day, char gender) { *id = get_area_code(province, city); // 地区码 *id += birth_year * 10000 + birth_month * 100 + birth_day; // 出生日期码 if (gender == 'M') { (*id)++; // 顺序码奇数给男性 } else { (*id) *= 2; // 顺序码偶数给女性 if ((*id) > 999999) (*id) %= 11; // 避免溢出后校验码错误 } } // 刘辉算法校验码计算 char calculate_check_digit(int id) { int sum = 0; for (int i = 17; i >= 7; i--) { sum += ((i % 2 == 0) ? id : id / 10) * (i % 5 + 1); } if (sum % 11 == 0) return '1'; return (char)(11 - sum % 11); } int main() { int id; set_date_and_gender(&id, 1990, 1, 1, 'F'); // 示例,1990年1月1日出生,女性 id += calculate_check_digit(id); // 添加校验码 printf("Generated ID: %d\n", id); return 0; } ``` 请注意,此代码仅为演示,并未完全考虑所有细节,如闰年的处理校验码计算的完整逻辑。在实际应用中,应当参考官方文档或现成库来保证准确性合规性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值