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

📄 page_112.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_112</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_111.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_112</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_113.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 112</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">These two statements employ some very advanced C++ notation. It's way too early in our look at C++ to explain fully the meaning of all the symbols and identifiers. But here's the general idea. </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setf</font><font face="Times New Roman, Times, Serif" size="3"> is a void function associated with the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cout</font><font face="Times New Roman, Times, Serif" size="3"> stream. (Note that the dot, or period, between </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">cout</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setf</font><font face="Times New Roman, Times, Serif" size="3"> is required.) The first function call ensures that floating point numbers are always printed in decimal form rather than scientific notation. The second function call specifies that the decimal point should always be printed, even for whole numbers. Our best advice is simply to use these statements just as you see them and not worry about the details.</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 third issue-the number of decimal places to be displayed-is handled by the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</font><font face="Times New Roman, Times, Serif" size="3"> manipulator:</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">cout聽&lt;&lt;聽setprecision(3)聽&lt;&lt;聽x;</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 parameter to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</font><font face="Times New Roman, Times, Serif" size="3"> specifies the desired number of decimal places. Unlike </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setw</font><font face="Times New Roman, Times, Serif" size="3">, which applies only to the very next item printed, the value sent to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</font><font face="Times New Roman, Times, Serif" size="3"> remains in effect for all subsequent output (until you change it with another call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</font><font face="Times New Roman, Times, Serif" size="3">). Here are some examples of using </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</font><font face="Times New Roman, Times, Serif" size="3"> in conjunction with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setw</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 cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td height="12"></td></tr><tr><td><table cellspacing="0" width="510" cellpadding="7"><tr><td valign="bottom"><font face="Times New Roman, Times, Serif" size="2"><i>Value of x</i></font></td><td valign="bottom"><font face="Times New Roman, Times, Serif" size="2"><i>Statement</i></font></td><td valign="bottom"><font face="Times New Roman, Times, Serif" size="2"><i>Output (<img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /> means blank)</i></font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">310.0</font></td><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">cout &lt;&lt; setw(10)<br />&lt;&lt; setprecision(2) &lt;&lt; x;</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2"><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /> 310.00</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">310.0</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></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">cout &lt;&lt; setw(10)<br />&lt;&lt; setprecision(5) &lt;&lt; x;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2"><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /> 310.00000</font></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">310.0</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></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">cout &lt;&lt; setw(7)<br />&lt;&lt; setprecision(5) &lt;&lt; x;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="2">310.00000 (expands to 9<br />columns)</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">4.827</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></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">cout &lt;&lt; setw(6)<br />&lt;&lt; setprecision(2) &lt;&lt; x;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="2"><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /> 4.83 (last displayed digit is<br />rounded off)</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><font face="Times New Roman, Times, Serif" size="2">4.827</font></td><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">cout &lt;&lt; setw(6)<br />&lt;&lt; setprecision(1) &lt;&lt; x;</font></td><td valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="0" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="1"></td>  <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="2"><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /><img src="991f4a033b73bdeac7864e4a633e8ee3.gif" border="0" alt="box.gif" width="14" height="19" /> 4.8 (last displayed digit is<br />rounded off)</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></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">Here, too, the total number of columns is expanded if the specified fieldwidth is too narrow. However, the number of columns for fractional digits is controlled entirely by the parameter to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">setprecision</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 table summarizes the three manipulators we have discussed in this section:</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_111.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_112</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_113.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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