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

📄 b_mdlg.cpp

📁 VC++编程实现了二元域的BM算法。Berlekamp-Massey 算法是一个少有的高效算法。只需要输入密文中的2l个比特
💻 CPP
字号:
// B_MDlg.cpp : implementation file
//

#include "stdafx.h"
#include "B_M.h"
#include "B_MDlg.h"
#include <vector>
using namespace std;

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

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


CString a;
int N;
int my_max(int c,int d);
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()

/////////////////////////////////////////////////////////////////////////////
// CB_MDlg dialog

CB_MDlg::CB_MDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CB_MDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CB_MDlg)
	m_edit1 = _T("");
	m_edit2 = _T("");
	m_edit3 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CB_MDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CB_MDlg)
	DDX_Control(pDX, IDC_EDIT2, c_edit2);
	DDX_Control(pDX, IDC_EDIT3, c_edit3);
	DDX_Control(pDX, IDC_EDIT1, c_edit1);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	DDX_Text(pDX, IDC_EDIT3, m_edit3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CB_MDlg, CDialog)
	//{{AFX_MSG_MAP(CB_MDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CB_MDlg message handlers

BOOL CB_MDlg::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 CB_MDlg::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 CB_MDlg::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 CB_MDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CB_MDlg::OnButton1() 
{
	int i=0;
	int t;
	UpdateData(TRUE);
	c_edit1.GetWindowText(m_edit1);
	while (i<m_edit1.GetLength()) 
	{ 
		t=m_edit1.GetAt(i)-'0';
		if ((t!=0)&&(t!=1)) 
		{
			MessageBox("输入有错,每位必须是0 或 1!","错误!",MB_ICONEXCLAMATION);
			c_edit1.SetFocus();
	        UpdateData(FALSE);
			return;
		}
		i++;
	}

    a=m_edit1;
    N=m_edit1.GetLength();
}


void CB_MDlg::OnButton2() 
{
	int n0=0;
	int i,j,n;
	int m=0,tm=0;
   	UpdateData(TRUE);
	m_edit2.Empty();
    m_edit3.Empty();
	UpdateData(FALSE);
    UpdateData(TRUE);
    vector<int> d(N+1);
	vector<int> fm(N+1);
	vector<int> temp(N+1);
	vector<int> l(N+1);
	vector<int> c(N+1);
	if(!N)
	{
		MessageBox("请按输入按钮输入序列","提示!",MB_ICONINFORMATION);
		c_edit1.SetFocus();
	    UpdateData(FALSE);
		return;
	
	}
	while(1)
	{
		if(a.GetAt(n0)=='0') n0++;
		else break;
	
	}
	for(i=0;i<n0;i++) {d[i]=0;l[i]=0;}
	d[n0]=a.GetAt(n0)-'0';
	l[n0]=0;
	l[n0+1]=n0+1;
	c[0]=1;
	for(i=1;i<n0+1;i++) {c[i]=0;fm[i]=0;}
	c[n0+1]=d[n0];
	n=n0+1;
	fm[0]=1;
	tm=n0;
    while(n<N)
	{   
		d[n]=a.GetAt(n)-'0';
		for(j=1;j<=n;j++)
			d[n]+=c[j]*(a.GetAt(n-j)-'0');
		d[n]=d[n]%2;
		if(d[n]==0) l[n+1]=l[n];
		if(d[n]!=0)
		{   
			l[n+1]=max(l[n],n+1-l[n]);
			for(i=n+1;i>0;i--)
			{
				if(l[i]>l[i-1])
				{
					m=i-1;
					break;			
				}
			}
			for(i=0;i<=tm;i++) temp[i]=fm[i];
			if(m!=tm)
			{		
				for(i=0;i<=m;i++)
				{
					fm[i]=c[i];
				}
				
			}
		
			for(i=(n-tm);i<=n;i++)
			{				
				c[i]=c[i]+d[n]*d[tm]*temp[i-(n-tm)];
			    c[i]=c[i]%2;
             
			}
			tm=m;
		
				
		}
		n++;
		

	}
	int tt[20];
	int ll;
	//c.resize(N);
//	for(i=0;i<=N;i++) tt[i]=c[i];
	for(i=l[N];i>=0;i--) 
	{	
		if(c[i])   { ll=i;		break;}
	}
	/*for(j=0;j<=ll;j++)
	{
	    m_edit3+=tt[j]+'0';	
		UpdateData(FALSE);
		
	}*/
	m_edit3+='1';
	CString stemp;
	for(j=1;j<=ll;j++)
	{
		if(c[j])
		{m_edit3+="+x^";
		//m_edit3+=j+'0';
		
		stemp.Format("%d",j);
		m_edit3+=stemp;}
		UpdateData(FALSE);
		
	}
	stemp.Format("%d",l[N]);
    m_edit2+=stemp;
	UpdateData(FALSE);


}

⌨️ 快捷键说明

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