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

📄 page_821.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_821</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_820.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_821</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_822.html">next page&nbsp;&gt;</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 821</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">It may be more trouble than it is worth to thoroughly test this program using the procedure outlined above. If the program is to be used only once, three short files with values might suffice. If this program is to be used more than once, however, it should be tested rigorously.</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">Before leaving this problem, we should talk about efficiency. When we input the data from a file into an array, the program stores the sentinel record into the array as well. Every loop that processes the array checks for the end of the list by invoking the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcmp</font><font face="Times New Roman, Times, Serif" size="3"> library function. If each input file contains 100 people's records, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcmp</font><font face="Times New Roman, Times, Serif" size="3"> function is called hundreds of times. This is very time-consuming as </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcmp</font><font face="Times New Roman, Times, Serif" size="3"> itself uses a loop to compare characters in two strings. It would make more sense for the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetRecords</font><font face="Times New Roman, Times, Serif" size="3"> function to return the integer length of the list instead of storing the sentinel record into the array. Thereafter, each loop could use the length of the list to control the number of loop iterations.</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 this program were to be run several times, it would be worthwhile to recode it so that function </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetRecords</font><font face="Times New Roman, Times, Serif" size="3"> returns the length of a list and the other functions make use of the appropriate list lengths. Case Study Follow-Up Exercise 3 asks you to do this.</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="17"></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">Testing and Debugging</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">As we have demonstrated in many examples in this chapter and the last, it is possible to combine data structures in various ways: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> whose components are arrays, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> whose components are </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3">, arrays whose components are arrays (multidimensional arrays), and arrays whose components are </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3">. When arrays and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> are combined, there can be confusion about precisely where to place the operators for </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> member selection (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">.</font><font face="Times New Roman, Times, Serif" size="3">) and array element selection (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">[聽]</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">To summarize the correct placement of these operators, let's use the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentRec</font><font face="Times New Roman, Times, Serif" size="3"> type we introduced at the beginning of the chapter:</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="Courier New, Courier, Mono New, Courier, Mono" size="2">typedef聽char聽NameString[16];<br /><br />struct聽StudentRec<br />{<br />聽聽聽聽NameString聽firstName;<br />聽聽聽聽NameString聽lastName;<br />聽聽聽聽float聽聽聽聽聽聽gpa;<br />聽聽聽聽.<br />聽聽聽聽.<br />聽聽聽聽.<br />};</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_820.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_821</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_822.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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