当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

清华大学:《数据结构及其应用》课程教学资源(试卷习题)试题1

资源类别:文库,文档格式:DOC,文档页数:4,文件大小:42KB,团购合买
1-4.什么是抽象数据类型?试用C++的类声明定义“复数”的抽象数据类型。要求 (1)在复数内部用浮点数定义它的实部和虚部。 (2)实现3个构造函数:缺省的构造函数没有参数;第二个构造函数将双精度浮点数 赋给复数的实部,虚部置为0;第三个构造函数将两个双精度浮点数分别赋给复数的实部 和虚部。
点击下载完整版文档(DOC)

1-4.什么是抽象数据类型?试用C++的类声明定义“复数”的抽象数据类型。要求 (1)在复数内部用浮点数定义它的实部和虚部。 (2)实现3个构造函数:缺省的构造函数没有参数:第二个构造函数将双精度浮点数赋 给复数的实部,虚部置为0;第三个构造函数将两个双精度浮点数分别赋给复数的实部和虚 (3)定义获取和修改复数的实部和虚部,以及+、-、*、等运算的成员函数。 (4)定义重载的流函数来输出一个复数 【解答】 抽象数据类型通常是指由用户定义,用以表示应用问题的数据模型。抽象数据类型由基 本的数据类型构成,并包括一组相关的服务。 ∥在头文件 complex. h中定义的复数类 #ifndef complex h #define complex h #include class comlex i complex (f Re= Im=0; ∥不带参数的构造函数 complex( double r)i Re ∥置实部的构造函数 complex( double r, dou ble i){Re=r;m=i;}分别置实部、虚部的构造函数 dou ble getReal(i return Re: j ∥取复数实部 double getlmag()i return Im; 3 取复数虚部 void setReal( double r)Re =r;) ∥修改复数实部 void setlmag( double i)i Im=i:) ∥修改复数虚部 or=( complex& ob)i Re=ob. R oblm;}∥复数赋值 「重载函数:复数四则运算 lex& ob ) friend ostream& operator<<( ostream&os, complex&c片;∥友元函数:重载<< private double Re.m ∥复数的实部与虚部 }; ∥复数类 complex的相关服务的实现放在C++源文件 complex. cpp中 #include <iostream h complex& complex operator+( complex ob)i ∥重载函数:复数加法运算。 comp esult=new complex( Re + ob. Re, Im +ob. Im )

1-4.什么是抽象数据类型?试用 C++的类声明定义“复数”的抽象数据类型。要求 (1) 在复数内部用浮点数定义它的实部和虚部。 (2) 实现 3 个构造函数:缺省的构造函数没有参数;第二个构造函数将双精度浮点数赋 给复数的实部,虚部置为 0;第三个构造函数将两个双精度浮点数分别赋给复数的实部和虚 部。 (3) 定义获取和修改复数的实部和虚部,以及+、-、*、/等运算的成员函数。 (4) 定义重载的流函数来输出一个复数。 【解答】 抽象数据类型通常是指由用户定义,用以表示应用问题的数据模型。抽象数据类型由基 本的数据类型构成,并包括一组相关的服务。 //在头文件 complex.h 中定义的复数类 #ifndef _complex_h_ #define _complex_h_ #include class comlex { public: complex ( ){ Re = Im = 0; } //不带参数的构造函数 complex ( double r) { Re = r; Im = 0; } //只置实部的构造函数 complex ( double r, double i ) { Re = r; Im = i; } //分别置实部、虚部的构造函数 double getReal ( ) { return Re; } //取复数实部 double getImag ( ) { return Im; } //取复数虚部 void setReal ( double r ) { Re = r; } //修改复数实部 void setImag ( double i ) { Im = i; } //修改复数虚部 complex& operator = ( complex& ob) { Re = ob.Re; Im = ob.Im; } //复数赋值 complex& operator + ( complex& ob ); //重载函数:复数四则运算 complex& operator – ( complex& ob ); complex& operator * ( complex& ob ); complex& operator / ( complex& ob ); friend ostream& operator #include #include “complex.h” complex& complex :: operator + ( complex & ob ) { //重载函数:复数加法运算。 complex * result = new complex ( Re + ob.Re, Im + ob.Im );

return· result omplex& complex operator-( complex& ob)t ∥重载函数:复数减法运算 complex result= new complex( Re-ob. Re, Im-ob. Im ) complex& complex operator ' complex& ob)( ∥重载函数:复数乘法运算 return· result; complex& complex operator / complex&)t ∥重载函数:复数除法运算 double d=ob. Re *ob.Re +ob. Im *ob. Im: complex result= new complex(( Re *ob. Re +Im*ob. Im )/d, Im"ob. Re-Re *ob. Im)/d); return·resl; friend ostream& operator =0.0)?“+”:“-” arraySize或者对于某 个k(0≤k≤m),使得k*2k>muxm时,应按出错处理。可有如下三种不同的出错处理方 式: (1)用cerx<及exit(1)语句来终止执行并报告错误 (2)用返回整数函数值0,1来实现算法,以区别是正常返回还是错误返回 (3)在函数的参数表设置一个引用型的整型变量来区别是正常返回还是某种错误返回。 试讨论这三种方法各自的优缺点,并以你认为是最好的方式实现它 【解答】 #include"iostream.h" #define array Sie 100 #define MaxInt Ox7fffffff int calc( int 7 int n)i if(n=0)t for(i=1;i<n;汁+){ 2

