⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 id15to18dlg.cpp

📁 用于将身份证号从15位转为18位
💻 CPP
字号:
// id15to18Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "id15to18.h"
#include "id15to18Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CId15to18Dlg dialog

CId15to18Dlg::CId15to18Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CId15to18Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CId15to18Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CId15to18Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CId15to18Dlg)
	DDX_Control(pDX, IDC_EDIT2, m_c18);
	DDX_Control(pDX, IDC_EDIT1, m_c15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CId15to18Dlg, CDialog)
	//{{AFX_MSG_MAP(CId15to18Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CId15to18Dlg message handlers

BOOL CId15to18Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CId15to18Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CId15to18Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CId15to18Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CId15to18Dlg::OnOK() 
{
	// TODO: Add extra validation here

	CString str15;
	m_c15.GetWindowText(str15);

	str15.TrimLeft();	
	str15.TrimRight();

	if(15 == m_c15.GetWindowTextLength())
	{
		if(!isNum(str15))
			id15to18(str15);
		else
			MessageBox("请正确输入身份证!");
	}
	else if (18 == m_c15.GetWindowTextLength())
	{
		if( !isNum(str15))
			id18to15(str15);
		else
			if(str15.GetAt(17)=='x'||str15.GetAt(17)=='X')
			{
				id18to15(str15);
			}
			else
				MessageBox("请正确输入身份证!");
	}
	else
		MessageBox("请输入15位或18位的身份证号码!");

	m_c15.GetFocus();
	//CDialog::OnOK();
}
/*
	15位身份证转换为18位
*/
void CId15to18Dlg::id15to18(CString str15)
{
	char id18[18+1];
	char W[18+1]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char Y[11+1]={"10X98765432"};
	CString str18;
	int iSum=0,iCur;
	/* 校验码算法
   (1)十七位数字本体码加权求和公式
       S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
       Ai:表示第i位置上的身份证号码数字值
       Wi:表示第i位置上的加权因子
       Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
   (2)计算模
      Y = mod(S, 11)

   (3)通过模得到对应的校验码
      Y: 0 1 2 3 4 5 6 7 8 9 10
      校验码: 1 0 X 9 8 7 6 5 4 3 2
    */
	memset(id18,0,sizeof(id18));
	sprintf(id18,"%s19",str15.Left(6));
	sprintf(id18,"%s%s",id18,str15.Right(9));
	for(iCur = 0; iCur < 17;iCur++)
    {
       iSum += ((id18[iCur] - 48) * W[iCur]);
    }
    iSum %= 11;
    id18[17] = Y[iSum];
	m_c18.SetWindowText(id18);
	UpdateData();
}
/*
	判断是否是数字
*/
int CId15to18Dlg::isNum(CString s)
{ 
	int sum=0;
	for(int   i=0;i<s.GetLength();i++)   
	{   
          if((s.GetAt(i)<'0'   ||   s.GetAt(i)>'9')   &&   s.GetAt(i)!='.')   
        {   
              //出错
			  sum++;
		}   
	}
	return   sum;
}
/*
	18位身份证转换为15位
*/
void CId15to18Dlg::id18to15(CString str18)
{
	char id15[15+1];
	memset(id15,0,sizeof(id15));
	sprintf(id15,"%s",str18.Left(6));
	sprintf(id15,"%s%s",id15,str18.Mid(8,9));
	m_c18.SetWindowText(id15);
	UpdateData();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -