📄 haimincodedlg.cpp
字号:
// HaiminCodeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HaiminCode.h"
#include "HaiminCodeDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CHaiminCodeDlg dialog
CHaiminCodeDlg::CHaiminCodeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHaiminCodeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHaiminCodeDlg)
m_CodeForCheck = _T("");
m_HaiminCode = _T("");
m_OriString = _T("");
m_CodeChecked = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHaiminCodeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHaiminCodeDlg)
DDX_Text(pDX, ID_CodeForCheck, m_CodeForCheck);
DDX_Text(pDX, ID_HaiminCode, m_HaiminCode);
DDX_Text(pDX, ID_OriString, m_OriString);
DDX_Text(pDX, ID_CodeChecked, m_CodeChecked);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHaiminCodeDlg, CDialog)
//{{AFX_MSG_MAP(CHaiminCodeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Reset1, OnResetCode)
ON_BN_CLICKED(IDC_Reset2, OnResetCheck)
ON_BN_CLICKED(IDC_Check, OnCheck)
ON_BN_CLICKED(IDC_HaiminCode, OnHaiminCode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHaiminCodeDlg message handlers
BOOL CHaiminCodeDlg::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 CHaiminCodeDlg::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 CHaiminCodeDlg::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 CHaiminCodeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHaiminCodeDlg::OnResetCode()
{
// TODO: Add your control notification handler code here
m_OriString = "";
m_HaiminCode ="";
UpdateData(false);
}
void CHaiminCodeDlg::OnResetCheck()
{
// TODO: Add your control notification handler code here
m_CodeForCheck = "";
m_CodeChecked = "";
UpdateData(false);
}
void CHaiminCodeDlg::OnCheck()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if( m_CodeForCheck.IsEmpty() )
{
AfxMessageBox("请输入待检测码字");
return;
}
// 检查二进制串时候合法
int n = m_CodeForCheck.GetLength();
for(int i =0;i< n;i++)
{
char a = m_CodeForCheck.GetAt(i);
if( a != '0' && a != '1')
{
AfxMessageBox("带检错码字中有非法(非0或1)字母,请重新输入");
return;
}
}
int m =0;
int r=GetR(m);
while( (m+r) != n )
{
m++;
r=GetR(m);
}
char* code = new char[n];
int j;
int* error = new int[n];
int index =-1;
for( i=0,j=1; i<r; i++,j=j*2)
{
//
int num=0;
for(int k=j+1;k<=n;k++)
{
char* s = changeToBinary(k);
if( s[i] == '1' && m_CodeForCheck.GetAt(k-1) == '1')
num++;
}
if(num%2 == 0 && m_CodeForCheck.GetAt(j-1) =='1')
error[++index] = j;
else
if( num%2 == 1 && m_CodeForCheck.GetAt(j-1) =='0')
error[++index] = j;
}
int e=0;
for(i=0;i<=index;i++)
e += error[i];
CString msg ;
msg.Format("码字中的第%d位出错",e);
AfxMessageBox(msg);
m_CodeChecked = m_CodeForCheck;
if(m_CodeForCheck.GetAt(e-1) == '0')
m_CodeChecked.SetAt(e-1,'1');
else
m_CodeChecked.SetAt(e-1,'0');
UpdateData(false);
return;
}
void CHaiminCodeDlg::OnHaiminCode()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if( m_OriString.IsEmpty() )
{
AfxMessageBox("请输入二进制串");
return;
}
// 检查二进制串时候合法
int m=m_OriString.GetLength();
for(int i =0;i< m;i++)
{
char a = m_OriString.GetAt(i);
if( a != '0' && a != '1')
{
AfxMessageBox("二进制串中有非法(非0或1)字母,请重新输入");
return;
}
}
int r = GetR(m);
int n= m+r;
char* code = new char[n];
int j =0;
int temp = 1;
//initialization
for(i = 1; i<= n ;i++)
{
if( i == temp)
{
j++;
temp = temp*2;
}
else
code[i-1] = m_OriString.GetAt(i-j-1) ;
}
for( i=0,j=1; i<r; i++,j=j*2)
{
//
int num=0;
for(int k=j+1;k<=n;k++)
{
char* s = changeToBinary(k);
if( s[i] == '1' && code[k-1] == '1')
num++;
}
if(num%2 == 0 )
code[j-1] = '0';
else
code[j-1] = '1';
}
// code[n] == '\0';
m_HaiminCode.Format(code);
UpdateData(false);
return;
}
//根据报文长度m得到校验位的个数r
int CHaiminCodeDlg::GetR(int m)
{
int r =1;
int left = m + r +1 ;
int right = 1;
for(int i =0 ; i < r ;i++)
right = right * 2;
while( left> right )
{
r++;
left = m + r +1 ;
right = 1;
for(int i =0 ; i < r ;i++)
right = right * 2;
}
return r;
}
char* CHaiminCodeDlg::changeToBinary(int i)
{
char s[MAX];
int j=0;
while(i>1)
{
if( i%2 == 0)
s[j] = '0';
else
s[j] = '1';
i = (i - s[j] + '0') / 2;
j ++;
}
if(i==1)
s[j] ='1';
else
s[j] = '0';
s[j+1] = '\0';
return s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -