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

📄 page_1095.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_1095</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_1094.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1095</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_1096.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 1095</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">herited from the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardPile</font><font face="Times New Roman, Times, Serif" size="3"> class. However, the private part declares a member function named </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Merge</font><font face="Times New Roman, Times, Serif" size="3">. This function is not accessible to clients of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardDeck</font><font face="Times New Roman, Times, Serif" size="3">. As we see shortly, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Merge</font><font face="Times New Roman, Times, Serif" size="3"> function is a helper function that is used by the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Shuffle</font><font face="Times New Roman, Times, Serif" size="3"> member 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="Times New Roman, Times, Serif" size="3">Now we implement the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardDeck</font><font face="Times New Roman, Times, Serif" size="3"> member functions. We begin with the class constructor.</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 class constructor CardDeck ()</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">When a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardDeck</font><font face="Times New Roman, Times, Serif" size="3"> class object is created, the constructor for its base class (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardPile</font><font face="Times New Roman, Times, Serif" size="3">) is implicitly executed first, creating an empty list. Starting with the empty list, we can generate the first cardthe ace of clubsand insert it into the list. The balance of the 52 cards can be generated in a loop. After every 13th card, we increment the suit and reset the rank to one.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table cellspacing="0" border="0" width="432" cellpadding="4"><tr><td valign="top"><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">Set聽tempCard.suit聽=聽CLUB<br />Set聽tempCard.rank聽=聽1<br />InsertTop(tempCard)聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Insert聽into聽deck<br />FOR聽count聽going聽from聽2聽through聽52<br />聽聽Increment聽tempCard.rank<br />聽聽IF聽tempCard.rank聽&gt;聽13<br />聽聽聽聽聽聽Increment聽tempCard.suit<br />聽聽聽聽聽聽Set聽tempCard.rank聽=聽1<br />聽聽InsertTop(tempCard)聽聽聽聽聽聽聽聽聽聽聽聽//聽Insert聽into聽deck</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></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">Although we don't use the rank of a card, we leave it there because we may want to print out the contents of the list during debugging. Also, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CardDeck</font><font face="Times New Roman, Times, Serif" size="3"> class may be used in other simulations. The class should be tested with a complete representation of a deck of cards.</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">Shuffle (In: numberOfShuffles)</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">When a human shuffles a deck of cards, he or she divides the deck into two nearly equal parts and then merges the two parts again. This process can be simulated directly (a simulation within a simulation). The list representing the deck can be divided into two lists, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">halfA</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">halfB</font><font face="Times New Roman, Times, Serif" size="3">. Then these two lists can be merged again. We use a random number generator to determine how many cards go into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">halfA</font><font face="Times New Roman, Times, Serif" size="3">. The rest go into </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">halfB</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">Through the header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">stdlib.h</font><font face="Times New Roman, Times, Serif" size="3">, the C++ standard library provides two functions for producing random numbers. The first, named </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">rand</font><font face="Times New Roman, Times, Serif" size="3">, has the following prototype:</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">int聽rand();</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_1094.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1095</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_1096.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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