return *result; } complex& complex :: operator – ( complex& ob ) { //重载函数:复数减法运算 complex * result = new complex ( Re – ob.Re, Im – ob.Im ); return * result; } complex& complex :: operator * ( complex& ob ) { //重载函数:复数乘法运算 complex * result = new complex ( Re * ob.Re – Im * ob.Im, Im * ob.Re + Re * ob.Im ); return *result; } complex& complex :: operator / ( complex& ) { //重载函数:复数除法运算 double d = ob.Re * ob.Re + ob.Im * ob.Im; complex * result = new complex ( ( Re * ob.Re + Im * ob.Im ) / d, ( Im * ob. Re – Re * ob.Im ) / d ); return * result; } friend ostream& operator = 0.0 ) ? “+” : “-” arraySize 或者对于某 一个 k (0  k  n),使得 k!*2k > maxInt 时,应按出错处理。可有如下三种不同的出错处理方 式: (1) 用 cerr<<及 exit (1)语句来终止执行并报告错误; (2) 用返回整数函数值 0, 1 来实现算法,以区别是正常返回还是错误返回; (3) 在函数的参数表设置一个引用型的整型变量来区别是正常返回还是某种错误返回。 试讨论这三种方法各自的优缺点,并以你认为是最好的方式实现它。 【解答】 #include "iostream.h" #define arraySize 100 #define MaxInt 0x7fffffff int calc ( int T[ ], int n ) { int i, value = 1; if ( n != 0 ) { int edge = MaxInt / n / 2; for ( i = 1; i < n; i++ ) { value *= i*2;

if value >edge )return 0; cout<<"Al<<n<<F <<7n<<endl; void main (t int Aarray Sie]; 汁+) cout <<"failed at"<<i<<""<<endI 1-9(1)在下面所给函数的适当地方插入计算cumn的语句: void d(ArrayElement x[, int n)& i while (i<=n) while(i<=(n/2)) (2)将由(1)所得到的程序化简。使得化简后的程序与化简前的程序具有相同的coum值 (3)程序执行结束时的coun值是多少? (4)使用执行频度的方法计算这个程序的程序步数,画出程序步数统计表 【解答】 (1)在适当的地方插入计算 count语句 void d( array Element x[, int n)t int i=1: i+=2: count ++ count + 对whle语句 I while(i<=n);

if ( value > edge ) return 0; } value *= n * 2; } T[n] = value; cout << "A[" << n << "]=" << T[n] << endl; return 1; } void main ( ) { int A[arraySize]; int i; for ( i = 0; i < arraySize; i++ ) if ( !calc ( A, i ) ) { cout << "failed at " << i << " ." << endl; break; } } 1-9 (1) 在下面所给函数的适当地方插入计算 count 的语句: void d (ArrayElement x[ ], int n ) { int i = 1; do { x[i] += 2; i +=2; } while (i <= n ); ; i = 1; while ( i <= (n/2) ) { x[i] += x[i+1]; i++; } } (2) 将由(1)所得到的程序化简。使得化简后的程序与化简前的程序具有相同的 count 值。 (3) 程序执行结束时的 count 值是多少? (4) 使用执行频度的方法计算这个程序的程序步数,画出程序步数统计表。 【解答】 (1) 在适当的地方插入计算 count 语句 void d ( ArrayElement x [ ], int n ) { int i = 1; count ++; do { x[i] += 2; count ++; i += 2; count ++; count ++; //针对 while 语句 } while ( i <= n ); i = 1;

while(i<=(n/2))i count + 针对 while语句 count +t count ++ ∥针对最后一次whle语句 (2)将由(1)所得到的程序化简。化简后的程序与原来的程序有相同的 count值: oid d( ArrayElement x[ int n)t com+=3;i+=2; while(i<=(n/2))i ount+=3; i++; (3)程序执行结束后的cowm值为3n+3 当n为偶数时,comn=3*(n/2)+3*(n/2)+3=3*n+3 当n为奇数时, count=3*(n+1)/2)+3*(n-1)/2)+3=3*n+3 (4)使用执行频度的方法计算程序的执行步数,画出程序步数统计表 次执行步数执行频度程序步数 1 void d( ArrayElement x[l, int n )i x+=2; (n+1)2」L(n+1)2 Ln2+l」 x0+=xi+1: i++

count ++; while ( i <= ( n / 2 ) ) { count ++; //针对 while 语句 x[i] += x[i+1]; count ++; i ++; count ++; } count ++; //针对最后一次 while 语句 } (2) 将由(1)所得到的程序化简。化简后的程序与原来的程序有相同的 count 值: void d ( ArrayElement x [ ], int n ) { int i = 1; do { count += 3; i += 2; } while ( i <= n ); i = 1; while ( i <= ( n / 2 ) ) { count += 3; i ++; } count += 3; } (3) 程序执行结束后的 count 值为 3n + 3。 当 n 为偶数时,count = 3 * ( n / 2 ) + 3 * ( n / 2 ) + 3 = 3 * n + 3 当 n 为奇数时,count = 3 * ( ( n + 1 ) / 2 ) + 3 * ( ( n – 1 ) / 2 ) + 3 = 3 * n + 3 (4) 使用执行频度的方法计算程序的执行步数,画出程序步数统计表: 行 号 程 序 语 句 一次执行步数 执行频度 程序步数 1 2 3 4 5 6 7 8 9 10 11 12 void d ( ArrayElement x [ ], int n ) { int i = 1; do { x[i] += 2; i += 2; } while ( i <= n ); i = 1; while ( i <= ( n / 2 ) ) { x[i] += x[i+1]; i ++; } } 0 1 0 1 1 1 1 1 1 1 0 0 1 1 (n+1)/2 (n+1)/2 (n+1)/2 (n+1)/2 1 n/2+1 n/2 n/2 n/2 1 0 1 0 (n+1)/2 (n+1)/2 (n+1)/2 1 n/2+1 n/2 n/2 0 0 ( n  0 ) 3n + 3

点击下载完整版文档(DOC)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
已到末页,全文结束
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有