📄 page_699.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_699</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_698.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_699</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_700.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 699</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">We leave the coding of this driver program and the creation of the test data as an exercise (see Programming Problem 6). Functions </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Search</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">SelSort</font><font face="Times New Roman, Times, Serif" size="3"> should be tested in a similar manner.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">At the beginning of this section, we said that three of the functions used in this chapter had been tested in the Exam program. However, to make sure that they stand up as general-purpose functions, they should be subjected to the same rigorous testing proposed for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Search</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Search2</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">SelSort</font><font face="Times New Roman, Times, Serif" size="3">.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">In two of the search functions (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Search2</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">SearchOrd</font><font face="Times New Roman, Times, Serif" size="3">), we stored the value being searched for in </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">list[length]</font><font face="Times New Roman, Times, Serif" size="3">. The assumption that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length</font><font face="Times New Roman, Times, Serif" size="3"> would be less than </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3"> (the number of places in the array) is written into the documentation. This could lead to a potential error if the calling module fails to check that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length</font><font face="Times New Roman, Times, Serif" size="3"> is less than </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">If the precondition listed in the function documentation is that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length < MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">, then the calling module must make sure that the function is not called with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length <u>></u> MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">. Another way of dealing with the case where </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length <u>></u> MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3"> is to add an error flag to the functions' formal parameter lists and have the functions themselves check for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length <u>></u> MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">. If </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length <u>></u> MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">, the error flag is set to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TRUE</font><font face="Times New Roman, Times, Serif" size="3"> and the search is terminated. This approach changes the preconditions of the functions so that they can be called even when </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">length <u>></u> MAX_LENGTH</font><font face="Times New Roman, Times, Serif" size="3">.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Either way of handling the problem is acceptable. The important point is that it must be clearly stated whether the calling routine or the called function is to check for the error condition.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3"><i>Testing and Debugging Hints</i></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">1. Review the Testing and Debugging Hints for Chapter 11. They apply to all one-dimensional arrays, including strings.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">2. Make sure that every string is terminated with the null character. String constants are automatically null-terminated by the compiler. On input, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">>></font><font face="Times New Roman, Times, Serif" size="3"> operator and the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">get</font><font face="Times New Roman, Times, Serif" size="3"> function automatically add the null character. If you store characters into a string variable individually or manipulate the array in any way, be sure to account for the null character.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">3. Remember that C++ treats string initialization (in a declaration) as different from string assignment. Initialization is allowed but assignment is not.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">4. Aggregate input/output is allowed for strings but not for other array types.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">5. If you use the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">>></font><font face="Times New Roman, Times, Serif" size="3"> operator to input into a string variable, be sure the array is large enough to hold the null character plus the longest sequence of (nonwhitespace) characters in the input stream.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">6. With string input, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">>></font><font face="Times New Roman, Times, Serif" size="3"> operator stops at, <i>but does not consume,</i> the first trailing whitespace character. Likewise, if the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">get</font><font face="Times New Roman, Times, Serif" size="3"> function stops reading early because it encounters a newline character, the newline character is not consumed.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">7. When you use the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcpy</font><font face="Times New Roman, Times, Serif" size="3"> library function, ensure that the destination array is at least as large as the array from which you are copying.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_698.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_699</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_700.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -