📄 page_1042.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_1042</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_1041.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1042</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_1043.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 1042</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="17"></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">Programming Problems</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">1. In Chapter 12, the Exam program printed the names of those students taking an exam and the names of those students missing an exam. Parallel arrays were used because the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> data type had not yet been introduced. Rewrite the Exam program combining </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">isPresent</font><font face="Times New Roman, Times, Serif" size="3"> into a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> (type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentType</font><font face="Times New Roman, Times, Serif" size="3">) with two members: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">name</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">isPresent</font><font face="Times New Roman, Times, Serif" size="3">. Make these </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> dynamic variables rather than named variables; that is, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> should be an array of pointers to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StudentType</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">As in the SortWithPointers program of this chapter, the use of dynamically allocated </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> along with an array of pointers can save both execution time and memory space.</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">2. In Chapter 14, the MergeLists program merged the records from three input files onto a single master file containing no duplicate records. The program uses four arrays of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3">. The array sizes are fixed statically (at compile time), so memory is wasted when the input files are very small. Also, the program merges the data by copying entire </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> from one array to another, which requires considerable execution time when a great amount of copying occurs.</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></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">Rewrite the MergeLists program so that the input records are stored into dynamically allocated </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3">. The four arrays should be arrays of pointers to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3">. The benefits of this approach are increased memory efficiency (space is allocated only for as many </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</font><font face="Times New Roman, Times, Serif" size="3"> as there are records in the files) and increased time efficiency (copying pointers during the merge operation is faster than copying entire </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">structs</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">3. Given two arrays </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">a</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3">, we can define the relation </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">a</font><font face="Times New Roman, Times, Serif" size="3"> < </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3"> to mean </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">a</font><font face="Times New Roman, Times, Serif" size="3">[0] < </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3">[0] <i>and</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">a</font><font face="Times New Roman, Times, Serif" size="3">[1] < </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3">[1] <i>and</i> </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">a</font><font face="Times New Roman, Times, Serif" size="3">[2] < </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3">[2], and so forth. (If the two arrays are of different sizes, the relation is defined only through the size of the smaller array.) We can define the other relational operators likewise. Enhance this chapter's </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">DynArray</font><font face="Times New Roman, Times, Serif" size="3"> class by adding two Boolean member functions, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Equal</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"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="29" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -