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

📄 fecdlg.cpp

📁 2/3了FEC 编码/译码的一种算法,能纠正一位错误
💻 CPP
字号:
// FECDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FEC.h"
#include "FECDlg.h"

#include <bitset>
#include <string>
#include <vector>
#include <iterator>
using std::bitset ;
using std::string ;
using std::vector ;
using std::iterator ;

#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()

/////////////////////////////////////////////////////////////////////////////
// CFECDlg dialog

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

void CFECDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFECDlg)
	DDX_Control(pDX, IDC_EDIT4, m_text);
	DDX_Text(pDX, IDC_EDIT1, m_input);
	DDV_MaxChars(pDX, m_input, 3);
	DDX_Text(pDX, IDC_EDIT2, m_output);
	DDV_MaxChars(pDX, m_output, 15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFECDlg, CDialog)
	//{{AFX_MSG_MAP(CFECDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENCODE, OnEncode)
	ON_BN_CLICKED(IDC_ERROR, OnError)
	ON_BN_CLICKED(IDC_DECODE, OnDecode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFECDlg message handlers

BOOL CFECDlg::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
	GetDlgItem(IDC_ERROR)->EnableWindow(false);
	GetDlgItem(IDC_DECODE)->EnableWindow(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFECDlg::OnEncode() 
{
	// TODO: Add your control notification handler code here
	UpdateData() ;

		//BCD码字母转换表
	char	DConvTable[10][5] = { "0000" , "0001" , "0010" , "0011" , "0100" ,
		                          "0101" , "0110" , "0111" , "1000" , "1001" } ;
	char	AConvTable[6][5] = { "1010" , "1011" , "1100" , "1101" , "1110" , "1111" } ;

	char	temp[13]="" ;	
	int		i ;
	
	GetDlgItem(IDC_EDIT2)->SetWindowText("");
	
	for( i = 0 ; i < strlen(m_input) ; ++ i )
		if( m_input[i] <= 'f' && m_input[i] >= 'a' )
			strcat(temp,AConvTable[m_input[i]-'a'] ) ;
		else
			strcat(temp, DConvTable[m_input[i]-'0'] ) ;

	bitset<12> code_temp(temp , 0, 12 ) ;
	if ( code_temp.to_ulong() > 1023 ) {
		AfxMessageBox("输入范围0--3ff");
		GetDlgItem(IDC_EDIT1)->SetWindowText("");
		return ;
	}

	bitset<10> code( code_temp.to_string().c_str()+2 , 0 , 10 ) ;

	bitset<5> reg("00000",0,5) ;

	for( i = 0 ; i <= 9 ; ++ i ) {
		bool d = (reg[0]^code[i]) ;
		reg[0] = (reg[1]^d) ;
		reg[1] = (reg[2]) ;
		reg[2] = (reg[3]^d );
		reg[3] = (reg[4] );
		reg[4] = d ;
	}
	m_output = (reg.to_string()+code.to_string()).c_str() ;
	//m_out=(reg.to_string()+code.to_string()).c_str() ;
	UpdateData(false) ;

	GetDlgItem(IDC_ERROR)->EnableWindow(true);
	GetDlgItem(IDC_DECODE)->EnableWindow(true);

	((CEdit*)GetDlgItem(IDC_EDIT2))->SetReadOnly(true);
}

void CFECDlg::OnError() 
{
	// TODO: Add your control notification handler code here
	((CEdit*)GetDlgItem(IDC_EDIT2))->SetReadOnly(false);
	GotoDlgCtrl(GetDlgItem(IDC_EDIT2));
}

void CFECDlg::OnDecode() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	
	static CString history_message ;
	char message[200]="" ;
	int i;
	bitset<15> decode((LPCTSTR)m_output,0,15);
	bitset<5> reg("00000",0,5);

	vector< bitset<5> > errTbl ;
	errTbl.push_back( bitset<5>("00001",0,5) ) ;
	errTbl.push_back( bitset<5>("00010",0,5) ) ;
	errTbl.push_back( bitset<5>("00100",0,5) ) ;
	errTbl.push_back( bitset<5>("01000",0,5) ) ;
	errTbl.push_back( bitset<5>("10000",0,5) ) ;
	errTbl.push_back( bitset<5>("01011",0,5) ) ;
	errTbl.push_back( bitset<5>("10110",0,5) ) ;
	errTbl.push_back( bitset<5>("00111",0,5) ) ;
	errTbl.push_back( bitset<5>("01110",0,5) ) ;
	errTbl.push_back( bitset<5>("11100",0,5) ) ;
	errTbl.push_back( bitset<5>("10011",0,5) ) ;
	errTbl.push_back( bitset<5>("01101",0,5) ) ;
	errTbl.push_back( bitset<5>("11010",0,5) ) ;
	errTbl.push_back( bitset<5>("11111",0,5) ) ;
	errTbl.push_back( bitset<5>("10101",0,5) ) ;


	for(i=0;i<15;i++) {
		bool d = (reg[0]^decode[i]) ;
		reg[0] = (reg[1]^d) ;
		reg[1] = (reg[2]) ;
		reg[2] = (reg[3]^d );
		reg[3] = (reg[4] );
		reg[4] = d ;
	}

	if ( reg.any() ) {		
		for ( i = 0 ; i < 15 ; ++ i )
			if ( errTbl[i] == reg )
				break ;		
		if( i == 15 )  {//1位以上错
			string temp_s = decode.to_string() ;
			const char *temp=temp_s.c_str()+5 ;
			bitset<10> temp_b(temp,0,10) ;
			sprintf(message,"信道传输产生2位或2位以上的错误!超过2/3FEC码纠错范围,不可纠错。译码结果为:%s,十六进制结果为:%0x",temp,temp_b.to_ulong()) ;
		}
		else { //1位错
			string temp_s = decode.flip(i).to_string() ;
			const char *temp=temp_s.c_str()+5 ;
			bitset<10> temp_b(temp,0,10) ;
			sprintf(message,"信道传输产生一位错码!可纠错!该码位于第%d位,译码结果为:%s,十六进制结果为:%0x",15-i,temp, temp_b.to_ulong() ) ;
		}
	}
	else {//无错
		string temp_s = decode.to_string() ;
		const char *temp=temp_s.c_str()+5 ;
		bitset<10> temp_b(temp,0,10) ;
		sprintf(message,"信道传输正确或产生不可检错的误码序列。接收序列为:%s,译码结果为:%s,十六进制结果为:%0x",m_output,temp,temp_b.to_ulong() ) ;
	}
	history_message +=CString(message) + CString("\t\t\t") ;
	GetDlgItem(IDC_EDIT3)->SetWindowText( history_message ) ;
}

⌨️ 快捷键说明

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