A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王国文 中级黑马   /  2012-6-10 22:30  /  2596 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看了几天的视频,做了一个小程序,查询你的二代身份证是否是真的
源代码如下:

namespace 验证身份证算法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnview_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.ToString()))
         {
                MessageBox.Show("号码不能为空");
                textBox1.Focus();
                return;
           
         }
            if (textBox1.TextLength !=18 )
            {
                MessageBox.Show("输入的号码长度为18位");
                return;
            }
            int sum = 0;
            string checkwei = "10x98765432";
            string ID = textBox1.Text;
            string number17 = ID.Substring(0, 17);
            string number18 = ID.Substring(17);
            int[] Wquan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            for (int i = 0; i < 17; i++)
            {
                sum = sum + Wquan[i] * Convert.ToInt32(number17[i].ToString());
            }
            string result = checkwei[sum % 11].ToString();
            if (result.Equals(number18,StringComparison.OrdinalIgnoreCase))
            {
                MessageBox.Show("身份证合法");
                this.pic朱逢娅.Visible = true;
            }
            else
            {
                MessageBox.Show("身份证不合法");
            }
            int 年龄 = Convert.ToInt32(textBox1.Text.Substring(6,4 ));
            int year = DateTime.Now.Year - 年龄;
            if (year <18)
            {
                this.pic朱逢娅.Visible = false;
                MessageBox.Show("年龄太小回家看动画片吧");
            }
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            if (e.KeyChar ==8)
            {
                e.Handled = false;
               
            }
      
            //440524188001010014
            //4405,2418,8001,0100,14
            
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      
程序下载地址
http://nj.disk.ftn.qq.com/ftn_handler/69a7c1f3e3a17e4421801f15dd174569cd01afd1ec84583ef561ab0a2046023fe38e095de54df0391178596a8c514bc06e1195dfe1ff319401e7dae46a2ef451/验证身份证算法.rar?k=233238329373ce991591bd501264074953570d50555c000349060807554906035d071550065d0a4b07000c03515d025152055d03347632b78de49cfbd9d3efb0c0f9db859c4a4007163205&fr=01&&txf_fid=92168ff9e3a4291d06e26b872c4d40b4b3ab7a40

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

3 个回复

倒序浏览
可以考虑用正则表达式来解决这个问题

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
好像是杨老师讲的代码,在视屏中讲的
回复 使用道具 举报
举个二代身份证是否是真的小例子:某男性的身份证号码是34052419800101001X。我们要看看这个身份证是不是合法的身份证;
首先:将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7. 9 .10 .5. 8. 4. 2. 1. 6. 3. 7. 9. 10. 5. 8. 4. 2. ,可得出,前17位的乘积和是189 ;
然后:用189除以11得出的结果是17 + 2/11,也就是说余数是2;
最后:通过对应规则就可以知道余数2对应的数字是x。所以,这是一个合格的身份证号码。

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马