C#WPF–身份证查询
作者心得
很简单的玩意儿,不过并不是通过api调用,只是简单的测试身份证号码的规则,麻雀虽小,五脏俱全!
源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SFZ
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{ char last = '#';
int num=login.GetLineLength(0);
if (num == 18)
{
char[] count = new char[18];
count = login.Text.ToCharArray();
int[] middle= new int[18];
int i = 0;
foreach (char k in count)
{
middle[i] = k-48;
i++;
}
int with=middle[0] * 7 + middle[1] * 9 + middle[2] * 10 + middle[3] * 5 + middle[4] * 8 + middle[5] * 4 + middle[6] * 2 + middle[7] * 1 + middle[8] * 6
+ middle[9] * 3 + middle[10] * 7 + middle[11] * 9 + middle[12] * 10 + middle[13] * 5 + middle[14] * 8 + middle[15] * 4 + middle[16] * 2;
int avg = with % 11;
switch (avg)
{
case 0:
last = '1';
break;
case 1:
last = '0';
break;
case 2:
last = 'X';
break;
case 3:
last = '9';
break;
case 4:
last = '8';
break;
case 5:
last = '7';
break;
case 6:
last = '6';
break;
case 7:
last = '5';
break;
case 8:
last = '4';
break;
case 9:
last = '3';
break;
case 10:
last = '2';
break;
}
if (last == count[17])
{
MessageBox.Show("合法的身份证!");
}
else
{
MessageBox.Show("输入的身份证不合法!");
}
}
else
{
MessageBox.Show("你没有输入完整的身份证号!", "提示");
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
login.Text = "";
MessageBox.Show("文本已经重置!");
}
}
}
效果截图
作者的话
int with=middle[0] * 7 + middle[1] * 9 + middle[2] * 10 + middle[3] * 5 + middle[4] * 8 + middle[5] * 4 + middle[6] * 2 + middle[7] * 1 + middle[8] * 6+ middle[9] * 3 + middle[10] * 7 + middle[11] * 9 + middle[12] * 10 + middle[13] * 5 + middle[14] * 8 + middle[15] * 4 + middle[16] * 2
这里是运算法则了,刚好只是老师上课提出了一下,干脆就给他弄了出来!乱填的话,貌似只有10分之1的几率蒙对!最后一个校验码可不是乱发的!