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

📄 page_578.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_578</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_577.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_578</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_579.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 578</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">character of the month is invalid) or erroneous output (if the second or third characters are invalid). Let's rewrite the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetMonth</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">J_Month</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">A_Month</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">M_Month</font><font face="Times New Roman, Times, Serif" size="3"> functions to incorporate active error detection. The first character must be checked to see whether it is one of the valid first letters. If not, the error must be reported. If the first character is an A, a J, or an M, the second or third character (or both) must be checked.</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 first character should be checked where it is read, in the body of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Get_Month</font><font face="Times New Roman, Times, Serif" size="3">. To handle invalid characters, we add a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">default</font><font face="Times New Roman, Times, Serif" size="3"> label to the Switch statement. The second and/or third character should be checked in the function that uses it. Functions </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">J_Month</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">A_Month,</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">M_Month</font><font face="Times New Roman, Times, Serif" size="3"> need an extra parameter to let </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetMonth</font><font face="Times New Roman, Times, Serif" size="3"> know whether an error occurred. If an error has occurred, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetMonth</font><font face="Times New Roman, Times, Serif" size="3"> must notify the user and request another input. This implies that the Switch statement must be in a loop that continues until a month has been recognized.</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">This scheme does not check the entire spelling of each month. It checks to see only that there are enough letters to recognize a month.</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">We said that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">J_Month</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">A_Month</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">M_Month</font><font face="Times New Roman, Times, Serif" size="3"> need an extra parameter to report to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GetMonth</font><font face="Times New Roman, Times, Serif" size="3"> whether an error occurred. A Boolean flag is appropriate for reporting an error, but now we have a problem. These three functions are value-returning functions and return only one value. We need to return <i>two</i> valuesthe appropriate month and a Boolean flag. Let's change these valuereturning functions into void functions, returning two results through the parameter list. Because void functions should be named using imperative verbs, we rename the three functions as </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CheckJ$ecs;,聽CheckA</font><font face="Times New Roman, Times, Serif" size="3">,聽and聽</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CheckM</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">The following code assumes that we have defined our own Boolean 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">void聽CheckA(聽char,聽Months&amp;,聽Boolean&amp;聽);<br />void聽CheckJ(聽char,聽char,聽Months&amp;,聽Boolean&amp;聽);<br />void聽CheckM(聽char,聽Months&amp;,聽Boolean&amp;聽);<br />void聽GetMonth(聽Months&amp;聽);<br />聽聽.<br />聽聽.<br />聽聽.<br />//******************************************************************<br /><br />void聽GetMonth(聽/*聽out聽*/聽Months&amp;聽month聽)聽聽聽//聽User's聽desired聽month<br /><br />//聽Inputs聽a聽month聽after聽prompting聽the聽user<br /><br />//聽Postcondition:<br />//聽聽聽聽聽User聽has聽been聽prompted聽to聽enter聽a聽month<br />//聽聽&amp;&amp;聽Only聽the聽characters聽needed聽to聽determine聽the聽month聽are聽read<br />//聽聽聽聽聽(the聽remaining聽characters聽on聽the聽input聽line聽are聽read聽and<br />//聽聽聽聽聽discarded)<br />//聽聽&amp;&amp;聽On聽invalid聽input,聽the聽user聽has聽been聽repeatedly聽prompted聽to<br />//聽聽聽聽聽type聽a聽correct聽month<br />//聽聽&amp;&amp;聽month聽==聽value聽of聽type聽Months聽corresponding聽to聽user's聽input<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_577.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_578</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_579.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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