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

📄 page_797.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_797</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_796.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_797</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_798.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 797</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">parallel row represents the information for one entry. This set of structures is shown in Figure 14-12.</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">A parallel organization of seven different arrays seems to be a clumsy way of representing this logical structure. Yet this is the structure that you have to use for this type of problem if your programming language does not have record data typesand some do not. However, in C++, it makes more sense to define an entry using a record (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3">) data 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="Times New Roman, Times, Serif" size="3">Let's look now at a data structure in which an array of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> is used to represent the address book. The following declarations describe this data structure.</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">const聽int聽MAX_FRIENDS聽=聽150;聽聽聽聽聽//聽Maximum聽number聽of聽friends<br /><br />typedef聽char聽String8[9];聽聽聽聽聽聽聽聽聽//聽Room聽for聽8聽characters聽plus聽\0<br />typedef聽char聽Stringl5[16];聽聽聽聽聽聽聽//聽Room聽for聽15聽characters聽plus聽\0<br /><br />struct聽EntryType<br />{<br />聽聽聽聽String15聽firstName;<br />聽聽聽聽Stringl5聽lastName;<br />聽聽聽聽int聽聽聽聽聽聽areaCode;聽聽聽聽聽聽聽//聽Range聽100..999<br />聽聽聽聽String8聽聽phoneNumber;<br />聽聽聽聽int聽聽聽聽聽聽month;聽聽聽聽聽聽聽聽聽聽//聽Range聽1..12<br />聽聽聽聽int聽聽聽聽聽聽day;聽聽聽聽聽聽聽聽聽聽聽聽//聽Range聽1..31<br />聽聽聽聽int聽聽聽聽聽聽year;聽聽聽聽聽聽聽聽聽聽聽//聽Range聽1900..2100<br />};<br /><br />EntryType聽addressBook[MAX-FRIENDS];</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">firstName</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">lastName</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">areaCode</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">phoneNumber</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">month</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">day</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">year</font><font face="Times New Roman, Times, Serif" size="3"> are member names within the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">EntryType</font><font face="Times New Roman, Times, Serif" size="3">. The members </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">firstName</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">lastName</font><font face="Times New Roman, Times, Serif" size="3"> are strings of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Stringl5</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">areaCode</font><font face="Times New Roman, Times, Serif" size="3"> is an integer, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">phoneNumber</font><font face="Times New Roman, Times, Serif" size="3"> is a string of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">String8</font><font face="Times New Roman, Times, Serif" size="3">. Phone numbers have only seven digits, but we decided to include the hyphen between the first three digits and the last four digits because this is how phone numbers are usually printed. The members </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">month</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">day</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">year</font><font face="Times New Roman, Times, Serif" size="3"> are integers. A complete entry with values stored in the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">addressBook[0]</font><font face="Times New Roman, Times, Serif" size="3"> is shown in Figure 14-13.</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">Notice that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">EntryType</font><font face="Times New Roman, Times, Serif" size="3"> is a flat (nonhierarchical) structure. For birth dates, we could have used a lower-level </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">DateType聽struct</font><font face="Times New Roman, Times, Serif" size="3"> as we did earlier in the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">MachineRec</font><font face="Times New Roman, Times, Serif" size="3"> type. Also, the two pieces of a phone numberthe area code and the numbermight reasonably be defined in a lower-level </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> type. The second Problem-Solving Case Study in this chapter uses just such a phone number type as part of a hierarchical record.</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_796.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_797</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_798.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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