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

📄 page_605.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_605</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_604.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_605</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_606.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 605</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">result into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">totalOccupants</font><font face="Times New Roman, Times, Serif" size="3">. Next, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> becomes 1 and the loop test occurs. The second loop iteration adds the contents of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">totalOccupants</font><font face="Times New Roman, Times, Serif" size="3"> to the contents of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[1]</font><font face="Times New Roman, Times, Serif" size="3">, storing the result into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">totalOccupants</font><font face="Times New Roman, Times, Serif" size="3">. Now </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> becomes 2 and the loop test is made. You can confirm that the loop invariant is true just before the test occurs: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">totalOccupants</font><font face="Times New Roman, Times, Serif" size="3"> equals the sum of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[0]</font><font face="Times New Roman, Times, Serif" size="3"> through </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[1]</font><font face="Times New Roman, Times, Serif" size="3">, and 0<u>&lt;</u></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> <u>&lt;</u> 350 because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> is 2. Eventually, the loop adds the contents of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[349]</font><font face="Times New Roman, Times, Serif" size="3"> to the sum and increments </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> to 350. Just before the loop test occurs, the loop invariant is true because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">totalOccupants</font><font face="Times New Roman, Times, Serif" size="3"> is the sum of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[0]</font><font face="Times New Roman, Times, Serif" size="3"> through </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">occupants[349]</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">counter</font><font face="Times New Roman, Times, Serif" size="3"> equals 350. At this point, the loop condition is false, and control exits the loop.</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">Note how we used the named constant </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BUILDING_SIZE</font><font face="Times New Roman, Times, Serif" size="3"> in both the array declaration and the For loop. When constants are used in this manner, changes can be made easily. If the number of apartments changes from 350 to 400, only the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">const</font><font face="Times New Roman, Times, Serif" size="3"> declaration of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BUILDING_SIZE</font><font face="Times New Roman, Times, Serif" size="3"> needs to be changed. If the literal value 350 were used in place of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BUILDING_SIZE</font><font face="Times New Roman, Times, Serif" size="3">, several of the statements in the code above, and probably many more throughout the rest of the program, would have to be changed.</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">Because an array index is an integer value, we can access the components by their position in the array-that is, the first, the second, the third, and so on, until the last. Using an </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">int</font><font face="Times New Roman, Times, Serif" size="3"> index is the most common way of thinking about an array. C++, however, provides more flexibility by allowing an index to be of any integral type. (The index expression still must evaluate to an integer in the range from 0 through one less than the array size.) The next example shows an array where the indices are values of an enumeration type.</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">enum聽Drink聽{ORANGE,聽COLA,聽ROOT_BEER,聽GINGER_ALE,聽CHEERY,聽LEMON};<br /><br />float聽salesAmt[6];聽//聽Array聽of聽6聽floats,聽to聽be聽indexed聽by聽Drink聽type<br />Drink聽flavor;聽聽聽聽聽聽//聽Variable聽of聽the聽index聽type</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="3">Drink</font><font face="Times New Roman, Times, Serif" size="3"> is an enumeration type in which the enumerators </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ORANGE</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">COLA</font><font face="Times New Roman, Times, Serif" size="3">, ..., </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LEMON</font><font face="Times New Roman, Times, Serif" size="3"> have internal representations 0 through 5, respectively. </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">salesAmt</font><font face="Times New Roman, Times, Serif" size="3"> is a group of six </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">float</font><font face="Times New Roman, Times, Serif" size="3"> components representing dollar sales figures for each kind of drink (see Figure 11-7). The following code prints the values in the array (see Chapter 10 to review how to increment values of enumeration types in For loops).</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">for聽(flavor聽=聽ORANGE;聽flavor聽&lt;=聽LEMON;聽flavor聽=聽Drink(flavor聽+聽1))<br /><br />聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽//聽聽聽聽聽salesAmt[0..int(flavor)-1]聽have聽been聽output<br />聽聽聽聽聽聽聽聽//聽聽&amp;&amp;聽ORANGE聽&lt;=聽flavor聽&lt;=聽int(LEMON)聽+聽1<br /><br />聽聽聽聽cout聽&lt;&lt;聽salesAmt[flavor]聽&lt;&lt;聽endl;</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_604.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_605</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_606.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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