?? fecdlg.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,"信道傳輸產(chǎn)生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,"信道傳輸產(chǎn)生一位錯碼!可糾錯!該碼位于第%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,"信道傳輸正確或產(chǎn)生不可檢錯的誤碼序列。接收序列為:%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 + -