通过身份证号码进行归属地的查询、出生日期,以及判断性别(升级版)

程序输出结果在这里插入图片描述
添加类(IDInfo.cs)–获取身份证中的各种信息


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace IDInformation02
{
    class IDInfo
    {
        public string GetUserBirthday(string IDInfo)
        {
            //读取出生日期
            string res = "";
            string IDcard1 = IDInfo;
            string strYearDate = IDcard1.Substring(6, 4);//截取第6-10位
            string strMonthDate = IDcard1.Substring(10, 2);//截取第6-10位
            string strDate = IDcard1.Substring(12, 2);//截取第6-10位
            res = strYearDate + '年' + strMonthDate + '月' + strDate + '日';
            return res;
        }
        public string GetUserAddress(string IDInfo)
        {
             //读取所在地
            string res = "";
            string IDcard1 = IDInfo;
            StreamReader file = null;
            JObject home_location;
            //这个异常处理是失败的,后续还需改进
            try
            {
                //System.IO.StreamReader file = System.IO.File.OpenText(@"F:\SoftwareLearning\VS2017\C#Learing\chapter01\ConsoleApp0721\province01.json");
                file = System.IO.File.OpenText(@"F:\SoftwareLearning\VS2017\C#Learing\chapter01\ConsoleApp0721\province.json");
                JsonTextReader reader = new JsonTextReader(file);
                //进行字符串切割赋值
                home_location = (JObject)JToken.ReadFrom(reader);
                res = home_location[IDcard1.Substring(0, 6)].ToString();
            }
            catch (ExceptionName)
            {
                res = "文件读取失败";
                throw;
            }
            return res;
        }

        public string GetUserGender(string IDInfo)
        {
            //读取性别
            string res = "男";
            string IDcard1 = IDInfo;
            string strSex = IDcard1.Substring(16, 1);//截取倒数第二位                                            
            int numSex = int.Parse(strSex);   //字符串型变为数值型
            if (numSex % 2 == 0)
            {
                res = "女";
            }
            else
            {
                res = "男";
            }
            return res;
        }
    }
}

添加类(ExceptionHandling.cs)–判断身份证是不是18位数

using System;

namespace IDInformation02
{
    class ExceptionHandling
    {
        //判断身份证是不是18位数
        public string GetUserNum(string IDInfo)
        {
            string IDcard1 = IDInfo;
            string res;
            int num = 0;
            foreach (char c in IDcard1)
            {
                if (Char.IsDigit(c)) ++num;
            }
            if (num == 18)
                res = "身份证号码正确";
            else
            {
                res = "身份证号码正确";
            }
            return res;
        }
    }
}

窗口程序(Form1.cs)

using System;
using System.Windows.Forms;

namespace IDInformation02
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string IDcard1;
            IDcard1 = textBox1.Text;
            IDInfo idinfo = new IDInfo();
            textBox2.Text = idinfo.GetUserBirthday(IDcard1); //输出出生日期
            textBox3.Text = idinfo.GetUserAddress(IDcard1);  //输出所在地
            textBox4.Text = idinfo.GetUserGender(IDcard1);   //输出性别
            ExceptionHandling exceptionHandling = new ExceptionHandling(); //异常处理,还不完善(比如前六位地址输入是否正确)
            textBox5.Text = exceptionHandling.GetUserNum(IDcard1);
        }
    }
}

主程序(Program.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IDInformation02
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值