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

📄 page_268.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_268</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_267.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_268</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_269.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 268</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">when an odd number is read. The counter in this example is an event counter; it is initialized to 0 and incremented only when a certain event occurs. The counter in the previous example was an <i>iteration counter;</i> it was initialized to 1 and incremented during each iteration of the loop.</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" width="100%"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="96" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td>  <td colspan="3" height="12"></td>  <td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="96" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td></tr><tr><td colspan="3" height="2"><table cellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table></td></tr><tr><td></td>  <td><font face="Times New Roman, Times, Serif" size="2">Event Counter A variable that is incremented each time a particular event occurs.</font></td><td></td></tr><tr><td colspan="3" height="2"><table cellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table></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">Keeping Track of a Previous Value</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">Sometimes we want to remember the previous value of a variable. Suppose we want to write a program that counts the number of not-equal operators (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">!=</font><font face="Times New Roman, Times, Serif" size="3">) in a file that contains a C++ program. We can do so by simply counting the number of times an exclamation mark (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">!</font><font face="Times New Roman, Times, Serif" size="3">) followed by an equal sign (=) appears in the input. One way in which to do this is to read the input file one character at a time, keeping track of the two most recent characters, the current value and the previous value. In each iteration of the loop, a new current value is read and the old current value becomes the previous value. When EOF is reached, the loop is finished. Here's a program that counts not-equal operators this 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="Courier New, Courier, Mono New, Courier, Mono" size="2">//******************************************************************<br />//聽NotEqualCount聽program<br />//聽This聽program聽counts聽the聽occurrences聽of聽!=聽in聽a聽data聽file<br />//******************************************************************<br />#include聽&lt;iostream.h&gt;<br />#include聽&lt;fstream.h&gt;聽聽聽//聽For聽file聽I/O<br /><br />int聽main()<br />{<br />聽聽聽聽int聽聽聽聽聽聽count;聽聽聽聽聽聽聽聽//聽Number聽of聽!=聽operators<br />聽聽聽聽char聽聽聽聽prevChar;聽聽聽聽聽聽//聽Last聽character聽read<br />聽聽聽聽char聽聽聽聽聽currChar;聽聽聽聽聽//聽Character聽read聽in聽this聽loop聽iteration<br />聽聽聽聽ifstream聽inFile;聽聽聽聽聽聽聽//聽Data聽file<br /><br />聽聽聽聽inFile.open(myfile.dat);聽聽聽聽聽聽聽聽聽聽//聽Attempt聽to聽open聽input聽file<br />聽聽聽聽if聽(聽!inFile聽)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Was聽it聽opened?<br />聽聽聽聽{<br />聽聽聽聽聽聽聽聽cout聽&lt;&lt;聽**聽Can't聽open聽input聽file聽**聽//聽No--print聽message<br />聽聽聽聽聽聽聽聽聽聽聽聽聽&lt;聽endl;<br />聽聽聽聽聽聽聽聽return聽1;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Terminate聽program<br />聽聽聽聽}<br />聽聽聽聽count聽=聽0;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Initialize聽counter<br />聽聽聽聽inFile.get(prevChar);聽聽聽聽聽聽聽//聽Initialize聽previous聽value<br />聽聽聽聽inFile.get(currChar);聽聽聽聽聽聽聽//聽Initialize聽current聽value<br />聽聽聽聽while聽(inFile)聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽While聽previous聽input聽succeeded...<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_267.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_268</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_269.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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