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

📄 page_777.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_777</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_776.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_777</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_778.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 777</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">The expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student.lastName[0]</font><font face="Times New Roman, Times, Serif" size="3"> would access the first letter in the last name, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student.firstName</font><font face="Times New Roman, Times, Serif" size="3">[1] would access the second letter in the first name, and so on.</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 addition to accessing individual components of a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> variable, we can in some cases manipulate </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> as a whole. C++ is more permissive about aggregate operations on </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> than on arrays, but there still are restrictions. The following table compares 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"> with respect to aggregate operations:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td height="12"></td></tr><tr><td><table cellspacing="0" width="538" cellpadding="7"><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2"><i>Aggregate Operation</i></font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2"><i>Arrays</i></font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2"><i>Structs</i></font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">I/O</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No (except strings)</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Assignment</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Arithmetic</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Comparison</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Parameter passage</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">By reference only</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">By value or by reference</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Return as a function's return value</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">No</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Yes</font></td></tr></table></td></tr></table><br /><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">According to the table, one </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> variable can be assigned to another. However, both variables must be declared to be of the same type. For example, if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">anotherStudent</font><font face="Times New Roman, Times, Serif" size="3"> are both declared to be of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentRec</font><font face="Times New Roman, Times, Serif" size="3">, the statement</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">anotherStudent聽=聽student;</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">copies the entire contents of 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">student</font><font face="Times New Roman, Times, Serif" size="3"> to the variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">anotherStudent</font><font face="Times New Roman, Times, Serif" size="3">, member by member.</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">An entire </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> can also be passed as a parameter, either by value or by reference, and a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> can be returned as the value of a value-returning function. Let's define a function that takes a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentRec</font><font face="Times New Roman, Times, Serif" size="3"> variable as a parameter.</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">The task of this function is to determine if a student's grade in a course is consistent with his or her overall grade point average (GPA). We define <i>consistent</i> to mean that the course grade is the same as the rounded GPA. The GPA is calculated on a 4-point scale, where A is 4, B is 3, C is 2, D is 1, and F is 0. If the rounded GPA is 4 and the course grade is A, then the function returns TRUE. If the rounded GPA is 4 and the course grade is not A, then the function returns FALSE. Each of the other grades is tested in the same way.</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">The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Consistent</font><font face="Times New Roman, Times, Serif" size="3"> function is coded below. The formal parameter </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">aStudent</font><font face="Times New Roman, Times, Serif" size="3">, a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentRec</font><font face="Times New Roman, Times, Serif" size="3">, is passed by value. (Assume that the data type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Boolean</font><font face="Times New Roman, Times, Serif" size="3"> has been defined previously.)</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_776.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_777</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_778.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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