国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
MFC簡易文章編輯器(Brute Froce算法)(符源碼) -


 

    

目 錄

  1. 實訓(xùn)目的    ………………………………………….. ………………………………………….. ……….. ….1
  2. 需求分析    ………………………………………….. ………………………………………….. ……….. ….1
  3. 文章編輯器NoteLite簡介    ………………………………………………………….. ……….. …1
    1. 主要控件    ………………………………………….. ………………………………………….. ……….. ….1
    2. 流程圖        ………………………………………….. ………………………………………….. ……….. ….2
  4. 詳細(xì)設(shè)計    ………………………………………….. ………………………………………….. ……….. ….3
  5. 使用說明    ………………………………………….. ………………………………………….. ……….. ….17
  6. 心得        ………………………………………….. ………………………………………….. ……….. ….21
  7. 參考書目    ………………………………………….. ………………………………………….. ……….. ….21

    (CTRL+左鍵可快速轉(zhuǎn)到相應(yīng)的頁碼)

        

 

文章編輯器NoteLite

  1. 實訓(xùn)目的

運用VC++6.0 通過MFC開發(fā)基于對話框的文章編輯器NoteLite,其中運用數(shù)據(jù)結(jié)構(gòu)中的模式匹配Brute Froce算法來實現(xiàn)匹配,刪除和替換功能。并實現(xiàn)文章編輯器NoteLite的數(shù)字及字符的統(tǒng)計等一般的操作。

 

    本程序演示用C++編寫,完成MFC創(chuàng)建的文章編輯器NoteLite,Brute Froce算法來實現(xiàn)匹配,刪除和替換功能。并統(tǒng)計主編輯框的字符總數(shù),數(shù)字總數(shù),空格總數(shù),字母個數(shù)及與測試編輯框的內(nèi)容匹配的總數(shù)和文章的總行數(shù),能對相應(yīng)的操作進(jìn)行無限撤消和無限重做。

輸入值的范圍:主編輯框,測試編輯框,替換編輯框均可輸入任意字符。在插入字符時要求測試編輯框的內(nèi)容的長度加上主編輯框的內(nèi)容的長度不超過主編輯框長度的允許范圍。在替換字符時要求替換后主編輯框的內(nèi)容長度不能超過主編輯框長度的允許范圍。

輸出形式:統(tǒng)計的結(jié)果會在相應(yīng)的編輯框顯示,其中行數(shù)和字符個數(shù)即時顯示。

測試數(shù)據(jù)

A    要主編輯框中輸入:文章編輯器 V1.0 Beta

    得到字符總數(shù)20,行數(shù)1.統(tǒng)計后得到字母個數(shù)5,空格個數(shù)2,數(shù)字個數(shù)2.

B    在測試編輯框中輸入:文章編輯器

    單擊匹配得匹配次數(shù)1,單擊刪除主編輯框的內(nèi)容變?yōu)?V1.0 Beta",單擊插入主編輯框恢復(fù)"文章編輯器 V1.0 Beta"。

C    在替換編輯框中輸入:NoteLite

    單擊替換主編輯框的內(nèi)容變?yōu)?NoteLite V1.0 Beta"

D    單擊清空主編輯框的內(nèi)容被清空,單擊撤消變?yōu)?文章編輯器 V1.0 Beta",單擊重做主編輯框的內(nèi)容再被清空。

 

    文章編輯器NoteLite運行于Windows下,主要實現(xiàn)文本的輸入,一般的編輯操作,如:插入,刪除,撤消,重做。能查找特定字符在文本中出現(xiàn)的字?jǐn)?shù),并能批量刪除或替換特定的字符串,能對輸入的文本進(jìn)行字符總數(shù),行數(shù),字母個數(shù),數(shù)字個數(shù),空格個數(shù)。

 

(1)為了實現(xiàn)文章編輯器NoteLite上述的功能,需要建立的控件有:

    1.    編輯框控件(10):

        主編輯框            IDC_EDIT2;

測試編輯框        IDC_EDIT1;

        替換編輯框        IDC_EDIT4;

        顯示行數(shù)            IDC_EDIT3;

        顯示字符總數(shù)        IDC_EDIT5;

        顯示字母個數(shù)        IDC_EDIT6;

        顯示數(shù)字個數(shù)        IDC_EDIT7;

顯示空格個數(shù)        IDC_EDIT8;

顯示匹配次數(shù)        IDC_EDIT9;

顯示匹配次數(shù)        IDC_EDIT10;

    2.    按扭控件(9)

插入                IDC_INSERT_BUTTON

刪除                IDC_DEL_BUTTON

匹配                IDC_MATCH_BUTTON

替換                 IDC_REPLACE_BUTTON

統(tǒng)計                IDC_COUNT_BUTTON

清空                 IDC_CLEAR_BUTTON

