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

📄 考研论坛 - 软件硕士(mse) - [推荐]数据结构(c++)习题解答(页 1) 简化版本.htm

📁 研究生硕士考题 数据结构C++部分 多道好题和答案
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                  //重载函数:复数四则运算<BR>&nbsp;&nbsp;complex&amp; operator – ( 
                  complex&amp; ob );<BR>&nbsp;&nbsp;complex&amp; operator * ( 
                  complex&amp; ob );<BR>&nbsp;&nbsp;complex&amp; operator / ( 
                  complex&amp; ob );<BR>&nbsp;&nbsp;friend ostream&amp; operator 
                  &lt;&lt; ( ostream&amp; os, complex&amp; c );&nbsp; &nbsp; 
                  &nbsp; &nbsp; 
                  //友元函数:重载&lt;&lt;<BR>private:<BR>&nbsp;&nbsp;double Re, 
                  Im;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; 
                  //复数的实部与虚部<BR>};<BR>#endif&nbsp;&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp; 
                  &nbsp; &nbsp; &nbsp; 
                  //复数类complex的相关服务的实现放在C++源文件complex.cpp中<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; #include &lt;iostream.h&gt;<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; #include &lt;math.h&gt;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; #include “complex.h”<BR>complex&amp; complex :: 
                  operator + ( complex &amp; ob ) {<BR>//重载函数:复数加法运算。<BR>complex 
                  * result = new complex ( Re + ob.Re,&nbsp;&nbsp;Im + ob.Im 
                  );<BR>return *result;<BR>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 
                  &nbsp; }<BR>complex&amp; complex :: operator – ( complex&amp; 
                  ob ) {<BR>//重载函数:复数减法运算<BR>&nbsp; &nbsp; &nbsp; &nbsp; complex 
                  * result = new complex ( Re – ob.Re,&nbsp;&nbsp;Im – ob.Im 
                  );<BR>&nbsp; &nbsp; &nbsp; &nbsp; return * 
                  result;<BR>}<BR>complex&amp; complex :: operator * ( 
                  complex&amp; ob ) {<BR>//重载函数:复数乘法运算<BR>complex * result = 
                  <BR>new complex ( Re * ob.Re – Im * ob.Im,&nbsp;&nbsp;Im * 
                  ob.Re + Re * ob.Im );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; return *result;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; }<BR>complex&amp; complex :: operator / ( complex&amp; 
                  ) {<BR>//重载函数:复数除法运算<BR>double d = ob.Re * ob.Re + ob.Im * 
                  ob.Im;<BR>complex * result = new complex ( ( Re * ob.Re + Im * 
                  ob.Im ) / d,<BR>&nbsp; &nbsp; &nbsp; &nbsp; ( Im * ob. Re – Re 
                  * ob.Im ) / d );<BR>return * result;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; }<BR>friend ostream&amp; operator &lt;&lt; ( 
                  ostream&amp; os, complex &amp; ob ) 
                  {<BR>//友元函数:重载&lt;&lt;,将复数ob输出到输出流对象os中。<BR>&nbsp;&nbsp;&nbsp; 
                  &nbsp; &nbsp; &nbsp; return os &lt;&lt; ob.Re &lt;&lt; ( ob.Im 
                  &gt;= 0.0 ) ? “+” : “-” &lt;&lt; fabs ( ob.Im ) &lt;&lt; 
                  “i”;<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>1-5 
                  用归纳法证明:<BR>&nbsp; &nbsp; &nbsp; &nbsp; (1) <BR><BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; (2)&nbsp; &nbsp;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; (3)&nbsp; &nbsp;<BR>【证明】略<BR><BR>1-6 什么是算法? 算法的5个特性是什么? 
                  试根据这些特性解释算法与程序的区别。<BR>【解答】<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  通常,定义算法为“为解决某一特定任务而规定的一个指令序列。”一个算法应当具有以下特性:<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; ① 
                  有输入。一个算法必须有0个或多个输入。它们是算法开始运算前给予算法的量。这些输入取自于特定的对象的集合。它们可以使用输入语句由外部提供,也可以使用赋值语句在算法内给定。<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; ② 
                  有输出。一个算法应有一个或多个输出,输出的量是算法计算的结果。<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  ③ 确定性。算法的每一步都应确切地、无歧义地定义。对于每一种情况,需要执行的动作都应严格地、清晰地规定。<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; ④ 有穷性。一个算法无论在什么情况下都应在执行有穷步后结束。<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; ⑤ 
                  有效性。算法中每一条运算都必须是足够基本的。就是说,它们原则上都能精确地执行,甚至人们仅用笔和纸做有限次运算就能完成。<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; 
                  算法和程序不同,程序可以不满足上述的特性(4)。例如,一个操作系统在用户未使用前一直处于“等待”的循环中,直到出现新的用户事件为止。这样的系统可以无休止地运行,直到系统停工。<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; 
                  此外,算法是面向功能的,通常用面向过程的方式描述;程序可以用面向对象方式搭建它的框架。<BR><BR>1-7 设n为正整数, 
                  分析下列各程序段中加下划线的语句的程序步数。<BR>&nbsp;&nbsp;(1)&nbsp;&nbsp;for (int 
                  i = 1; i &lt;= n; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; <BR>&nbsp; &nbsp; (2)&nbsp;&nbsp;x = 
                  0;&nbsp;&nbsp;y = 0;<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= n; j++) {&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;for (int 
                  i = 1; i &lt;= n; i++)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp;&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp; c[ i ][ j ] = 0.0;&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for 
                  (int j = 1; j &lt;= i; j++)<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for (int k = 1; k &lt;= n; 
                  k++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;for 
                  (int k = 1; k &lt;= j; k++)<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;c[ i ][ j ] = c[ 
                  i ][ j ] + a[ i ][ k ] * b[ k ][ j ];&nbsp;&nbsp;&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp; x = x + y;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;<BR>&nbsp;&nbsp;(3)&nbsp;&nbsp;int i = 1,&nbsp;&nbsp;j = 
                  1; &nbsp; &nbsp; &nbsp; &nbsp; 
                  <BR>&nbsp;&nbsp;(4)&nbsp;&nbsp;int i =1;<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp;&nbsp; &nbsp;while (i&lt;=n &amp;&amp; j&lt;=n) 
                  {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;do {<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;i = i + 1;&nbsp;&nbsp;j = j + 
                  i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for 
                  (int j = 1; j &lt;= n; j++) <BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp;&nbsp;i = i + j;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
                  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;} while ( i &lt; 100 + n 
                  );<BR><BR>【解答】<BR>&nbsp; &nbsp; &nbsp; &nbsp; (1) 
                  <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; (2) <BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; <BR><BR><BR><BR>(3) i = 1时,i = 2,j = j + i = 1 + 
                  2 = 2 + 1,<BR>i = 2时,i = 3,j = j + i = ( 2 + 1 ) + 3 = 3 + 1 + 
                  2,<BR>i = 3时,i = 4,j = j + i = ( 3 + 1 + 2 ) + 4 = 4 + 1 + 2 + 
                  3,<BR>i = 4时,i = 5,j = j + i = ( 4 + 1 + 2 + 3 ) + 5 = 5 + 1 + 
                  2 + 3 + 4,<BR>……<BR>i = k时,i = k + 1,j = j + i = ( k + 1 ) + ( 
                  1 + 2 + 3 + 4 + … + k ),<BR>解出满足上述不等式的k值,即为语句i = i + 
                  1的程序步数。<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  <BR>(4)&nbsp;&nbsp;<BR><BR><BR><BR><BR><BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp;&nbsp;&nbsp;一般地,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;求出满足此不等式的k值,即为语句i = i + 
                  j的程序步数。<BR><BR>1-8 
                  试编写一个函数计算n!*2n的值,结果存放于数组A[arraySize]的第n个数组元素中,0 &#61603; n &#61603; 
                  arraySize。若设计算机中允许的整数的最大值为maxInt,则当n &gt; arraySize或者对于某一个k (0 
                  &#61603; k &#61603; n),使得k!*2k &gt; 
                  maxInt时,应按出错处理。可有如下三种不同的出错处理方式:<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  (1) 用cerr&lt;&lt;及exit (1)语句来终止执行并报告错误;<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; (2) 用返回整数函数值0, 1来实现算法,以区别是正常返回还是错误返回;<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; (3) 
                  在函数的参数表设置一个引用型的整型变量来区别是正常返回还是某种错误返回。<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; 试讨论这三种方法各自的优缺点,并以你认为是最好的方式实现它。<BR>【解答】<BR>#include 
                  "iostream.h"<BR>#define arraySize 100<BR>#define MaxInt 
                  0x7fffffff<BR><BR>int calc ( int T[ ], int n ) {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; int i, value = 1;<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; if ( n != 0 ) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int edge = 
                  MaxInt / n / 2;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( i = 1; i &lt; 
                  n; i++ ) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  value *= i*2;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; if ( value &gt; edge ) return 0;<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value *= n * 2;<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T[n] = 
                  value;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; cout &lt;&lt; "A[" &lt;&lt; n &lt;&lt; "]=" &lt;&lt; 
                  T[n] &lt;&lt; endl;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; return 1;<BR>}<BR><BR>void main ( ) 
                  {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  int A[arraySize];<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; int i;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; for ( i = 0; i &lt; arraySize; i++ 
                  )<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; if ( !calc ( A, i ) ) {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; 
                  "failed at " &lt;&lt; i &lt;&lt; " ." &lt;&lt; endl;<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<BR>&nbsp; 

⌨️ 快捷键说明

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