Use a recursive algorithm to solve a bug in the CEDIT in the VC

xiaoxiao2021-03-06  43

Summary Visual C is the boss in the Microsoft programming tool, which is powerful, using it almost any program on the Windows platform. But at the same time, it is difficult to enter the threshold of VC due to powerful and strong. Since it is a fist product of Microsoft development tools, there must be strict test before publishing, and continuously upgrade maintenance, therefore, it is difficult to find BUG once a bug is expected.

Key words Visual C ; control; bug; debug

The control is a packaged class in Visual C , using the control, we can develop the OOP program quickly; bug is an error in the program; debug is debugging during program operation.

BUG description

As shown below:

Figure 1

Press the CONFIRM button, the debug interface is as follows:

Figure 2

As can be seen from the value of Strtext, the input content in Figure 1 is not "", " :

Deleting the 8th, 9 characters of the third line, that is, the content in Figure 1 is now only 58 characters, replaced with the first line of the first line of the content in Figure 1 and the last character of the second line, as shown:

Figure 3

Now if you add a character in the middle of the first line and the second line, the following interface appears:

Figure 4

Then I want to ask everyone a question, can also enter a few characters in the third line? The answer is: can't enter characters, and you cannot enter characters anywhere in the content. Through Debug we can know the reasons, please see the Debug interface:

Figure 5

From Figure 5, we can see that the alternative characters (spaces) are not displayed in Figure 4, and the first row and the second line become 21 characters. A small number of such situations can make users understand that it is convenient to modify the input content, but if there is a lot of this situation, it is a serious problem, such as: if there is 1000 lines above, 10 characters per line At the same time, there are 200 above, then the performance is given to the user: if the user wants to fill the edit box, 200 characters in the edit box cannot be input, and each line of content in 200 is 11 characters. At the same time, if the subsequent processing of the program is strictly required for the characters of each row you entered above (such as Swift packets used in bank international business settlement), then a serious problem will happen.

problem analysis

This is a very tricky problem, there is the following key must be resolved:

(1) Determine the space in this problem is what users need, and is formed by the user or by the above method;

(2) After each input characters, the string of the current input line cannot be obtained;

(3) Cannot get a sign of each line, such as the return line, etc.

If there is 100 rows in the edit box, it is difficult to determine if the length of 10 rows is more than 10 lines.

Solve the problem algorithm

For the above problems, after acquiring the contents of the edit box, the recursive algorithm can be used to filter out related inputs.

Algorithm ideology:

Starting from the first row, recursive calls in the auto-transportation behavior, add each time. The record of recursive is: According to the maximum number of characters per line, it is determined for each line. If the last character is space, the space is deleted, and finally, the unwanted space is removed. Calling program as follows: CString strText, strObjText, strExchange; CEdit * pEdit; UpdateWindow (); pEdit = (CEdit *) GetDlgItem (IDC_ET_CON); int nPos = 0, nLen = 0; int nMaxCols = 20; strObjText = ""; pEdit -> getWindowText; npos = strText.Find ("/ r / n"); while (npos! = -1) {strexchange = strText.Light (NPOS); strText = strText.right (strText.getLength () - nPos -2); TrimEndNull (nMaxCols, strExchange, nMaxCols); strObjText = strObjText strExchange "/ r / n"; nPos = strText.Find ( "/ r / n");} strExchange = strText; TrimEndNull (nMaxCols , strexchange, nmaxcols; strobjtext = strobjtext strexchange; pedit-> setWindowText (strobjtext); UpdateWindow ();

The recursive function is as follows:

TrimEndNull (int nPos, CString & strExchange, const int nMaxCols) {if (nPos> = strExchange.GetLength ()) return; if (strExchange.GetAt (nPos) == '') {strExchange = strExchange.Left (nPos) strExchange .Right (strexchange.getlength () - NPOS - 1);} npos = npos nmaxcols; trimendnull (npos, strexchange, nmaxcols);}

The current procedure is now as follows:

WINDOW.OPEN ("http://dev.yesky.com/softchannel/72342371928702976/index.shtml")

Original input

Replace

After entering A, B, you can pay the edit box

Conclude

This article mainly discusses a bug that solves the CEDIT control in VC , using the recursive algorithm that everyone is familiar with, through the analysis of this bug, this difficult problem has become very simple.

转载请注明原文地址:https://www.9cbs.com/read-54811.html

New Post(0)