撤消                 IDC_UNDO_BUTTON

重做                 IDC_REDO_BUTTON

退出                 IDC_CANCEL_BUTTON2

    3.    靜態(tài)編輯框若干個

(2) 流程圖

    1.    匹配算法(Brute Froce算法)

  1. 詳細(xì)設(shè)計
      1
    // NoteLiteDlg.h : header file
      2
    //
      3

      4
    #if !defined(AFX_NOTELITEDLG_H__10C7DDD7_EB75_4AAD_9498_533D9CAAEAC5__INCLUDED_)
      5
    #define AFX_NOTELITEDLG_H__10C7DDD7_EB75_4AAD_9498_533D9CAAEAC5__INCLUDED_
      6

      7
    #if _MSC_VER > 1000
      8
    #pragma once
      9
    #endif // _MSC_VER > 1000
     10

     11
    /////////////////////////////////////////////////////////////////////////////
     12
    // CNoteLiteDlg dialog
     13

     14
    class CNoteLiteDlg : public CDialog
     15
    {
     16
    // Construction
     17
    public:
     18
        
    int undoNum;            //撤消計數(shù)器
     19
        int doNum;                //操作計數(shù)器
     20
        CString CurText[1000];    //當(dāng)前主編輯框內(nèi)容
     21
        CString PreText[1000];    //之前主編輯框內(nèi)容
     22
        BOOL CheckFocus;        //測試編輯框焦點檢測
     23
        CNoteLiteDlg(CWnd* pParent = NULL);    // standard constructor
     24

     25
    // Dialog Data
     26
        
    //{{AFX_DATA(CNoteLiteDlg)
     27
        enum { IDD = IDD_NOTELITE_DIALOG };
     28
        CEdit    m_Time;
     29
        CEdit    m_Replace;
     30
        CEdit    m_Match;
     31
        CEdit    m_TDigitNum;
     32
        CEdit    m_TSpaceNum;
     33
        CEdit    m_TLetterNum;
     34
        CEdit    m_Check;
     35
        CEdit    m_Main;
     36
        CEdit    m_TCharNum;
     37
        CEdit    m_TLine;
     38
        
    //}}AFX_DATA
     39

     40
        
    // ClassWizard generated virtual function overrides
     41
        
    //{{AFX_VIRTUAL(CNoteLiteDlg)
     42
        protected:
     43
        
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
     44
        
    //}}AFX_VIRTUAL
     45

     46
    // Implementation
     47
    protected:
     48
        BOOL MainFocus;        
    //主編輯框焦點檢測
     49
        HICON m_hIcon;
     50

     51
        
    // Generated message map functions
     52
        
    //{{AFX_MSG(CNoteLiteDlg)
     53
        virtual BOOL OnInitDialog();
     54
        afx_msg 
    void OnSysCommand(UINT nID, LPARAM lParam);
     55
        afx_msg 
    void OnPaint();
     56
        afx_msg HCURSOR OnQueryDragIcon();
     57
        afx_msg 
    void OnCancelButton2();
     58
        afx_msg 
    void OnChangeEdit2();
     59
        afx_msg 
    void OnSetfocusEdit1();
     60
        afx_msg 
    void OnSetfocusEdit2();
     61
        afx_msg 
    void OnInsertButton();
     62
        afx_msg 
    void OnClearButton();
     63
        afx_msg 
    void OnCountButton();
     64
        afx_msg 
    void OnDelButton();
     65
        afx_msg 
    void OnUndoButton();
     66
        afx_msg 
    void OnRedoButton();
     67
        afx_msg 
    void OnMatchButton();
     68
        afx_msg 
    void OnReplaceButton();
     69
        afx_msg 
    void OnChangeEdit3();
     70
        
    //}}AFX_MSG
     71
        DECLARE_MESSAGE_MAP()
     72
    }
    ;
     73

     74
    //{{AFX_INSERT_LOCATION}}
     75
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
     76

     77
    #endif // !defined(AFX_NOTELITEDLG_H__10C7DDD7_EB75_4AAD_9498_533D9CAAEAC5__INCLUDED_)
     78

     79
    // NoteLiteDlg.cpp : implementation file
     80
    //
     81

     82
    #include 
    "stdafx.h"
     83
    #include 
    "NoteLite.h"
     84
    #include 
    "NoteLiteDlg.h"
     85

     86
    #ifdef _DEBUG
     87
    #define new DEBUG_NEW
     88
    #undef THIS_FILE
     89
    static char THIS_FILE[] = __FILE__;
     90
    #endif
     91

     92
    /////////////////////////////////////////////////////////////////////////////
     93
    // CAboutDlg dialog used for App About
     94

     95
    class CAboutDlg : public CDialog
     96
    {
     97
    public:
     98
        CAboutDlg();
     99

    100
    // Dialog Data
    101
        
    //{{AFX_DATA(CAboutDlg)
    102
        enum { IDD = IDD_ABOUTBOX };
    103
        
    //}}AFX_DATA
    104

    105
        
    // ClassWizard generated virtual function overrides
    106
        
    //{{AFX_VIRTUAL(CAboutDlg)
    107
        protected:
    108
        
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    109
        
    //}}AFX_VIRTUAL
    110

    111
    // Implementation
    112
    protected:
    113
        
    //{{AFX_MSG(CAboutDlg)
    114
        
    //}}AFX_MSG
    115
        DECLARE_MESSAGE_MAP()
    116
    }
    ;
    117

    118
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    119
    {
    120
        
    //{{AFX_DATA_INIT(CAboutDlg)
    121
        
    //}}AFX_DATA_INIT
    122
    }

    123

    124
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    125
    {
    126
        CDialog::DoDataExchange(pDX);
    127
        
    //{{AFX_DATA_MAP(CAboutDlg)
    128
        
    //}}AFX_DATA_MAP
    129
    }

    130

    131
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    132
        
    //{{AFX_MSG_MAP(CAboutDlg)
    133
            
    // No message handlers
    134
        
    //}}AFX_MSG_MAP
    135
    END_MESSAGE_MAP()
    136

    137
    /////////////////////////////////////////////////////////////////////////////
    138
    // CNoteLiteDlg dialog
    139

    140
    CNoteLiteDlg::CNoteLiteDlg(CWnd
    * pParent /*=NULL*/)
    141
        : CDialog(CNoteLiteDlg::IDD, pParent)
    142
    {
    143
        doNum 
    = 0;        //初始化之前的操作計數(shù)器
    144
        undoNum = 0;    //初始化撤消的操作計數(shù)器
    145

    146
        
    //{{AFX_DATA_INIT(CNoteLiteDlg)
    147
        
    //}}AFX_DATA_INIT
    148
        
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    149
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    150
    }

    151

    152
    void CNoteLiteDlg::DoDataExchange(CDataExchange* pDX)
    153
    {
    154
        CDialog::DoDataExchange(pDX);
    155
        
    //{{AFX_DATA_MAP(CNoteLiteDlg)
    156
        DDX_Control(pDX, IDC_EDIT10, m_Time);
    157
        DDX_Control(pDX, IDC_EDIT4, m_Replace);
    158
        DDX_Control(pDX, IDC_EDIT9, m_Match);
    159
        DDX_Control(pDX, IDC_EDIT7, m_TDigitNum);
    160
        DDX_Control(pDX, IDC_EDIT8, m_TSpaceNum);
    161
        DDX_Control(pDX, IDC_EDIT6, m_TLetterNum);
    162
        DDX_Control(pDX, IDC_EDIT1, m_Check);
    163
        DDX_Control(pDX, IDC_EDIT2, m_Main);
    164
        DDX_Control(pDX, IDC_EDIT5, m_TCharNum);
    165
        DDX_Control(pDX, IDC_EDIT3, m_TLine);
    166
        
    //}}AFX_DATA_MAP
    167
    }

    168

    169
    BEGIN_MESSAGE_MAP(CNoteLiteDlg, CDialog)
    170
        
    //{{AFX_MSG_MAP(CNoteLiteDlg)
    171
        ON_WM_SYSCOMMAND()
    172
        ON_WM_PAINT()
    173
        ON_WM_QUERYDRAGICON()
    174
        ON_BN_CLICKED(IDC_CANCEL_BUTTON2, OnCancelButton2)
    175
        ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
    176
        ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
    177
        ON_EN_SETFOCUS(IDC_EDIT2, OnSetfocusEdit2)
    178
        ON_BN_CLICKED(IDC_INSERT_BUTTON, OnInsertButton)
    179
        ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton)
    180
        ON_BN_CLICKED(IDC_COUNT_BUTTON, OnCountButton)
    181
        ON_BN_CLICKED(IDC_DEL_BUTTON, OnDelButton)
    182
        ON_BN_CLICKED(IDC_UNDO_BUTTON, OnUndoButton)
    183
        ON_BN_CLICKED(IDC_REDO_BUTTON, OnRedoButton)
    184
        ON_BN_CLICKED(IDC_MATCH_BUTTON, OnMatchButton)
    185
        ON_BN_CLICKED(IDC_REPLACE_BUTTON, OnReplaceButton)
    186
        ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
    187
        
    //}}AFX_MSG_MAP
    188
    END_MESSAGE_MAP()
    189

    190
    /////////////////////////////////////////////////////////////////////////////
    191
    // CNoteLiteDlg message handlers
    192

    193
    BOOL CNoteLiteDlg::OnInitDialog()
    194
    {
    195
        CDialog::OnInitDialog();
    196

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

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

    203
        CMenu
    * pSysMenu = GetSystemMenu(FALSE);
    204
        
    if (pSysMenu != NULL)
    205
        
    {
    206
            CString strAboutMenu;
    207
            strAboutMenu.LoadString(IDS_ABOUTBOX);
    208
            
    if (!strAboutMenu.IsEmpty())
    209
            
    {
    210
                pSysMenu
    ->AppendMenu(MF_SEPARATOR);
    211
                pSysMenu
    ->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    212
            }

    213
        }

    214

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

    224

    225
    void CNoteLiteDlg::OnSysCommand(UINT nID, LPARAM lParam)
    226
    {
    227
        
    if ((nID & 0xFFF0== IDM_ABOUTBOX)
    228
        
    {
    229
            CAboutDlg dlgAbout;
    230
            dlgAbout.DoModal();
    231
        }

    232
        
    else
    233
        
    {
    234
            CDialog::OnSysCommand(nID, lParam);
    235
        }

    236
    }

    237

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

    242
    void CNoteLiteDlg::OnPaint() 
    243
    {
    244
        
    if (IsIconic())
    245
        
    {
    246
            CPaintDC dc(
    this); // device context for painting
    247

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

    250
            
    // Center icon in client rectangle
    251
            int cxIcon = GetSystemMetrics(SM_CXICON);
    252
            
    int cyIcon = GetSystemMetrics(SM_CYICON);
    253
            CRect rect;
    254
            GetClientRect(
    &rect);
    255
            
    int x = (rect.Width() - cxIcon + 1/ 2;
    256
            
    int y = (rect.Height() - cyIcon + 1/ 2;
    257

    258
            
    // Draw the icon
    259
            dc.DrawIcon(x, y, m_hIcon);
    260
        }

    261
        
    else
    262
        
    {
    263
            CDialog::OnPaint();
    264
        }

    265
    }

    266

    267
    // The system calls this to obtain the cursor to display while the user drags
    268
    //  the minimized window.
    269
    HCURSOR CNoteLiteDlg::OnQueryDragIcon()
    270
    {
    271
        
    return (HCURSOR) m_hIcon;
    272
    }

    273

    274
    //退出函數(shù)
    275
    void CNoteLiteDlg::OnCancelButton2() 
    276
    {
    277
        OnOK();    
    278
    }

    279

    280
    //主編輯框更新函數(shù)
    281
    void CNoteLiteDlg::OnChangeEdit2() 
    282
    {
    283
        CString MText;        
    //主編輯框內(nèi)容    
    284
        char Count[6];        //統(tǒng)計
    285
        int CharNum;        //字符總個數(shù)
    286
        int LineNum;        //主編輯框的行數(shù)
    287

    288
            m_Main.GetWindowText(MText);    
    //獲得主編輯框內(nèi)容保存入MText
    289
            CharNum = MText.GetLength();        //取得字符串長度
    290
            LineNum = m_Main.GetLineCount();    //取得行數(shù)
    291
            CharNum -= (LineNum-1);            //減去換行產(chǎn)生的字符
    292
            itoa(CharNum,Count,10);    //將整形CharNum轉(zhuǎn)換為字符串保存在Count中
    293
            m_TCharNum.SetWindowText(Count);    //顯示字符數(shù)
    294
            itoa(LineNum,Count,10);    //將整形LineNum轉(zhuǎn)換為字符串保存在Count中
    295
            m_TLine.SetWindowText(Count);            //顯示行數(shù)
    296

    297
        
    298
        
    299
        
    //當(dāng)光標(biāo)移置主編輯框時
    300
        if(MainFocus && !CheckFocus && doNum < 1000)
    301
        
    {
    302
            
    //將之前主編輯框時的內(nèi)容保存
    303
            m_Main.GetWindowText(PreText[doNum]);
    304
            doNum
    ++;
    305
        }
     
    306
            
    307
    }

    308

    309
    //測試編輯框的焦點函數(shù)
    310
    void CNoteLiteDlg::OnSetfocusEdit1() 
    311
    {
    312
        MainFocus 
    = FALSE;        //主編輯框失去焦點
    313
        CheckFocus = TRUE;        //測試編輯框得到焦點
    314

    315
        
    //顯示時間
    316
        CTime   tnow;
    317
        tnow 
    = CTime::GetCurrentTime();  
    318
        CString   snow;
    319
        snow 
    = tnow.Format("%A %B %d %Y");   
    320
        m_Time.SetWindowText(snow); 
    321
        
    322
    }

    323

    324
    //主編輯框的焦點函數(shù)
    325
    void CNoteLiteDlg::OnSetfocusEdit2() 
    326
    {
    327
        MainFocus 
    = TRUE;            //主編輯框得到焦點
    328
        CheckFocus = FALSE;        //測試編輯框失去焦點    
    329
    }

    330

    331
    //插入函數(shù)
    332
    void CNoteLiteDlg::OnInsertButton() 
    333
    {    
    334
        
    if(doNum < 1000)
    335
        
    {
    336
            
    //將之前主編輯框時的內(nèi)容保存
    337
            m_Main.GetWindowText(PreText[doNum]);
    338
            doNum
    ++;
    339
        }

    340

    341
        m_Check.SetSel(
    0,-1);        //選中測試框的全部內(nèi)容
    342
        m_Check.Copy();            //復(fù)制到剪貼版
    343
        m_Main.Paste();                //粘貼入主編輯框    
    344
    }

    345

    346
    //清空函數(shù)
    347
    void CNoteLiteDlg::OnClearButton() 
    348
    {
    349
        
    if(doNum < 1000)
    350
        
    {
    351
            
    //將之前主編輯框時的內(nèi)容保存
    352
            m_Main.GetWindowText(PreText[doNum]);
    353
            doNum
    ++;
    354
        }

    355

    356
        
    357
        m_Main.SetSel(
    0,-1);        //選中主編輯框的所有內(nèi)容
    358
        m_Main.Clear();            //清空所有內(nèi)容
    359
        
    360
    }

    361

    362

    363
    //統(tǒng)計函數(shù)
    364
    void CNoteLiteDlg::OnCountButton() 
    365
    {    
    366
        CString MText;                
    //主編輯框內(nèi)容    
    367
        char Count[6];                //統(tǒng)計
    368
        int LetterNum = 0;            //字母個數(shù)
    369
        int DigitNum = 0;            //數(shù)字個數(shù)
    370
        int SpaceNum = 0;            //空格個數(shù)
    371
        int i = 0;                        //主編輯框的下標(biāo)
    372
        
    373
        m_Main.GetWindowText(MText);        
    //獲得主編輯框內(nèi)容保存入MText
    374

    375
        
    while( i != MText.GetLength())
    376
        
    {
    377
            
    //統(tǒng)計字母個數(shù)
    378
            if( (MText[i] >= 'a' && MText[i] <= 'z'|| (MText[i] >= 'A' && MText[i] <= 'Z'))
    379
                    LetterNum
    ++;
    380
            
    381
            
    //統(tǒng)計數(shù)字個數(shù)
    382
            else if(MText[i] >= '0' && MText[i] <= '9')
    383
                DigitNum
    ++;
    384
            
    385
            
    //統(tǒng)計空格個數(shù)
    386
            else if(MText[i] == ' ')
    387
                SpaceNum
    ++;
    388

    389
            
    else;
    390
                
    391
            i
    ++;
    392
        }

    393

    394
        itoa(LetterNum,Count,
    10); //將整形LetterNum轉(zhuǎn)換為字符串保存在Count中
    395
        m_TLetterNum.SetWindowText(Count);            //顯示字母個數(shù)
    396
        itoa(DigitNum,Count,10);    //將整形NumberNum轉(zhuǎn)換為字符串保存在Count中
    397
        m_TDigitNum.SetWindowText(Count);            //顯示數(shù)字個數(shù)
    398
        itoa(SpaceNum,Count,10);//將整形LineNum轉(zhuǎn)換為字符串保存在Count中
    399
        m_TSpaceNum.SetWindowText(Count);            //顯示空格個數(shù)
    400

    401
    }

    402

    403
    //刪除函數(shù)(Brute Force算法)
    404
    void CNoteLiteDlg::OnDelButton() 
    405
    {
    406
        CString CText;                        
    //測試編輯框的內(nèi)容
    407
        CString MText;                        //主編輯框內(nèi)容
    408
        int i = 0;                                //主編輯框的下標(biāo)
    409
        int j = 0;                                //測試編輯框下的標(biāo)
    410
        int pos = 0;                            //開始刪除的起始位置
    411

    412
        m_Main.GetWindowText(MText);        
    //獲得主編輯框的內(nèi)容
    413
        m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    414

    415
        
    //主編輯框和測試框有內(nèi)容時進(jìn)行刪除
    416
        if ((MText.GetLength() != 0&& (CText.GetLength() != 0))
    417
        
    {
    418
            m_Main.GetWindowText(MText);        
    //獲得主編輯框的內(nèi)容
    419
            m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    420

    421
            
    while (i < MText.GetLength() && MText.GetLength() != 0 )
    422
            
    {
    423
                m_Main.GetWindowText(MText);        
    //獲得主編輯框的內(nèi)容
    424
                m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    425
                
    426
                
    while ( j < CText.GetLength() && i < MText.GetLength())
    427
                
    {
    428
                    
    if (MText[i] == CText[j])
    429
                    
    {
    430
                        i
    ++;
    431
                        j
    ++;
    432
                    }

    433

    434
                    
    else
    435
                    
    {
    436
                        i 
    = i - j + 1;            //主編輯框的下標(biāo)回退
    437
                        j = 0;                //測試編輯框的下標(biāo)回退
    438
                    }

    439
                }
                
    440
                
    441
                
    if (j == CText.GetLength()) 
    442
                
    {
    443
                    pos 
    = i - j;                            //記錄開始刪除的起始位置
    444
                    m_Main.SetFocus();
    445
                    m_Main.SetSel(pos,i);                
    //選中匹配的子串
    446
                    m_Main.ReplaceSel("");                //刪除匹配的子串
    447
                    j = 0;                                //測試編輯框的下標(biāo)回退
    448

    449
                    m_Main.GetWindowText(MText);        
    //獲得主編輯框的內(nèi)容
    450
                    m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    451

    452
                    i 
    = i;                                //重新定位主編輯框的下標(biāo)
    453

    454

    455
                }

    456

    457
            }

    458
        }
        
    459
    }

    460

    461

    462
    //撤消函數(shù)
    463
    void CNoteLiteDlg::OnUndoButton() 
    464
    {
    465
        
    if(undoNum < 1000)
    466
        
    {
    467
            
    //保存當(dāng)前主編輯框的內(nèi)容
    468
            m_Main.GetWindowText(CurText[undoNum]);
    469
            undoNum
    ++;    
    470
        }

    471

    472
        
    if(doNum > 0)
    473
        
    {
    474
            
    //顯示之前的文本
    475
            m_Main.SetWindowText(PreText[doNum-1]);
    476
            doNum
    --;
    477
        }

    478
    }

    479

    480
    //重做函數(shù)
    481
    void CNoteLiteDlg::OnRedoButton() 
    482
    {
    483
        
    if(undoNum > 0)
    484
        
    {
    485
            
    //顯示當(dāng)前的文本
    486
            m_Main.SetWindowText(CurText[undoNum-1]);
    487
            undoNum
    --;
    488
            doNum
    ++;
    489
        }

    490
        
    491
    }

    492

    493
    //匹配函數(shù)(Brute Force算法)
    494
    void CNoteLiteDlg::OnMatchButton() 
    495
    {
    496
        CString CText;                    
    //測試編輯框的內(nèi)容
    497
        CString MText;                    //主編輯框內(nèi)容
    498
        int i = 0;                            //主編輯框的下標(biāo)
    499360docimg_501_    char Count[6];                    //統(tǒng)計
    500360docimg_502_    int MatchNum = 0;                //匹配次數(shù)
    501360docimg_503_    int j = 0;                            //測試編輯框下標(biāo)
    502360docimg_504_
    503360docimg_505_    m_Main.GetWindowText(MText);    //獲得主編輯框的內(nèi)容
    504360docimg_506_    m_Check.GetWindowText(CText);    //獲得測試框的內(nèi)容
    505360docimg_507_
    506360docimg_508_    //主編輯框和測試框有內(nèi)容時進(jìn)行匹配運算
    507360docimg_509_    if ((MText.GetLength() != 0&& (CText.GetLength() != 0))
    508360docimg_510_    {
    509360docimg_511_        while (i < MText.GetLength())
    510360docimg_512_        
    511360docimg_513_            while ( j < CText.GetLength() && i < MText.GetLength())
    512360docimg_514_            {
    513360docimg_515_                if (MText[i] == CText[j])
    514360docimg_516_                {
    515360docimg_517_                    i++;
    516360docimg_518_                    j++;
    517360docimg_519_                }

    518360docimg_520_
    519360docimg_521_                else
    520360docimg_522_                {
    521360docimg_523_                    i = i - j + 1;            //主編輯框的下標(biāo)回退
    522360docimg_524_                    j = 0;                //測試編輯框的下標(biāo)回退
    523360docimg_525_                }

    524360docimg_526_            }

    525360docimg_527_
    526360docimg_528_            if (j == CText.GetLength()) 
    527360docimg_529_            {
    528360docimg_530_                MatchNum++;                //匹配的次數(shù)自加
    529360docimg_531_                j = 0;                        //測試編輯框的下標(biāo)回退
    530360docimg_532_            }

    531360docimg_533_        }

    532360docimg_534_    }

    533360docimg_535_
    534360docimg_536_//將整形MatchNum轉(zhuǎn)換為一個字符串,并將值保存在Count中
    535360docimg_537_    itoa(MatchNum,Count,10);            
    536360docimg_538_    m_Match.SetWindowText(Count);        //顯示匹配次數(shù)
    537360docimg_539_
    538360docimg_540_    
    539360docimg_541_}

    540360docimg_542_
    541360docimg_543_
    542360docimg_544_//替換函數(shù)(Brute Force算法)
    543360docimg_545_void CNoteLiteDlg::OnReplaceButton() 
    544360docimg_546_{
    545360docimg_547_    CString CText;                    //測試編輯框的內(nèi)容
    546360docimg_548_    CString MText;                    //主編輯框的內(nèi)容
    547360docimg_549_    CString RText;                    //替換編輯框的內(nèi)容
    548360docimg_550_    int i = 0;                            //主編輯框的下標(biāo)
    549360docimg_551_    int j = 0;                            //測試編輯框下標(biāo)
    550360docimg_552_    int pos = 0;                        //開始替換的起始位置
    551360docimg_553_
    552360docimg_554_    m_Main.GetWindowText(MText);    //獲得主編輯框的內(nèi)容
    553360docimg_555_    m_Check.GetWindowText(CText);    //獲得測試框的內(nèi)容
    554360docimg_556_    m_Replace.GetWindowText(RText);    //獲得替換編輯框的內(nèi)容
    555360docimg_557_    
    556360docimg_558_
    557360docimg_559_    //主編輯框,測試框的替換框有內(nèi)容時進(jìn)行替換
    558360docimg_560_    if ((MText.GetLength() != 0&& (CText.GetLength() != 0&& (RText.GetLength() != 0))
    559360docimg_561_    {
    560360docimg_562_        m_Main.GetWindowText(MText);        //獲得主編輯框的內(nèi)容
    561360docimg_563_        m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    562360docimg_564_        m_Replace.GetWindowText(RText);        //獲得替換編輯框的內(nèi)容
    563360docimg_565_        
    564360docimg_566_        while (i < MText.GetLength())
    565360docimg_567_        
    566360docimg_568_            m_Main.GetWindowText(MText);        //獲得主編輯框的內(nèi)容
    567360docimg_569_            m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    568360docimg_570_            m_Replace.GetWindowText(RText);        //獲得替換編輯框的內(nèi)容
    569360docimg_571_
    570360docimg_572_            while ( j < CText.GetLength() && i < MText.GetLength() )
    571360docimg_573_            {
    572360docimg_574_                if (MText[i] == CText[j] )
    573360docimg_575_                {
    574360docimg_576_                    i++;
    575360docimg_577_                    j++;
    576360docimg_578_                }

    577360docimg_579_
    578360docimg_580_                else
    579360docimg_581_                {
    580360docimg_582_                    i = i - j + 1;            //主編輯框的下標(biāo)回退
    581360docimg_583_                    j = 0;                //測試編輯框的下標(biāo)回退
    582360docimg_584_                }

    583360docimg_585_            }

    584360docimg_586_
    585360docimg_587_            if (j == CText.GetLength()) 
    586360docimg_588_            {
    587360docimg_589_                pos = i - j;                            //記錄開始替換的起始位置
    588360docimg_590_                m_Main.SetSel(pos,i);                //選中匹配的子串
    589360docimg_591_                m_Main.ReplaceSel(RText);            //替換匹配的子串
    590360docimg_592_                j = 0;                                //測試編輯框的下標(biāo)回退
    591360docimg_593_
    592360docimg_594_                m_Main.GetWindowText(MText);        //獲得主編輯框的內(nèi)容
    593360docimg_595_                m_Check.GetWindowText(CText);        //獲得測試框的內(nèi)容
    594360docimg_596_                m_Replace.GetWindowText(RText);        //獲得替換編輯框的內(nèi)容
    595360docimg_597_
    596360docimg_598_                //當(dāng)替換的文本比被替換的文本長時重新定位主編輯框的下標(biāo)
    597360docimg_599_                if(CText.GetLength() < RText.GetLength())
    598360docimg_600_                    i = i + RText.GetLength() - CText.GetLength();
    599360docimg_601_                
    600360docimg_602_                //當(dāng)替換的文本比被替換的文本短時重新定位主編輯框的下標(biāo)
    601360docimg_603_                else if(CText.GetLength() > RText.GetLength())
    602360docimg_604_                    i = i + RText.GetLength() - CText.GetLength();
    603360docimg_605_
    604360docimg_606_                //當(dāng)替換的文本與被替換的文本等長時重新定位主編輯框的下標(biāo)
    605360docimg_607_                else if(CText.GetLength() == RText.GetLength())
    606360docimg_608_                    i = i;
    607360docimg_609_                else;
    608360docimg_610_
    609360docimg_611_            }

    610360docimg_612_        }

    611360docimg_613_    }

    612360docimg_614_
    613360docimg_615_}

    614360docimg_616_

程序名為NoteLite.exe,運行后如圖:

360docimg_617_

  1. 在主編輯框中輸入一些文字得:

360docimg_618_

 

 

  1. 單擊統(tǒng)計得:

360docimg_619_

  1. 在測試編輯框中輸入:桂林電子科技大學(xué),單擊插入三次得

360docimg_620_

單擊刪除兩次得:

360docimg_621_

單擊撤消兩次得:

360docimg_622_

單擊重做兩次得

360docimg_623_

單擊匹配得

360docimg_624_

 

  1. 在替換編輯框中輸入:06050303,然后單擊替換得

360docimg_625_

單擊清空得

360docimg_626_

  1. 按退出,關(guān)閉程序.

 

 

    本次實訓(xùn)初始使用MFC來開發(fā)一個文章編輯器,首先去了解了相關(guān)的類,當(dāng)開始發(fā)現(xiàn)CEditeView類建立的單文檔已經(jīng)實現(xiàn)的一個文章編輯器的大部分功能,只需要做的是重寫里邊的函數(shù)達(dá)到實訓(xùn)的目的.但由于對單文檔的運用過于生疏,由于時間不允許只能換回比較上手的基于對話框的開發(fā),也只用CEdit.先寫了一個開發(fā)的計劃,要實現(xiàn)的功能有兩個亮點:1是能按列選擇并刪除或替換;2是能實現(xiàn)無限撤消和刪除.最后才考慮用KMP算法實現(xiàn)查找匹配等功能.在開發(fā)過程中發(fā)現(xiàn)要實現(xiàn)第一點并非一件容易的事,要使用到的知識遠(yuǎn)遠(yuǎn)超出我的知識范圍,只能開始第二點,首先想到的是用棧來實現(xiàn),但在MFC里這也不是件容易的事,因為如何捕獲文本框的即時內(nèi)容變化呢?換成數(shù)組就好辦多了,可以在文本框焦點得失時保存入相應(yīng)的數(shù)組里,這里設(shè)兩個數(shù)組,一個用于撤消,一個用于重做,最后完美的實現(xiàn)了第二點.在第三點中算法是沒有問題,但在運行時出現(xiàn)一個系統(tǒng)的錯誤,這個常見的錯誤一般是空指針,下標(biāo)越界等引起,但在調(diào)試過程中并未找到出錯的下標(biāo).最后改成Brute Froce算法才得以實現(xiàn)刪除,替換,匹配的功能.當(dāng)然這個算法效率不佳,但對于這個小程序足矣.其實在實現(xiàn)刪除,替換功能時一定要注意的一點是在文本框的內(nèi)容改變時要重新獲取內(nèi)容才能有達(dá)到目的.這一點也是我在調(diào)試過程中發(fā)現(xiàn)的,很多問題總是在調(diào)試過程中得以解決.

    另外有一點我始終不明白,本想統(tǒng)計漢字的個數(shù),由于用平常的方法無法實現(xiàn),就想到用總字符減去空格,數(shù)字,字母,標(biāo)點余下的應(yīng)該就是漢字了,可以系統(tǒng)卻把一個漢字當(dāng)成了兩個標(biāo)點符號了,而我的標(biāo)點符號集里并沒有漢字.這一點會在今后的學(xué)習(xí)中多加了解,因為一個要國際化的軟件總是應(yīng)該要支持多國語言的.

    這次實訓(xùn)鍛煉了我?guī)讉€方面的功能:1. 規(guī)劃能力;2. 自學(xué)、獨立解決問題能力;3. 知識捕獲、信息整理能力;4. 美工設(shè)計、排版能力;5. 綜合運用知識能力

 

  1. 《數(shù)據(jù)結(jié)構(gòu)習(xí)題與解答》                        (冶金工業(yè)出版社)

    《數(shù)據(jù)結(jié)構(gòu)C++實現(xiàn)》                            (科學(xué)出版社)

    《數(shù)據(jù)結(jié)構(gòu)(C++描述)》                            (清華大學(xué)出版社)

    《C語言程序設(shè)計》                            (清華大學(xué)出版社)

    《算法與數(shù)據(jù)結(jié)構(gòu)—C語言描述》                (高等教育出版社)

    《算法設(shè)計與分析》                            (清華大學(xué)出版社)

    《Windows API函數(shù)實用手冊》                    (冶金工業(yè)出版社)

    《MFC程序設(shè)計Using Visual C++ 6.0》            (北京希望電子出版社)

《C++.NET MFC 類庫應(yīng)用詳解》                    (科學(xué)出版社)


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VC++中使用ADO方式操作ACCESS數(shù)據(jù)庫2
vc技巧????-轉(zhuǎn)載
VC實現(xiàn)數(shù)據(jù)的加密和解密(MD5加密/DES/RSA加密解密)
動態(tài)創(chuàng)建一個CButton按鈕并實現(xiàn)其ON_BN_CLICKED消息
深入淺出VC++串口編程之基于Win32 API
在MFC中創(chuàng)建動態(tài)控件的生成與響應(yīng) c/c++ mfc 動態(tài)按鈕
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服