编写一个程序:输入一个身份证号,判断该号码对应的人是多少岁,是男是女

#include<stdio.h>

int main(){
    char ID[19];
    int year=0,i;
    scanf("%s",&ID);
    for(i=6;i<=9;i++)
    year=10*year+(ID[i]-'0');
    printf("%d",2018-year);
    if((ID[16]-'0')%2==1)
    printf("男\n");
    else
    printf("女\n");
    return 0;

这里写图片描述

以下是一个简单的 Java 程序,可以对输入的身份证号码进行分析: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class IDCardAnalyzer { private static Map<String, String> cityCodeMap = new HashMap<>(); static { cityCodeMap.put("11", "北京"); cityCodeMap.put("12", "天津"); cityCodeMap.put("13", "河北"); cityCodeMap.put("14", "山西"); cityCodeMap.put("15", "内蒙古"); cityCodeMap.put("21", "辽宁"); cityCodeMap.put("22", "吉林"); cityCodeMap.put("23", "黑龙江"); cityCodeMap.put("31", "上海"); cityCodeMap.put("32", "江苏"); cityCodeMap.put("33", "浙江"); cityCodeMap.put("34", "安徽"); cityCodeMap.put("35", "福建"); cityCodeMap.put("36", "江西"); cityCodeMap.put("37", "山东"); cityCodeMap.put("41", "河南"); cityCodeMap.put("42", "湖北"); cityCodeMap.put("43", "湖南"); cityCodeMap.put("44", "广东"); cityCodeMap.put("45", "广西"); cityCodeMap.put("46", "海南"); cityCodeMap.put("50", "重庆"); cityCodeMap.put("51", "四川"); cityCodeMap.put("52", "贵州"); cityCodeMap.put("53", "云南"); cityCodeMap.put("54", "西藏"); cityCodeMap.put("61", "陕西"); cityCodeMap.put("62", "甘肃"); cityCodeMap.put("63", "青海"); cityCodeMap.put("64", "宁夏"); cityCodeMap.put("65", "新疆"); cityCodeMap.put("71", "台湾"); cityCodeMap.put("81", "香港"); cityCodeMap.put("82", "澳门"); cityCodeMap.put("91", "国外"); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入身份证号码:"); String idCard = scanner.nextLine().trim(); if (!isValidIdCard(idCard)) { System.out.println("error"); return; } String province = getProvince(idCard); int age = getAge(idCard); String gender = getGender(idCard); System.out.printf("省份:%s\n年龄:%d\n性别:%s\n", province, age, gender); } public static boolean isValidIdCard(String idCard) { if (idCard == null || idCard.length() != 18) { return false; } String provinceCode = idCard.substring(0, 2); if (!cityCodeMap.containsKey(provinceCode)) { return false; } String birthdayStr = idCard.substring(6, 14); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); sdf.setLenient(false); try { Date birthday = sdf.parse(birthdayStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(birthday); int year = calendar.get(Calendar.YEAR); if (year < 1900 || year > 2021) { return false; } } catch (ParseException e) { return false; } return true; } public static String getProvince(String idCard) { String provinceCode = idCard.substring(0, 2); return cityCodeMap.get(provinceCode); } public static int getAge(String idCard) { String birthdayStr = idCard.substring(6, 14); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); sdf.setLenient(false); try { Date birthday = sdf.parse(birthdayStr); Calendar calendar = Calendar.getInstance(); int nowYear = calendar.get(Calendar.YEAR); calendar.setTime(birthday); int birthYear = calendar.get(Calendar.YEAR); return nowYear - birthYear; } catch (ParseException e) { return -1; } } public static String getGender(String idCard) { int genderCode = Integer.parseInt(idCard.substring(16, 17)); if (genderCode % 2 == 0) { return ""; } return ""; } } ``` 程序中,我们定义了一个 `cityCodeMap` 映射表,用于将身份证号码中的省份编码转换成对应的省份名称。然后我们实现了三个方法: - `isValidIdCard` 方法用于判断身份证号码是否合法。 - `getProvince` 方法用于从身份证号码中获取省份名称。 - `getAge` 方法用于从身份证号码中获取年龄。 - `getGender` 方法用于从身份证号码中获取性别。 最后在 `main` 方法中,我们通过 `Scanner` 获取用户输入的身份证号码,并调用以上三个方法进行分析,输出分析结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值