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

📄 page_1122.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_1122</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_1121.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1122</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_1123.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 1122</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><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 align="center"><font face="Times New Roman, Times, Serif" size="2">FIGURE 19-1 </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="2">聽Function</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">//******************************************************************<br />//聽Exponentiation聽program<br />//******************************************************************<br />#include聽&lt;iostream.h&gt;<br /><br />int聽Power(聽int,聽int聽);<br /><br />int聽main()<br />{<br />聽聽聽聽int聽number;聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Number聽that聽is聽being聽raised聽to聽power<br />聽聽聽聽int聽exponent;聽聽聽聽聽聽聽聽聽聽聽//聽Power聽the聽number聽is聽being聽raised聽to<br /><br />聽聽聽聽cin聽&gt;&gt;聽number聽&gt;&gt;聽exponent;<br />聽聽聽聽cout聽&lt;&lt;聽Power(number,聽exponent);聽</font><font face="Symbol" size="2">录</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">聽//聽Nonrecursive聽call<br />聽聽聽聽return聽0;<br />}<br /><br />//******************************************************************<br /><br />int聽Power(聽/*聽in聽*/聽int聽x,聽聽聽//聽Number聽that聽is聽being聽raised聽to聽power<br />聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽int聽n聽)聽聽//聽Power聽the聽number聽is聽being聽raised聽to<br /><br />//聽Computes聽x聽to聽the聽n聽power聽by聽multiplying聽x聽times聽the聽result聽of<br />//聽computing聽x聽to聽the聽n聽-聽1聽power.<br /><br />//聽Precondition:<br />//聽聽聽聽聽x聽is聽assigned聽&amp;&amp;聽n聽&gt;聽0<br />//聽Postcondition:<br />//聽聽聽聽聽Function聽value聽==聽x聽raised聽to聽the聽power聽n<br />//聽Note:<br />//聽聽聽聽聽Large聽exponents聽may聽result聽in聽integer聽overflow<br /><br />{<br />聽聽聽聽if聽(n聽==聽1)<br />聽聽聽聽聽聽聽聽return聽x;聽</font><font face="Symbol" size="2">录</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">聽//聽Base聽case<br />聽聽聽聽else<br />聽聽聽聽聽聽聽聽return聽x聽*聽Power聽(x,聽n聽-聽1);聽</font><font face="Symbol" size="2">录</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">聽//聽Recursive聽call<br />}</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">Each recursive call to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="3"> can be thought of as creating a completely new copy of the function, each with its own copies of the parameters </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3">. The value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3"> remains the same for each version of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="3">, but the value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> decreases by one for each call until it becomes 1.</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 trace the execution of this recursive function, with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">number</font><font face="Times New Roman, Times, Serif" size="3"> equal to 2 and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">exponent</font><font face="Times New Roman, Times, Serif" size="3"> equal to 3. We use a new format to trace recursive routines: we number the calls and then discuss what is happening in paragraph form.</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"><i>Call 1:</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="3"> is called by </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">main</font><font face="Times New Roman, Times, Serif" size="3">, with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">number</font><font face="Times New Roman, Times, Serif" size="3"> equal to 2 and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">exponent</font><font face="Times New Roman, Times, Serif" size="3"> equal to 3. Within </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="3">, the formal parameters </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> are initialized to 2 and 3, respectively. Because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">n</font><font face="Times New Roman, Times, Serif" size="3"> is not equal to 1, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Power</font><font face="Times New Roman, Times, Serif" size="3"> is called recursively with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">x</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_1121.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1122</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_1123.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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