📄 page_875.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_875</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_874.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_875</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_876.html">next page ></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 875</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"><i>The Print function:</i> The date is to be printed in the form month, day, comma, and year. We need a blank to separate the month and the day, and a comma followed by a blank to separate the day and the year. The month is to be printed in word form rather than as an integer. We set up a local data structure to store the names of the months: a 12-element array of strings containing the names of the months. Remembering that arrays are indexed starting at 0 in C++, we select the appropriate string by using the index [</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">mo-1</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">Print聽()</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="509" 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">Declare聽monthString聽to聽be聽a聽12-element聽array聽of聽strings聽and<br />聽聽initialize聽monthString[0]聽to聽January,聽monthString[1]<br />聽聽to聽February,聽and聽so聽forth<br />Print聽monthString[mo-1],聽,聽day,聽,聽,聽yr</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">When we implement the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function in C++, we want to be sure to declare </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">monthString</font><font face="Times New Roman, Times, Serif" size="3"> to be </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">static</font><font face="Times New Roman, Times, Serif" size="3">. By default, local variables in C++ are automatic variablesthat is, memory is allocated for them when the function begins execution and is deallocated when the function returns. By declaring </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">monthString</font><font face="Times New Roman, Times, Serif" size="3"> to be </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">static</font><font face="Times New Roman, Times, Serif" size="3">, the array is allocated once only, when the program begins execution, and remains allocated until the program terminates. From function call to function call, the computer does not waste time creating and destroying the array. The body of the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function appears as follows.</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 />聽聽聽聽static聽char聽monthString[12][10]聽=<br />聽聽聽聽{<br />聽聽聽聽聽聽聽聽聽January,聽February,聽March,聽April,聽May,聽June,<br />聽聽聽聽聽聽聽聽聽July,聽August,聽September,聽October,聽November,<br />聽聽聽聽聽聽聽聽聽December<br />聽聽聽聽};<br /><br />聽聽聽聽cout聽<<聽monthString[mo1聽]聽<聽聽聽<聽day聽<聽,聽聽<聽yr;<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">Testing: In testing the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function, we should print each month at least once. Both the year and the day should be tested at their end points and at several points between.</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>The</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ComparedTo</font><font face="Times New Roman, Times, Serif" size="3"> <i>function:</i> If we were to compare two dates in our heads, we would look first at the years. If the years were different, we would immediately determine which date came first. If the years were the same, we</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_874.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_875</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_876.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -