首页 高级语言程序设计习题答案(免费)

高级语言程序设计习题答案(免费)

举报
开通vip

高级语言程序设计习题答案(免费)第1章  基本数据与表达式 1.1选择题 1.在一个C++程序中,main函数的位置(  c  )。 (a) 必须在程序的开头          (b) 必须在程序的后面 ( c ) 可以在程序的任何地方      (d) 必须在其它函数中间 2.用C++语言编制的源程序要变为目标程序必须要经过(  d  )。 (a) 解释      (b) 汇编        (c) 编辑      (d) 编译 3.C++程序基本单位是(  c  )。 (a) 数据      (b) 字符        (c) 函数     ...

高级语言程序设计习题答案(免费)
第1章  基本数据与表达式 1.1选择 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 1.在一个C++程序中,main函数的位置(  c  )。 (a) 必须在程序的开头          (b) 必须在程序的后面 ( c ) 可以在程序的任何地方      (d) 必须在其它函数中间 2.用C++语言编制的源程序要变为目标程序必须要经过(  d  )。 (a) 解释      (b) 汇编        (c) 编辑      (d) 编译 3.C++程序基本单位是(  c  )。 (a) 数据      (b) 字符        (c) 函数      (d) 语句 4. C++程序中的语句必须以(  b  )结束。 (a) 冒号      (b) 分号        (c) 空格      (d)花括号     5. 执行C++程序时出现的“溢出”错误属于(  c  )错误。 (a) 编译      (b) 连接        (c) 运行      (d) 逻辑 6.下列选项中,全部都是C++关键字的选项为(  c  )。 (a)  while  IF  static            (b)  break  char  go  (c)  sizeof  case  extern         (d)  switch  float  integer   7. 按C++标识符的语法规定,合法的标识符是( a ,c )。   (a) _abc      (b) new          (c)  int1      (d) “age” 8.下列选项中,(  a  )不是分隔符。 (a) ?        (b) ;          (c) :        (d) () 9.下列正确的八进制整型常量表示是(  b  )。 (a) 0a0      (b)  015        (c)  080      (d)  0x10 10.下列正确的十六进制整型常量表示是( a,b,d )。 (a) 0x11      (b)  0xaf        (c)  0xg        (d)  0x1f 11.在下列选项中,全部都合法的浮点型数据的选项为( a,b,d ),全部都不合法的浮点型数据选项是(  c  )。 (a)  -1e3 ,  15. , 2e-4        (b) 12.34 , -1e+5 , 0.0 (c) 0.2e-2.5 ,  e-5            (d) 5.0e-4 , 0.1 , 8.e+2 12.下列正确的字符常量为( b,d )。 (a)  " a "        (b) '\0'            (c) a        (d) '\101' 13.下列选项中,( a,b,c )能交换变量a和b的值。   (a) t=b ;b=a ;a=t;          (b) a=a+b ;b=a-b ;a=a-b;   (c) t=a ;a=b ;b=t;          (d) a=b ; b=a ; 14.执行语句 int i = 10,*p = &i; 后,下面描述错误的是(  a  )。 (a) p的值为10                    (b) p指向整型变量i (c) *p表示变量i的值            (d) p的值是变量i的地址 15.执行语句 int a = 5,b = 10,c;int *p1 = &a, *p2 = &b; 后,下面不正确的赋值语句是(  b  )。 (a) *p2 = b;                  (b) p1 = a; (c) p2 = p1;                  (d) c = *p1 *(*p2); 16.执行语句 int a = 10,b;int &pa = a,&pb = b; 后,下列正确的语句是(  b  )。 (a) &pb = a;                  (b) pb = pa; (c) &pb = &pa;                (d) *pb = *pa; 17.执行下面语句后,a和b的值分别为(  b  )。 int a = 5,b = 3,t; int &ra = a; int &rb = b;       t = ra;ra = rb;rb = t; (a) 3和3        (b) 3和5        (c) 5和3        (d) 5和5     18. 在下列运算符中,(  d  )优先级最高。 (a) <=            (b)*=            (c)+            (d)* 19. 在下列运算符中,(  d  )优先级最低。 (a) !        (b)&&        (c)!=        (d)?:   20.设i=1,j=2,则表达式i+++j的值为(  c  )。 (a) 1        (b)2          (c)3          (d)4     21.设i=1,j=2,则表达式 ++i+j的值为(  d  )。 (a)1          (b)2          (c)3          (d)4     22.在下列表达式选项中,(  c  )是正确。 (a)++(a++)    (b)a++b        (c)a+++b      (d)a++++b   23.已知i=0,j=1,k=2,则逻辑表达式 ++i||--j&&++k的值为(  b  )。 (a) 0            (b)1          (c)2          (d)3 24. 执行下列语句后,x的值是(  d  ),y的值是(  c  )。       int  x ,  y  ;       x = y = 1;  ++ x || ++ y  ; (a) 不确定        (b) 0              (c) 1                (d) 2 25.设X为整型变量,能正确表达数学关系1< X < 5的C++逻辑表达式是( b, c, d )。 (a) 1=5) 26. 已知x=5,则执行语句 x += x -= x*x ; 后,x的值为(  c  )。 (a)25            (b)40              (c)-40            (d)20 第7章 27. 设a=1,b=2,c=3,d=4,则条件表达式a=-100 && y<=-10 ) && !( y>=10 && y<=100 ) 6. sqrt(pow((x-10),2) + pow((y-20),2))< 35 7. a+b>c && b+c>a && c+a>b 8. (year%4 == 0) && (year%100!=0)||(year%400==0) 1.4 阅读下列程序,写出执行结果 1.#include       void main()     { int a = 1, b = 2, x, y;       cout << a++ + ++b << endl;       cout << a % b << endl;       x = !a>b;  y = x-- && b;       cout << x << endl;       cout << y << endl;       } 答案: 4 2       -1       0 2.#include       void main()       {         int x,y,z,f;         x = y = z = 1;         f = --x || y-- && z++;         cout << "x =" << x << endl;         cout << "y =" << y << endl;         cout << "z =" << z << endl;         cout << "f =" << f << endl;       }     答案: x = 0 y = 0 z = 2 f = 1 1.5编程题 1.输入一个三位整数,将它反向输出。 解答: #include     void main()     {  int x,i,j,k;       cout << "please input x:";       cin >> x;       i = x/100;       j = ( x - i*100 ) / 10;       k = x - i*100 - j*10;       cout << k << j << i << endl;     } 2.输入平面上某点横坐标x和纵坐标y,若该点在由图3.1表示的方块区域内,则输出1;否则,输出0。 图3.1 正方形 解答: #include     void main()     {  float x,y,b;       cout << "please input x,y:";       cin >> x >> y;       b = ( -2<=x ) && ( x<=2 ) && ( -2<=y ) && ( y<=2 );       cout << b << endl;     } 3.输入三个整数,求出其中最小数( 要求 对教师党员的评价套管和固井爆破片与爆破装置仓库管理基本要求三甲医院都需要复审吗 使用条件表达式)。      解答:略 第2章  程序控制结构 2.1选择题 1.已知 int i,x,y;在下列选项中错误的是(  c  )。 (a)  if(x == y)i++;            (b)  if(x = y)i--; (c)  if( xy )i--;            (d)  if( x+y )i++; 2.设有函数关系为y=  ,下面选项中能正确表示上述关系为(  c  )。 (a) y = 1;                          (b) y = -1; if( x>=0 )                          if( x!=0) if( x==0 )y=0;                    if( x>0 )y = 1; else  y = -1;                        else y = 0       (c) if( x<=0 )                      (d)  y = -1;             if( x<0 )y = -1;                  if( x<=0 )             else  y = 0;                          if( x<0 )y = -1;           else  y = 1;                            else y = 1; 3.假设i=2,执行下列语句后i的值为(  b  )。 switch(i) { case 1:i++;       case 2:i--;       case 3:++i;break;       case 4:--i;       default:i++; } (a) 1            (b) 2            (c) 3            (d) 4 4.已知int i=0,x=0;下面while语句执行时循环次数为(  d  )。 while(!x && i<3 ){ x++;i++;} (a) 4            (b) 3            (c) 2            (d) 1 5.已知int i=3;下面do_while 语句执行时循环次数为(  b  )。 do{ i--; cout<=0 ); (c) int a=5;while(a){ a--;}; (d) int i=3;for(;i;i--); 2.2阅读下列程序,写出执行结果     1. #include         void main()         {  int a,b,c,d,x;           a = c = 0; b = 1; d = 20;           if( a ) d = d-10;           else if( !b )             if( !c )                 x = 15;               else x = 25;           cout << d << endl;         }     答案: 20 2. #include         void main()         {  int a = 0, b = 1;           switch( a )           { case 0:  switch( b )                     { case 0 : cout << "a=" << a << " b=" << b << endl; break;                       case 1 : cout << "a=" << a << " b=" << b << endl; break;                     }             case 1:  a++; b++; cout << "a=" << a << " b=" << b << endl;           }         }     答案: a=0 b=1        a=1 b=2 3. #include         void main()       {  int i = 1;           while( i<=10 )             if( ++i % 3 != 1 )               continue;             else cout << i << endl;       } 答案: 4 7 10 4. #include         void main()         {  int i = 0 , j = 5;           do { i++; j--;               if ( i>3 ) break;             } while ( j>0 );           cout << "i=" << i << '\t '<< "j=" << j << endl;         } 答案: i=4 j=1 5. #include         void main()         {  int i,j;           for( i=1, j=5; i       void main()       {  int i, s = 0;           for( i=0; i<5; i++ )           switch( i )         {  case 0:  s += i;  break;             case 1:  s += i;  break;             case 2:  s += i;  break;             default: s += 2;         }         cout << "s=" << s <         void main()         {  int i, j, x = 0;           for( i=0; i<=3; i++ )             {  x++;                 for( j=0; j<=3; j++ )                   { if( j % 2 ) continue;                     x++; }                 x++;             }           cout << "x=" << x << endl;         }     答案: x=16 2.3编程题 1.输入某学生成绩,若成绩在85分以上输出“very good”,若成绩在60分到85分之间输出“good”,若成绩低于60分输出“no good”。     解答:略。 2.输入三个数,按从小到大的顺序输出。 解答: #include void main() {  int a, b, c, t;       cout << "a, b, c=";       cin >> a >> b >> c;       if( a>b ) { t = a; a = b; b = t; }       if( a>c ) { t = a; a = c; c = t; }       if( b>c ) { t = b; b = c; c = t; }       cout << a << '\t '<< b << '\t' << c << endl; } 3.输入三角形的三条边,判别它们能否形成三角形,若能,则判断是等边、等腰、还是一般三角形。 解答:略。 4.输入百分制成绩,并把它转换成五级分制,转换公式为: 解答:略。 5.编程序模拟剪刀、石头和纸游戏。游戏规则为:剪刀剪纸,石头砸剪刀,纸包石头。玩游戏者从键盘上输入s(表示剪刀)或r(表示石头)或p(表示纸),要求两个游戏者交替输入,计算机给出输赢的信息。 第7章 解答: 第8章 #include 第9章 void main() 第10章 { char first,second; 第11章   cout << "First input( s,r or p ):"; 第12章 cin >> first; 第13章   cout << "Second input( s,r or p ):"; 第14章   cin >> second; 第15章   switch  ( first ) 第16章   { case  's': 第17章           switch  ( second ) 第18章             { case  's': cout << "Scissor ties scissor." << endl; goto end;  第19章               case  'r': cout << "Scissor is crushed by rock." << endl; goto end; 第20章               case  'p': cout << "Scissor cuts paper." << endl; goto end;                  第21章               default :  cout << "second input error!" << endl ; goto end; 第22章               } 第23章   case  'r': 第24章             switch  ( second ) 第25章             { case  's': cout << "Rock crushes scissor." << endl; goto end;  第26章               case  'r': cout << "Rock ties rock." << endl; goto end; 第27章               case  'p': cout << "Rock is wrapped by paper." << endl; goto end;  第28章               default :  cout << "second input error!" << endl; goto end; 第29章               } 第30章   case  'p': 第31章             switch  ( second ) 第32章             { case  's': cout << "Paper is cut by scissor." << endl; goto end;  第33章               case  'r': cout << "Paper wraps the rock." << endl; goto end; 第34章               case  'p': cout << "Paper ties paper." << endl; goto end;  第35章               default :  cout << "second input error!" << endl; goto end; 第36章               } 第37章   default : cout << "First input error!" << endl; goto end; 第38章   } 第39章 end: ; 第40章 } 第41章 6.输入一个整数,输出该整数的所有素数因子。例如,输入120,输出为2、2、2、3和5。 解答: #include     void main()     {  int m,i = 2;       cout << "please input m:";       cin >> m;       while( i<=m )         if( m % i == 0 )       { cout << i << ",";         m = m / i;         }         else i++;     } 第42章 7.使用迭代公式编程序求某一正整数的平方根。 解答:略。 第43章 8.已知X=0,10,20,…,180,求sinx,cosx和tgx的值。 解答:略。 第44章 9.在100到200中找出同时满足用3除余2,用5除余3和用7除余2的所有整数。 解答: #include     void main()     {  int i;       for( i=100; i<=200; i++ )       { if ( ( i % 3 == 2) && ( i % 5 == 3 ) && ( i % 7 == 2 ) )             cout << i << endl;         }     } 第45章 10.求100到999中的水仙花数。所谓水仙花数是指一个三位数,它的每位数字的立方之和等于该数。例如,因为153=1+5+3,所以153为水仙花数。 解答:略。 第46章 11.求1000之内的所有完数。所谓完数是指一个数恰好等于它的所有因子之和。例如,因为6=1+2+3,所以6为完数。 解答: #include     void main()     { int i,j,s;       for( i=1; i<=1000; i++ )       { s = 0;         for( j=1; j     void main()     {  int i,j,k;       for( i=1; i<=5; i++ )       {  for( k=1; k<=5-i; k++ ) cout << " ";           for( j=1; j<=2*i-1; j++ ) cout << "*";           cout << endl;         }     } 第48章 13.已知XYZ+YZZ=532,其中X,Y和Z为数字,编一程序求出X,Y和Z。 解答:略。                     第3章  函    数 3.1  选择题 1.以下正确的函数原型为(  d  )。 (a) f( int x; int y );              (b) void f( x, y ); (c) void f( int x, y );          (d) void f( int, int ); 2.有函数原型 void fun1( int ); 下面选项中,不正确的调用是(  c  )。 (a) double x = 2.17 ; fun1( x ); (b) int a = 15 ;  fun1( a*3.14 ) ; (c) int b = 100 ; fun1( & b ); (d) fun1( 256 ); 3.有函数原型 void fun2( int * ); 下面选项中,正确的调用是(  c  )。 (a) double x = 2.17 ; fun2( &x ); (b) int a = 15 ;  fun2( a*3.14 ); (c) int b = 100 ; fun2( &b ); (d) fun2( 256 ); 4.有函数原型 void fun3( int & ); 下面选项中,正确的调用是(  c  )。 (a) int x = 2.17; fun3( &x ); (b) int a = 15;  fun3( a*3.14 ); (c) int b = 100;  fun3( b ); (d) fun3( 256 ) ; 5.有声明 int fun4( int ); int (*pf)(int) = fun4; 下面选项中,正确的调用是(  c  )。 (a) int a = 15 ;int n = fun4( &a ); (b) int a = 15; cout< #include int f( int ) ; void main() {  int i;     for( i = 0; i < 3; i ++ )         cout << f( i ) << endl; } int f( int a ) {  int b = 0 , c = 1;     b ++; c++;     return ( a + pow( b, 2 ) + c ); } 答案:   3   4   5 2. void func(int a, int b, int c = 3, int d = 4 ); #include void main() {  func( 10, 15, 20, 30 );     func( 10, 11, 12 );     func( 12, 12 ); } void func( int a, int b, int c, int d ) { cout << a << '\t' << b << '\t' << c << '\t' << d << endl; }     答案: 10      15      20      30 10      11      12      4 12      12      3        4 3. #include void func( int, int, int * ) ; void main() {  int x, y, z;     func( 5, 6, &x );     func( 7, x, &y );     func( x, y, &z );     cout << x << " ," << y << ", "<< z << endl; } void func( int a , int b , int *c ) { b += a ;  *c = b – a ; } 答案:   6,6,6 4. #include void func( int, int, int & ); void main() {  int x=0 , y=1 , z=2;     func( 1 , 2 , x );     func( x + y , y , y );     func( z , x + y , z );     cout << x << " ," << y << ", "<< z << endl ; } void func( int a , int b , int &c ) { b += a ;  c = b – a ; } 答案:   2,1,3 5. #include int f2( int, int ); int f1( int a , int b ) {  int c ;     a += a ; b += b ;     c = f2( a + b , ++ b );     return c; } int f2( int a , int b ) {  int c ;     c = b % 2 ;     return a + c; } void main() {  int a = 3 , b = 4;     cout << f1( a , b ) << endl; } 答案:   16 6. #include int age( int n ) {  int f;     if( n == 1 ) f = 10 ;     else f = age( n-1 ) + 2;     return f ; } void main() { cout << "age : " << age( 5 ) << endl; } 答案:   age:18 7. #include int f1( int a , int b ) { return a + b ; } int f2( int a ,int b ) { return a – b ; } int f3( int( *t )( int , int ) , int a , int b ) { return ( *t )( a, b ) ; } void main()   { int ( *p )( int, int );     p = f1 ; cout << f3( p, 4, 8 ) << endl;     p = f2 ; cout << f3( p, 8, 4 ) << endl; } 答案:   12   4 8. #include int sub( int, int ); int a = 1 ; void main()   { int m = 1, n = 2, f;     f = sub( m, n );     cout << a << '\t' << f << endl;     f = sub( m, n ) ;     cout << a << '\t' << f << endl;   } int sub( int c, int d )   {  static int m = 2, n = 5 ;     cout << m << '\t' << n << '\t' << endl;     a = ++ a ; c = m ++ ; d = n ++;     return c + d ;   } 答案:   2          5 第7章   2          7 第8章   3          6   3          9 3.3  编程题 1.已知  , 其中sh为双曲正弦函数,即。编一程序,输入x的值,求y的值。 解答:略。 2. 输入m、n和p的值,求s = 的值。注意判运算中的溢出。 解答:#include #include double f( long k,long num ); void main() { long m,n,p;   double s;   cout << "m,n,p=";   cin >> m >> n >> p;   s = ( f( 1,m ) + f( 3,n ) )/f( 5,p );   cout << "s=" << s << endl; } double f( long k,long num ) { long i;   double sum = 0;   for( i=1; i<=num; i++ )   { sum = sum + pow( i,k );   }   return sum; } #include #include double f( long k,long num ); void main() { long m,n,p;   double s,f1,f2,f3;   cout << "m,n,p=";   cin >> m >> n >> p;   f1=f( 1,m );  f2=f( 3,n ) ;  f3=f( 5,p );   if ( f1 && f2 && f3 )         { s = ( f1 + f2 ) / f3; cout << "s=" << s << endl; }   else cout<<"data over!\n" ; } double f( long k,long num ) { long i;   double sum = 0;   for( i=1; i<=num && sum void fmaxmin( float,float ,float ,float *,float * ); void main() { float a,b,c,max,min;       cout << "a,b,c = ";       cin >> a >> b >> c;       fmaxmin( a,b,c,&max,&min );       cout << "max=" << max << endl;       cout << "min=" < y ) { u = x; v = y; }     else { u = y; v = x; };   if ( z>u ) u = z;   if ( z int Rand(int,int);        //生成指定范围的随机数 void main() { int w,i,r,t;   char op;   int a,b,d;   while(1)         //游戏开始   { t = 0; cout<<"现在开始?( Y 或 N )\n" ;     cin>>answer;     if (answer=='N'||answer=='n') break; while(1)         { cout << "请输入难度( 1或2 ):";       cin >> w;       if ( w != 1 && w != 2 )             cout << "输入难度错误,重新输入!" << endl;           else break ;         }     while(1)           { cout << "请输入运算类型( +,-,*,/ ):" ;          cin >> op;         if ( op != '+' && op != '-' && op != '*' && op != '/' )               cout << "输入运算符错误,重新输入!" << endl;         else break;           }     //出10道题,每题10分     for( i=1; i<=10; i++ )           {  while(1)             { if( w == 1 ){ a = Rand(0,10); b = Rand(0,10); }                 else if( w == 2 ){ a = Rand(10,100); b = Rand(10,100); }                 if ( op == '-' )               if ( a=n);   return r; } 5.已知勒让德多项式为 编一程序,从键盘上输入x和n的值,使用递归函数求p(x)的值。 解答: 该程序可以由递归函数的形式直接编写。 #include float p( float x,int n ); void main() { int n; float x;   cout << "please input x and n:";   cin >> x >> n;   cout << "p(" << x << "," << n << ")=" << p( x,n ) <     void print( int w )     { for( int i = 1 ; i <= w ; i ++ )       { for( int j = 1 ; j <= i ; j ++ )           cout << i << " " ;         cout << endl ;       }     }     void main() { print( 5 ) ; } 运行显示: 1 2  2 3  3  3 4  4  4  4 5  5  5  5  5 解答: #include void print(int w) { int i;   if( w )     {   print( w-1 );       for( i=1; i<=w; i++ ) cout << w << "  ";       cout << endl;       } } void main() {   print( 5 ); } 7.已知用梯形法求积分的公式为:,其中h = ( b-a ) / n,n为积分区间的等分数,编程序求如下积分的值。要求把求积分公式编写成一个函数,并使用函数指针作为形式参数。调用该函数时,给定不同的被积函数作为实际参数求不同的积分。 ①       ②       ③ 解答: #include #include double f1( double x ) {  return 4 / ( 1 + x*x ); } double f2( double x ) {  return sqrt( 1 + x*x ); } double f3( double x ) {  return sin( x ); } double trap( double( *fun )( double x ), double a,double b,long n ) { double t,h;  int i;   t = ( ( *fun )(a) + ( *fun )( b ) ) / 2.0;   h = ( b – a ) / n;   for( i=1; i<=n-1; i++ ) t += ( *fun )( a + i * h );   t *= h;   return t; } void main() { double t1,t2,t3;   t1 = trap( f1,0,1,10000 );   cout << "t1=" << t1 << endl;   t2 = trap( f2,1,2,10000 );   cout << "t2=" << t2 << endl;   t3 = trap( sin,0,3.14159265/2,10000 );   cout << "t3=" << t3 << endl; } 8.编写一个程序,包含三个重载的display函数和一个主函数。要求第一个函数输出double值,前面用字符串“a double:”引导,第二个函数输出一个int值,前面用字符串“a int:”引导,第三个函数输出一个char字符值,前面用字符串“a char:”引导,在主函数中分别用double、int和char型变量作为实参调用display函数。 解答: #include void display( double  d ) {  cout << "a double:" << d << endl; } void display( int i ) {  cout << "a int:" << i << endl; } void display( char c ) {  cout << "a char:" << c << endl; } void main() { double d = 1.5; int i = 100; char c = 'a';   display( d );   display( i );   display( c ); } 9.使用重载函数编程序分别把两个数和三个数从大到小排列。 解答:略。 10.给定求组合数公式为:,编一程序,输入m和n的值,求的值。注意优化算法,降低溢出可能。要求主函数调用以下函数求组合数: int Fabricate( int m, int n ) ;        //返回的值 Fabricate函数内又须调用Multi函数: int Multi( int m, int n ) ;            // 返回 m × m-1 × … × n 程序由4个文件组成。头文件存放函数原型作为调用接口;其他3个cpp文件分别是main、Fabricate和Multi函数的定义。 解答: //Fabricate.h #ifndef FABRICATE_H   #define FABRICATE_H int Fabricate( int m,int n ); int Multi( int m, int n ); #endif //main.cpp #include #include "Fabricate.h" void main() { int m ,n;   cout << "input m and n:";   cin >> m >> n;   cout << "Fabricate(" << m << "," << n << ")=" << Fabricate( m, n ) << endl; } //Fabricate.cpp #include "Fabricate.h" int Fabricate( int m, int n ) {   return Multi( m, m –n + 1 ) / Multi( n, 1 ); } //Multi.cpp int Multi( int m, int n ) {int i, t = 1;   for( i=n; i<=m; i++ )   t = t * i;   return t; } 第4章  数    组 4.1  选择题 1.以下对一维数组 a 的正确定义是(  c  )。 (a) int n = 5, a[n];                (b) int a(5); (c) const int n = 5; int a[n];        (d) int n; cin>>n; int a[n];    2.下列数组定义语句中,不合法的是(  a  )。 (a) int a[3] = { 0, 1, 2, 3 };        (b) int a[] = { 0, 1, 2 }; (c) int a[3] = { 0, 1, 2 };        (d) int a[3] = { 0 }; 3.已知 int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, *p = a ;则不能表示数组 a 中元素的式子是(  c  )。 (a) *a              (b) *p                (c) a            (d) a[ p-a ] 4.已知 int a[] = { 0, 2, 4, 6, 8, 10 }, *p = a ; 值不等于0的表达式是(  b,d  )。 (a) *(p++)          (b) *(++p)            (c) *(p-- )        (d) *(--p) 5.以下不能对二维数组a进行正确初始化的语句是(  c  )。 (a) int a[2][3] = { 0 }; (b) int a[][3] = { { 0, 1 }, { 0 } }; (c) int a[2][3] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; (d) int a[][3] = { 0, 1, 2, 3, 4, 5 }; 6.已知int a[][3] = { { 0, 1 }, { 2, 3, 4 }, { 5, 6 }, { 7 } } ;则 a[2][1]的值是(  c  )。 (a) 0                (b) 2                (c) 6            (d) 7 7.已知int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; 则不能表示数组元素a[2][1]的地址是( a,b )。 (a) &[2][1]        (b) *(a[2]+1)        (c) a[2]+1            (d) *(a+2)+1 8.已知char *a[]={ "fortran", " basic", "pascal", "java", "c++" ; 则 cout<s2                            (b) strcmp(s1,s2)==0 (c) strcmp(s1,s2)>0                (d) strcmp(s2,s1)>0 4.2  阅读下列程序,写出执行结果 1.#include void main() { int i, conut=0, sum=0 ;     float average ;     int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;     for( i=0; i<10; i++ )       { if( a[i] % 2 == 0 ) continue ;         sum += a[ i ] ;         conut ++ ;       }     average = sum / conut ;     cout << "conut = " << conut << '\t' << "average = " << average << endl ; } 答案:   conut = 5      average = 5 2.#include void main() { int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;   int *p = a , sum = 0 ;   for( ; p void main() { char *str[] = { "c++", "basic", "pascal" } ;   char **p ;   int i ;       p = str ;         for( i=0 ; i<3 ; i++ )           cout << *( p+i ) << endl ;     } 答案:   c++   basic   pascal 8.#include void main() { char s1[] = "Fortran" , s2[] = "Foxpro" ;   char *p , *q ;   p = s1 ;  q = s2 ;   while( *p && *q )   { if ( *p == *q )     cout << *p ;     p ++ ;     q ++ ;   }   cout << endl ; } 答案:   For 9.#include #include void main() { char str[][10] = { "vb", "pascal", "c++" }, s[10] ;   strcpy( s , ( strcmp( str[0] , str[1] ) < 0 ? str[0] : str[1] ) ) ;   if( strcmp( str[2], s ) < 0 ) strcpy( s, str[2] ) ;   cout << s << endl ; } 答案:   C++ 4.3  编程题 1.已知求成绩的平均值和均方差公式:,, 其中n为学生人数,s为第i个学生成绩。求某班学生的平均成绩和均方差。 解答:略。 2.用随机函数产生10个互不相同的两位整数存放到一维数组中,并输出其中的素数。 解答: 程序定义了三个函数:函数createAry对数组元素赋值,函数outAry输出数组,以及函数isPrime判断素数。由主函数main调用它们,完成题目要求。 #include #include #include #include int isPrime(int); void createAry(int[], int); void outAry(const int[], int); void main() { int a[10]={0};     createAry(a,10);                   //对数组元赋值     outAry(a,10);                  //输出数组元素     for( int i=0; i<10; i++ )        //寻找数组中的素数       {  if (isPrime(a[i]))           cout << a[i] << "是素数!" << endl;       }   cout<<(isPrime(25))<=100 )             //取两位数的随机值           continue;         j=0;         while(j<=i && a[j]!=x )            //舍弃相同元素           j++;         if (j>i) a[i++]=x;                //不同元素送入数组       } } void outAry(const int a[], int n)        //输出数组元素 { cout<<"数组元素:"< void main() { int b[10], i, j, k, flag ;   long num, n ;   cout << "num=" ;  cin >> num;   k = 0;   n = num;   do              //拆分整数,把各数字放入数组b   { b[k++] = n % 10;     n = n/10;   } while( n != 0);   flag=1;            //判断标志   i=0; j=k-1;        //设置指示下标的指针   while(i void main() { char *weekday[7] = { "sunday", "monday", "tuesday",                     "wednesday", "thursday", "friday", "saturday" };   int d;   cout << "please input week day: ";   cin >> d;   if( d>=0 && d<=6 )cout << d << "---" << *( weekday + d ) < void create( int *&app, int n ); void del( int *&app, int *&bpp, int n ); int maindiagonal( int *&app, int n ); void output( int *&app, int ); void main() { int *ap = NULL, *bp = NULL, n;     cout << "输入矩阵的阶:";     cin >> n;     create( ap,n );     cout << "\n形成矩阵:\n";     output( ap, n );     cout << "去掉靠边元素生成的矩阵:\n";     del( ap,bp,n );     output( bp,n-2 );     cout << "主对角线元素之和:" << maindiagonal( ap, n ) < void insert( int a[],int,int ); void main() { int a[] = { 10, 12, 23, 25, 48, 48, 53, 58, 60, 78 };   int x,n,i;   cout << "please input insert data: ";   cin >> x;   n = sizeof(a)/sizeof(int);        //求数组长度   insert( a, n, x );            //插入元素   for( i=0; i=p; j-- )        //后移元素,挤出最大值           a[j] = a[j-1];         a[p] = x;                    //插入元素   } } 11.编写程序,按照指定长度生成动态数组,用随机数对数组元素赋值,然后逆置该数组元素。输出逆置前后的数组元素序列。 解答:就地逆置可以采用对称位置元素交换的 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 。请参阅第4题判回文算法。程序略。 12.把一个字符串插入到另一个字符串中指定位置。 解答:本题应该注意源串(被插串)的允许长度,考虑当源串定义的长度不够时的处理方法: (1)串截断。即仅保留源串允许长度的字符; (2)重新申请空间,保存插入后的字符串。 程序略。 13.把某班学生的姓名和学号分别存放到两个数组中,从键盘上输入某一学生学号,查找该学生是否在该班,若找到该学生,则显示出相应的姓名。     解答:略。 14.把一组C++关键字存放到一个二维数组中,找出这些关键字的最小者。 解答:需使用字符串处理函数。程序略。     15.使用指针函数编写程序把两个字符串连接起来。 解答:略 第5章 类与对象 5.1  选择题 第7章 1. 在下列结构变量的定义中,不正确的是(  d  )。 (a)  struct  employee {  char  name[ 20 ] ;         long  code ; } emp ; (b)  struct { char  name[ 20 ] ;         long  code ; } emp ; (c)  struct  employee { char name[20]; long  code; } ; employee  emp; (d)  struct { char  name[20];         long code; } employee; employee  emp; 2.已知有职工情况结构变量emp定义为: struct employee { char name[ 20 ] ; long  code ; struct { int  year ; int  month ;     int  day ; } birth ; } emp ; 下列对 emp 的 birth 正确赋值方法是(  d  )。 (a) year = 1980 ; month = 5 ; day = 1 ; (b) birth.year = 1980 ; birth.month = 5 ; birth.day = 1 ; (c) emp.year = 1980 ; emp.month = 5 ; emp.day = 1 ; (d) emp.birth.year = 1980 ; emp.birth.month = 5 ; emp.birth.day = 1 ; 3.假定有以下声明和定义,则下面引用形式错误的是(  b  )。 struct student { int num ; float score ; } stu[3] = {{1001,80},{1002,75},{1003,91}}, *p = stu ; (a) p->num          (b) (p++).num        (c) (p++)->num    (d) (*p).num 4.下列四个运算符中,优先级最低的是(  a  )。 (a) ++              (b) .                    (c) ->            (d) () 5.若有以下声明和定义,则下列错误的引用是(  d  )。 struct  worker { int  no ; char name[ 20 ] ; } w, *p = &w ; (a) w.no          (b) p->no              (c) (*p).no        (d) *p.no 6.若有以下声明和定义,则下列引用非法的是(  d  )。 struct data { int  n; float  score; data *q ; }; data a[3] = {1001,87,&a[1],1002,75,&a[2],1003,90,&a[0]}; data *p = a; (a) p->n++            (b) (*p).n++            (c) ++p->n        (d) *p->n 7.关于类和对象不正确的说法是(  c  )。 (a) 类是一种类型,它封装了数据和操作    (b) 对象是类的实例 (c) 一个类的对象只有一个                (d) 一个对象必属于某个类 8.在类定义的外部,可以被访问的成员有(  c  )。 (a) 所有类成员                            (b) private或protected的类成员 (c) public的类成员                        (d) public或private的类成员 9.关于this指针的说法错误的是( a,d )。 (a) this指针必须显式说明 (b) 当创建一个对象后,this指针就指向该对象 (c) 成员函数拥有this指针 (d) 静态成员函数拥有this指针 10.声明一个类的对象时,系统自动调用(  b,d  )函数;撤消对象时,系统自动调用(  c  )函数。 (a) 成员函数      (b) 构造函数            (c) 析构函数        (d) 复制构造函数 11.下面对构造函数的不正确描述是(  b  )。 第7章   (a) 系统可以提供默认的构造函数      (b) 构造函数可以有参数,所以也可以有返回值 (c) 构造函数可以重载 (d) 构造函数可以设置默认参数 12.下面对析构函数的正确描述是(  a,c  )。 (a) 系统可以提供默认的析构函数            (b) 析构函数必须由用户定义 (c) 析构函数没有参数                    (d) 析构函数可以设置默认参数 13.对静态成员的不正确描述是(  c,d  )。 (a) 静态成员不属于对象,是类的共享成员 (b) 静态数据成员要在类外定义和初始化 (c) 调用静态成员函数时要通过类或对象激活,所以静态成员函数拥有this指针 (d) 只有静态成员函数可以操作静态数据成员 14.下面选项中,不是类的成员函数为(  c  )。 (a) 构造函数        (b) 析构函数            (c) 友元函数        (d) 复制构造函数 15.下面对友员的错误描述是(  d  )。 (a) 关键字friend用于声明友员 (b) 一个类中的成员函数可以是另一个类的友员 (c) 友员函数访问对象的成员不受访问特性影响 (d) 友员函数通过this指针访问对象成员 5.2  阅读下列程序,写出执行结果 1. #include < iostream.h >         struct data         { int n ;           float score ;         } ;         void main()         { data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ;           cout << (p++)->n << endl ;           cout << (p++)->n << endl ;           cout << p->n++ << endl ;           cout << (*p).n++ << endl ;         } 答案:       1001       1002       1003       1004 2. #include < iostream.h >         struct employee         { char name[ 20 ] ;           char sex ;         } ;         void fun( employee *p )         {  if( (*p).sex == 'm' )             cout << (*p).name << endl ;         }         void main()         { employee emp[5] = { "Liming", 'm', "Wangxiaoping", 'f', "Luwei", 'm' } ;           int i ;           for( i=0; i<3; i++ )             fun( emp+i ) ;         }     答案: Liming Luwei 3. #include < iostream.h >         struct node         { char * s ;           node * q ;         } ;         void main()         { node a[ ] = { { "Mary", a+1 }, { "Jack", a+2 }, { "Jim", a } } ;           node *p = a ;           cout << p->s << endl ;           cout << p->q->s << endl ;           cout << p->q->q->s << endl ;           cout << p->q->q->q->s << endl ;         }     答案: Mary Jack Jim Mary 4. #include < iostream.h > class A { public : int f1(); int f2(); void setx( int m ) { x = m ; cout << x << endl; } void sety( int n ) { y = n ; cout << y << endl; } int getx() { return x ; } int gety() { return y ; } private : int x, y ; }; int A::f1() { return x + y ; }    int A::f2() { return x - y ; }    void main() { A a ; a.setx( 10 ) ;    a.sety( 5 ) ; cout << a.getx() << '\t' << a.gety() << endl ; cout << a.f1() << '\t' << a.f2() << endl ; } 答案: 10 5 10      5 15      5 5. #include < iostream.h > class T { public :         T( int x, int y )       { a = x ; b = y ;           cout << "调用构造函数1." << endl ;         cout << a << '\t' << b << endl ;         }     T( T &d )       { cout << "调用构造函数2." << endl ;         cout << d.a << '\t' << d.b << endl ;         }         ~T() { cout << "调用析构函数."< class T { public:         T(int x){ a=x; b+=x;};         static void display(T c)         { cout<<"a="< #include < math.h > class point { public :         point( float x, float y ) { a = x; b = y;  cout << "点( " << a << ", " << b << " )" ; }     friend double d( point &A , point &B )       { return sqrt((A.a-B.a) * (A.a-B.a) + (A.b-B.b) * (A.b-B.b)) ; } private:         double a, b ; }; void main() { point p1( 2, 3 ) ; cout << " 到 " ;       point p2( 4, 5 ) ;       cout << "的距离是:" << d( p1,p2 ) << endl ; } 答案: 点(3,4)到点(4,5)的距离是:2.82843 8. #include < iostream.h > class A { public :         A() { a = 5 ; }       void printa() { cout << "A:a = " << a << endl ; } private :       int a ;       friend  class B ; } ; class B { public:       void display1( A t ) { t.a ++ ; cout << "display1:a = " << t.a << endl ; } ;     void display2( A t ) { t.a -- ; cout << "display2:a = " << t.a << endl ; } ; }; void main() {  A obj1 ;       B obj2 ;       obj1.printa() ;       obj2.display1( obj1 ) ;       obj2.display2( obj1 ) ;       obj1.printa() ; } 答案: A:a = 5 display1:a = 6 display2:a = 4 A:a = 5 5.3  编程题 1.使用结构类型表示复数。设计程序输入两个复数,可以选择进行复数的+、-、×或÷运算,并输出结果。 解答: #include     #include     struct complex     {       float re,im;     };     void main()     { complex a,b,c; char oper;       cout << "输入复数a的实部和虚部: ";       cin >> a.re >> a.im;       cout << "输入复数b的实部和虚部:";       cin >> b.re >> b.im;       cout << "输入运算符: ";       cin >> oper;       switch ( oper )       {  case '+': c.re=a.re+b.re; c.im=a.im+b.im; break;           case '-': c.re=a.re-b.re; c.im=a.im-b.im; break;           case '*': c.re=a.re*b.re-a.im*b.im;                   c.im=a.im*b.re+a.re*b.im; break;           case '/': c.re=(a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im);                   c.im=(a.im*b.re-a.re*b.im)/(b.re*b.re+b.im*b.im);                   break;           default: cout << "input error!" << endl; return;       }   cout << "c=" << c.re;       cout << setiosflags( ios::showpos );       cout << c.im << "i" << endl;       return;     } 2.把一个班的学生姓名和成绩存放到一个结构数组中,寻找和输出最高分者。     解答:略。 3.使用结构表示X—Y平面直角坐标系上的点,程序顺序读入一个四边形的四个顶点坐标,判别由这四个顶点的连线构成的图形是否正方形、矩形或其它四边形。要求定义求两个点距离的函数使用结构参数。 解答: struct coordinates { double x,y; }; #include #include double distance( coordinates p1,coordinates p2 ) {  return sqrt( pow( p1.x-p2.x,2 )+pow( p1.y-p2.y,2 ) ); } void main() { int i;   coordinates p[5];   for( i=1; i<=4; i++ ) { cout << "输入第" << i << "个顶点的横坐标和纵坐标: ";     cin >> p[i].x >> p[i].y;     }   if(fabs( distance( p[1],p[2] ) - distance( p[3],p[4] ))<=1e-8     && fabs( distance( p[1],p[4] ) - distance( p[2],p[3] ))<=1e-8     && fabs( distance( p[1],p[3] ) - distance( p[2],p[4] ))<=1e-8)     if( fabs( distance( p[1],p[2] ) - distance( p[2],p[3] ))<1e-8 )       cout << "四个顶点构成的图形为正方形!" << endl;     else cout << "四个顶点构成的图形为矩形!" << endl;   else cout << "四个顶点构成的图形为其它四边形!" << endl; } 4.建立一个结点包括职工的编号、年龄和性别的单向链表,分别定义函数完成以下功能: (1)遍历该链表输出全部职工信息; (2)分别统计出男女性职工的人数; (3)在链表尾部插入新职工结点; (4)删除指定编号的职工结点; (5)删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,保存在另一个链表中。 主函数建立简单菜单选择,测试你的程序。 解答:略。 5. 输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并输出该链表中的字符。 解答: 为了形成反序链表,每读入一个字符都插入表头。 #include #include struct node { char ch;     node *next; }; void show( node *head ); void main() { node *head, *p;     char c;     head = NULL;     while( (c = getchar()) != '\n' )        //输入一行字符     { p = new node;            //建立新结点           p->ch = c;           p->next = head;        //插入表头           head=p;     }     show(head); } void show( node *head )        //输出链表 { node *p = head;     cout << "链表中的字符是: \n";     while( p )     { cout << p->ch;       p = p->next;     }     cout << endl; } 6. 定义一个Book(图 关于书的成语关于读书的排比句社区图书漂流公约怎么写关于读书的小报汉书pdf )类,在该类定义中包括 数据成员: bookname(书名)、price(价格)和number(存书数量); 成员函数: display()显示图书的情况;borrow()将存书数量减1,并显示当前存书数量;restore()将存书数量加1,并显示当前存书数量。 在main函数中,要求创建某一种图书对象,并对该图书进行简单的显示、借阅和归还管理。     解答: #include #include #include class Book { public:     void setBook(char*,float,int);         void borrow();         void restore();         void display();   private:         char bookname[40];         float price;         int number; }; //在类外定义Book类的成员函数 void Book::setBook(char *name, float pri, int num) { strcpy(bookname, name);   price=pri;   number=num; } void Book::borrow() { if (number==0 )         { cout << "已没存书,退出!" << endl;           abort();         }   number = number - 1;   cout << "借一次,现存书量为:" << number << endl; } void Book::restore() { number = number + 1;   cout << "还一次,现存书量为:" << number << endl; } void Book::display() { cout << "存书情况:" << endl       << "bookname:" << bookname << endl       << "price:" << price << endl       << "number:" << number << endl; } void main() {  char flag, ch;   Book computer;   computer.setBook( "c++程序设计基础" , 32, 1000 );   computer.display();   ch = 'y';   while ( ch == 'y' )   { cout << "请输入借阅或归还标志(b/r):";     cin >> flag;     switch ( flag )           { case 'b':  computer.borrow(); break;         case 'r':  computer.restore();           }         cout << "是否继续?(y/n)";         cin >> ch;   }   computer.display(); } 7. 定义一个Box(盒子)类,在该类定义中包括 数据成员: length(长)、width(宽)和height(高); 成员函数: 构造函数Box 设置盒子长、宽和高三个初始数据;函数volume 计算并输出盒子的体积。 在main函数中,要求创建Box对象,并求盒子的体积。 解答:略。 8. 定义一个Student类,在该类定义中包括:一个数据成员score(分数)及两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(float s) 用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。 在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。 解答: 类声明如下,实现代码略。 class student { public:         void scoretotalcount( float ) ;     static float sum();     static float average();   private:         float  score;         static float total;         static float count; }; float student::total = 0; float student::count = 0;     9. 定义一个表示点的结构类型Point和一个由直线方程y = ax + b确定的直线类Line。结构类型Point有x和y两个成员,它们分别表示点的横坐标和纵坐标。Line类有两个数据成员a和b,它们分别表示直线方程中的系数a和b。Line类有一个成员函数print用于显示直线方程;友员函数setPoint(Line &l1,Line &l2)用于求两条直线的交点。在main函数中,建立两个直线对象,分别调用print函数显示两条直线的方程,并调用函数setPoint求这两条直线的交点。 解答: 结构和类声明如下,实现代码略。 struct Point        //点结构 { float x; float y; }; class Line        //线类 { public:         line( float , float );        //构造函数         void print()                //显示直线方程     friend point setpoint(line &l1,line &l2);    //求两条直线交点 private:         float a, b; }; 第6章 运算符重载 6.1  选择题 1.在下列运算符中,能重载的是( a,c,d )。 (a) !            (b) sizeof            (c) new            (d)  delete 2. 在下列运算符中,不能重载的是(  c  )。 (a) <=            (b) >>                (c) &&            (d) &= 3.下列关于运算符重载的描述中,(  d  )是正确的。 (a) 可以改变参与运算的操作数个数    (b) 可以改变运算符原来的优先级 (c) 可以改变运算符原来的结合性     (d) 不能改变原运算符的语义 4.下列函数中,能重载运算符的函数是( b,c )。 (a) 成员函数  (b) 构造函数        (c) 析构函数  (d) 友员函数 5.不能用友员函数重载的是(  a  )。 (a) =            (b) ==                (c) +=            (d) != 6.下面描述中,错误的是(  b  )。 (a) 只有系统预先定义的运算符才可能被重载 (b) 使用类型转换函数不能把一个类转换为另一个类 (c) 使用类型转换函数可以把类转换为基本类型 (d) 类型转换函数只能定义为一个类的成员函数,不能定义为类的友员函数 6.2  阅读下列程序,写出执行结果   1.#include class T { public :         T() { a = 0; b = 0; c = 0; }         T( int i , int j , int k ) { a = i; b =j ; c = k; }           void get( int &i , int &j , int &k )           { i = a; j = b; k = c; }         T operator * ( T obj ); private:         int a , b , c; }; T  T::operator * ( T obj ) { T tempobj;       tempobj.a = a * obj.a;         tempobj.b = b * obj.b;       tempobj.c = c * obj.c;         return tempobj; } void main() {  T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3;       int a , b , c;       obj3 = obj1 * obj2;       obj3.get( a, b, c );       cout << "( obj1 * obj2 ): \t"             << "a = " << a << '\t' << "b = " << b<< '\t' << "c = " << c << '\t' << endl;       ( obj2 * obj3 ).get( a, b, c );       cout << "( obj2 * obj3 ): \t "             << "a = " << a << '\t' << "b = " << b << '\t' << "c = "<< c << '\t' << endl; } 答案:   ( obj1 * obj2 ):  a = 5      b = 10      c = 15   ( obj2 * obj3 ):  a = 25      b = 50      c = 75 2.#include < iostream.h > class vector { public:   vector(){ }       vector(int i,int j)         { x = i ; y = j ; }       friend vector operator + ( vector v1, vector v2 )       { vector tempvector ;         tempvector.x = v1.x + v2.x ;           tempvector.y = v1.y + v2.y ;           return  tempvector ;         }       void display()       { cout << "( " << x << ", " << y << ") "<< endl ; } private:       int x , y ; }; void main()         { vector v1( 1, 2 ), v2( 3, 4 ), v3 ;         cout << "v1 = " ;         v1.display() ;           cout << "v2 = " ;         v2.display() ;         v3 = v1 + v2 ;         cout << "v3 = v1 + v2 = " ;         v3.display() ;         } 答案:   v1 = ( 1, 2 )   v2 = ( 3, 4 )   v3 = v1 + v2 = ( 4, 6 ) 6.3  编程题 1.分别使用成员函数和友员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。 解答:略。 2.设向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),则它们之间的加、减和积分别定义为:   X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 )   X - Y = ( x1 - y1 , x2 - y2 , x3 - y3 )   X * Y =  x1 * y1 + x2 * y2 + x3 * y3 编程序对运算符“+”、“-”和“*”重载,实现向量之间的加、减、乘运算。 解答:略。 3. 定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。 解答: #include const double n = 1.852;    // 定义海里与千米和米的转换系数(1海里=1.852千米) class nauticalmile_kilometer { public:           nauticalmile_kilometer( int km,double m )       { kilometer = km;  meter = m; }           void print()       { cout<<"kilometer="< class rectangle { public:         rectangle( float l,float w )       { length = l;width = w; }         float area()       {  return( length*width ); }     float getlength() { return length; }     float getwidth() { return width; } private:     float length;         float width; }; class rectangular:public rectangle {  public:       rectangular( float l,float w,float h ) : rectangle( l,w )       { height = h; }       float getheight() { return height; }           float volume() { return area() *height; }   private:         float height; }; void main() {  rectangle obj1( 2,8 );       rectangular obj2( 3,4,5 );       cout << "length=" << obj1.getlength() << '\t' << "width=" << obj1.getwidth() << endl;       cout << "rectanglearea=" << obj1.area() << endl;   cout << "length=" << obj2.getlength() << '\t' << "width=" << obj2.getwidth();       cout << '\t'<< "height=" << obj2.getheight() << endl;       cout << "rectangularvolume=" << obj2.volume() << endl; } 2. 假设某销售公司有销售经理和销售员工,月工资的计算办法是:销售经理的固定月薪为8000元并提取销售额的千分之五作为工资;销售员工只提取销售额的千分之五作为工资。编一程序,定义一个基类employee,它包含三个数据成员number、name和salary,以及用于输入编号和姓名的构造函数。由employee类派生salesman类,再由salesman类派生salesmanager类。salesman类包含两个新数据成员 commrate和sales,还包含用于输入销售额并计算销售员工工资的成员函数pay()和用于输出的成员函数print()。salesmanager类包含新数据成员 monthlypay,及用于输入销售额并计算销售经理工资的成员函数pay()、用于输出的成员函数print()。在main函数中,测试你的类结构,求若干个不同员工的工资。 解答:略。 3.试写出所能想到的所有形状(包括二维的和三维的),生成一个形状层次类结构。生成的层次结构以Shape作为基类,并由此派生出TwoDimShape类和ThreeDimShape类。它们的派生类是不同形状类。定义层次结构中的每一个类,并用main函数进行测试。 解答:略 第8章  虚函数与多态性 8.1  选择题 1.在C++中,要实现动态联编,必须使用(  d  )调用虚函数。 (a) 类名            (b) 派生类指针      (c) 对象名          (d) 基类指针 2.下列函数中,可以作为虚函数的是( c,d )。 (a) 普通函数        (b) 构造函数          (c) 成员函数          (d) 析构函数 3.在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值(  b  )。 (a) 不同            (b) 相同              (c) 相容              (d) 部分相同 4.下面函数原型声明中,(  b  )声明了fun为纯虚函数。 (a) void fun()=0;                  (b)virtual void fun()=0; (c) virtual void fun();             (d)virtual void fun(){ }; 5.若一个类中含有纯虚函数,则该类称为(  d  )。 (a) 基类            (b) 纯基类          (c) 派生类          (d) 抽象类 6.假设 Aclass为抽象类,下列声明( a,c,d )是错误的。 (a) Aclass fun( int ) ;              (b)Aclass * p ; (c) int fun( Aclass ) ;              (d)Aclass Obj ; 7.下面描述中,正确的是( b,d )。 (a) 虚函数是没有实现的函数              (b) 纯虚函数的实现在派生类定义 (c) 抽象类是只有纯虚函数的类          (d) 抽象类指针可以指向不同的派生类   8.2 阅读下列程序,写出执行结果 1.#include class Bclass { public: Bclass( int i, int j ) { x = i; y = j; }       virtual int fun() { return 0 ; }   protected:       int x, y ; }; class Iclass:public Bclass { public :         Iclass(int i, int j, int k):Bclass(i, j) { z = k; }       int fun() { return ( x + y + z ) / 3; }     private :         int z ; }; void main() { Iclass obj( 2, 4, 10 );       Bclass p1 = obj;       cout << p1.fun() << endl;       Bclass & p2 = obj ;       cout << p2.fun() << endl;       cout << p2.Bclass :: fun() << endl;       Bclass *p3 = &obj;       cout << p3 -> fun() << endl; } 答案:   0   5   0   5 2.#include class BASE { public:         virtual void getxy( int i,int j = 0 ) { x = i; y = j; }       virtual void fun() = 0 ;     protected:         int x , y; } ; class A: public BASE { public: void fun()         { cout<<"x = "< 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 类          (b) 标准对象        (c) 标准函数          (d) 标准类模板 9.2  阅读下列程序,写出执行结果     1. #include template void fun( T &x, T &y ) { T temp;     temp = x;  x = y;  y = temp; } void main() { int i , j; i = 10; j = 20; fun( i, j ); cout << "i = " << i << '\t' << "j = " << j << endl; double a , b; a = 1.1; b = 2.2; fun( a, b );   cout << "a = " << a << '\t' << "b = " << b << endl; } 答案:   i=20    j=10   a=2.2    b=1.1 2. #include template class base { public:         base( T i , T j ) { x = i;  y = j; }         T sum() { return x + y; }     private:         T x , y; } ; void main() { base  obj2(3.3,5.5); cout << obj2.sum() << endl; base obj1(3,5); cout << obj1.sum() << endl; } 答案:   8.8   8 9.3  编程题 1.使用函数模板实现对不同类型数组求平均值的功能,并在main()函数中分别求一个整型数组和一个单精度浮点型数组的平均值。 解答:略。 2. 建立结点包括一个任意类型数据域和一个指针域的单向链表类模板。在main()函数中使用该类模板建立数据域为整型的单向链表,并把链表中的数据显示出来。 解答: #include template class List { public:     List( T x )  { data = x; }         void append( List *node )         { node->next = this;           next = 0;         }         List *getnext() { return next; }         T getdata() {return data; }   private:         T data; List *next; }; void main() { int i, idata, n, fdata;       cout << "输入结点的个数:";       cin >> n;       cout << "输入结点的数据域:";       cin >> fdata;       List headnode( fdata );       List *p, *last;       last = &headnode;       for( i=1; i> idata;           p = new list ( idata );           p->append( last );           last = p;         }       cout << "链表已经建立!" << endl;       cout << "链表中的数据为:" << endl;       p = &headnode;       while( p )   { cout << p->getdata() << endl;         p = p->getnext();       } } 3.学习使用STL编程。选择合适的组件,重做第5章习题5.3第4题,建立职工信息链表,并完成题中要求的各种操作。 解答:略 第10章  输入/输出流 10.1  选择题 1.下列类中( b,d )不是输入输出流类iostream的派生类。 (a)fstream          (b)ofstream      (c)strstream    (d)ostrstream 2.在下列选项中(  b,c,d  )是ostream类的对象。 (a)cin              (b)cerr          (c)clog            (d)cout 3.read函数的功能是从输入流中读取(  d  )。 (a)一个字符      (b)当前字符      (c)一行字符        (d)指定若干个字符 4.下列选项中,用于清除基数格式位设置以十六进制输出的语句是(  b  )。 (a) cout<>setf( ios::hex,ios::basefield); 5.下列格式控制符,在iostream.h中定义的是( a,d ),在iomanip.h中定义的是( b,c )。 (a)endl              (b)setfill          (c)setw            (d)oct 6.下列串流类,在strstream.h中定义的是(  b,d  ),在sstream.h中定义的是( a,c )。 (a)istringstream    (b)istrstream    (c)ostringstream    (d)ostrstream 7. 包含类fstream定义的头文件是(  a  )。 (a)fstream.h          (b)ofstream.h        (c)ifstream.h    (d)iostream.h 8.以写的方式打开文件 ” D:\file.dat”,错误的语句是( a,b,c,d )。 (a) ifstream infile( “D:\file.dat”, ios::in ); (b) ifstream infile( “D:\\file.dat”, ios::in); (c) ofstream infile( “D:\file.dat”, ios::out); (d) fstream infile( “D:\\file.dat”, ios::in|ios::out ); 9.假定已定义浮点型变量data,以二进制方式把data的值写入输出文件流对象outfile中去,正确的语句是(  c  )。 (a) outfile.write(( float* ) &data , sizeof( float )); (b) outfile.write(( float* ) &data , data ); (c) outfile.write(( char* ) &data , sizeof( float )); (d) outfile.write(( char* ) &data , data ); 10.2  阅读下列程序,写出执行结果   1. #include < iostream.h > void main() { double x = 123.456;     cout.width( 10 );     cout.setf( ios :: dec, ios :: basefield );     cout << x << endl;     cout.setf( ios :: left );        cout << x << endl;     cout.width( 15 );     cout.setf( ios::right , ios::left );     cout << x << endl;     cout.setf( ios::showpos );        cout << x << endl;     cout << -x << endl;     cout.setf( ios :: scientific );        cout << x << endl; } 答案:       123.456   123.456           123.456   +123.456   -123.456   +1.234560e+002 2. #include void main() { double x = 123.45678;   cout.width( 10 );   cout << ( "#" );   cout << x << endl;   cout.precision( 5 );      cout << x << endl;   cout.setf( ios::showpos );      cout << x << endl;   cout.setf( ios::scientific );      cout << x << endl; } 答案:               #123.457     123.46     +123.46 +1.23457e+002 3. #include #include void main() { double x = 123.456789;   cout << setiosflags( ios::fixed | ios::showpos ) << x < #include #include #include struct txrec {       char no[6];   char name[20];       char tel[9];       char postc[7];       char addr[30]; }; void main() {       struct txrec gzrec;  int i;       char filename[20], num[5];       fstream infile;   cout << "请输入通讯录文件名:";   cin >> filename ;          infile.open( filename, ios::in|ios::binary );      if ( !infile )     {     cerr << "文件不能打开!" << endl;     abort();     }   infile.seekg( 0,ios::end );       long posend = infile.tellp();       infile.seekg( 0,ios::beg );       cout << "请输入职工编号:" ;       cin >> num;       do       {         infile.read(( char * )&gzrec,sizeof( txrec ));       } while ( strcmp( gzrec.no,num ) !=0 && infile.tellp() != posend ); if ( strcmp( gzrec.no,num ) == 0 )       { cout << "该职工的记录找到了!" << endl;         cout << "编号:" << gzrec.no << endl;                cout << "姓名:"<< gzrec.name << endl;         cout << "电话号码:"<< gzrec.tel << endl;         cout << "邮政编码:" << gzrec.postc << endl; cout << "通信地址:" << gzrec.addr << endl;       }       else       {     cout << "该职工的记录找不到!" << endl;       }       infile.close(); } 6.设有两个按升序排列的二进制数据文件f和g,将它们合并生成一个新的升序二进制数据文件h中。 解答:函数mergeFile有三个字符串形式参数。f和g接受已存在的有序文件的实际文件名,h是合并之后数据存放的文件名。合并的方法是,以读方式打开f和g文件,以写方式打开h文件。比较f和g文件的当前元素,把较小者写入h文件。如此循环,直到开f或g文件其中一个被读完,然后把另一个文件的成语剩余数据顺序写入h文件。最后关闭所有文件。 void mergeFile(char * f, char * g, char * h) { int data1,data2;   fstream infile1,infile2,outfile;   infile1.open( f, ios::in|ios::binary);    //以读方式打开f文件   if ( !infile1 )         { cerr << "文件不能打开!" << endl ;       abort();         }   infile1.seekg(0,ios::end);                //f文件指针指向末尾位置   long posend1 = infile1.tellp();            //记录f文件尾位置   infile2.open( g, ios::in|ios::binary);    //以读方式打开g文件   if ( !infile2 )         { cerr << "文件不能打开!" << endl ;       abort();         }   infile2.seekg( 0,ios::end );            //g文件指针指向末尾位置   long posend2 = infile2.tellp();            //记录g文件尾位置   outfile.open( h, ios::out|ios::binary );        //以写方式打开h文件   infile1.seekg( 0,ios::beg );            //f文件指针指向开始位置   infile2.seekg( 0,ios::beg );            //g文件指针指向开始位置   while ( infile1.tellp() != posend1 && infile2.tellp() != posend2 )    //合并   { infile1.read(( char * )&data1,sizeof( int ));        //从f读一个数据         infile2.read(( char * )&data2,sizeof( int ));        //从g读一个数据         if( data1 int a[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int fun( int i ); void main() { int i ,s = 0;     for( i = 0; i <= 10; i++ )     { try       { s = s + fun( i ) ; }           catch( int )           { cout << "数组下标越界!" << endl; }       }       cout << "s = " << s << endl; } int fun( int i ) {  if ( i >= 10 )           throw i ;         return a[i] ; } 答案: 数组下标越界!   S=55 2.#include void f(); class T { public:         T() { cout << "constructor" << endl;           try { throw  "exception"; }           catch( char * )             { cout << "exception1" << endl; }           throw  "exception";         }         ~T() { cout << "destructor"; } }; void main() { cout << "main function " << endl;     try { f() ; }     catch( char * )       { cout << "exception2" << endl; }     cout << "main function " << endl;    } void f() {  T t;  } 答案:   main function   constructor   exception1   exception2   main function 11.2  编程题 1.从键盘上输入x和y的值,计算y = ln( 2x – y ) 的值,要求用异常处理“负数求对数”的情况。 答案: #include #include double f( double x,double y ); void main() { double x,y; try     { cout << "输入x和y的值:"; cin >> x >> y;           cout << f( x,y ) << endl;         } catch( char * )         { cout << "负数不能求对数!" << endl; } } double f( double x,double y ) { if( 2*x – y < 0 )          throw  "error";       else         return log( 2*x - y ); } 2.程序中典型的异常有:内存不足以满足new的请求、数组下标越界、运算溢出、除数为0、无效函数参数等。简单描述程序应该如何用异常处理的方法处理这些情况。 解答:略
本文档为【高级语言程序设计习题答案(免费)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_569018
暂无简介~
格式:doc
大小:380KB
软件:Word
页数:76
分类:生活休闲
上传时间:2017-09-20
浏览量:115