C++编写程序控制小灯闪烁。在一个从大到小的一维数组()中插入一个整数并且仍然保持由小到大的顺序。

当前位置: >>
C++程序设计基础(第4版)(上)习题解答(1)
《C++程序设计基础》 (第 4 版) (上) 习题与解答第 1 章基本数据与表达式1.1 选择题1.一个最简单的 C++程序,可以只有一个(C) 。 (A)库函数 (A)解释 (A)冒号 (A)x=a/x; (A)编译 (A)while IF (B)自定义函数 (B)汇编 (B)分号 (B)x=x/a; (B)连接 Static extern (C)运行 (C)main 函数 (C)编辑 (C)空格 (C)a=a%x; (D)逻辑 (B)break (D)switch char float go integer (D)&age& (D)+ (D)0x10 (D)0x1f ?1e+5 0.1 0.1E?12 8e+2 (D)x=x?a; (D)空函数 (D)编译 (D)花括号 2.用 C++语言编写的源程序要成为目标程序必须要经过(D) 。 3.C++程序中的简单语句必须以(B)结束。 4.假设有说明 int a=0; double x=5.16;,则在以下语句中, (C)属于编译错误。 5.执行 C++程序时出现的D溢出‖错误属于(C)错误。 6.在下列选项中,全部都是 C++关键字的选项为(C) 。 (C)sizeof case7.按 C++标识符的语法规定,合法的标识符是(A) 。 (A)_abc (B)new (C)π 8.在 C++语句中,两个标识符之间(A)不能 作为 C++的分隔符。 .. (A)数字 (A)0a0 (A)0x11 (A)?1e3.5 (C)0.2e?2 15. (B); (B)015 (B)0xaf 2e?4 e?5 (C): (C)080 (C)0xg (B)12.34 9.下列正确的八进制整型常量表示是(B) 。 10.下列错误的十六进制整型常量表示是(C) 。 11.在下列选项中,全部都合法的浮点型数据的选项为(B) 。 ?12345. (D)5.0e(1+4)12.在下列选项中,正确的字符常量为(D) 。 (A)&a& (B)'name' (C)a 13.在下列选项中, (D)不能 交换变量 a 和 b 的值。 .. (A)t=b; (C)t=a; b=a; a=b; a=t; b=t; (B)a=a+b; (D)a=b; b=a-b; b=a; (D)'\101' a=aCb;14.关于下列语句,叙述错误的是(A) 。 int i=10,?p=&i; (A)p 的值为 10 (B)p 指向整型变量 i (C)?p 表示变量 i 的值 (D)p 的值是变量 i 的地址 15.有以下变量说明,下面不正确 的赋值语句是( B) 。 ... int a=5, b=10, int ?p1 = &a, ?p2 = &b; (A)?p2 = (C)p2 = p1; (B)p1 = (D)c = ?p1 ?(?p2);16.有以下变量说明,下面正确的语句是(B) 。 int a=10, int &pa=a, &pb=b; (A)&pb = (B)pb = (C)pb = & (D)?pb = ?17.执行下面语句序列后,a 和 b 的值分别为(B) 。 int a=5, b=3, int &ra=a; int &rb=b; t= ra= rb=t; (A)3 和 3 (A)&= (A)! (A)1 (A)1 (A)++(a++) (A)0 (B)3 和 5 (B)?= (B)&& (B)2 (B)2 (B)a++b (B)1 (C)5 和 3 (C)+ (C)!= (C)3 (C)3 (C)a+++b (C)2 (D)5 和 5 (D)? (D)? : (D)4 (D)4 (D)a++++b (D)318.在下列运算符中, ( D)优先级最高。 19.在下列运算符中, (D)优先级最低。 20.已知 int i=1, j=2;,则表达式 i+++j 的值为(C) 。 21.已知 int i=1, j=2;,则表达式 ++i+j 的值为(D) 。 22.在下列表达式选项中, (C)是正确。 23.已知 int i=0, j=1, k=2;,则逻辑表达式 ++i||? ?j&&++k 的值为(B) 。 24.执行下列语句后,x 的值是(D) ,y 的值是() 。 int x, x=y=1; ++x || ++y; (A)不确定 (B)0 (C)1 (D)2 25.设 x 为整型变量,不能 正确表达数学关系 1 < x < 5 的 C++ 逻辑表达式是( A ) 。 .. (A)1& x &5 (C)1&x && x&5 x+=x-=x?x; (A)25 (B)x==2||x==3||x==4 (D)! (x&=1)&&! (x&=5)26.已知 int x=5;,执行下列语句后,x 的值为(C) 。 (B)40 (C)C40 (D)2027.设 int a=1, b=2, c=3, d=4;,则以下条件表达式的值为(A) 。 a&b?a:c&d?c:d (A)1 (x=4?5, x?5), x+25 (A)25 (B)2 (C)3 (D)428.以下逗号表达式的值为(D) 。 (B)20 (C)100 (D)45 1.2 根据下列数学表达式写出 C++算术表达式1 1? 1? 1 1 x? y1.2.x{x[x(ax+b)+c]+d}+e10 ? a?b ? ? 3.ln ?1 ? ? a?b ? ? ?? 4. 1 ? cos 48° 2? 1 ? x2 ? 5.cot ? ? 1 ? x2 ? ? ? ?【解答】6.lg(a2+ab+b2)1. 1/(1 + 1/(1 + 1/(x + y))) 2. x * ( x * ( x * ( a * x + b ) + c ) + d ) + e 3. log( 1 + pow( fabs( ( a + b )/( a C b ) ),10) 4. sqrt( 1 + 3.14159/2 * cos( 48 * 3. ) ) 5. 1/tan( ( 1 - x*x )/( 1 + x*x)) 或者 cos( ( 1 - x*x )/( 1 + x*x ) )/sin( ( 1 - x*x )/( 1 + x*x ) ) 6. log10( a * a + a * b + b * b )1.3 用逻辑表达式表示下列条件1.i 被 j 整除 3.1≤x<10 5.y ? [C100,C10],并且 y ? [10,100] 6.坐标点(x, y)落在以(10, 20)为圆心,以 35 为半径的圆内 7.三条边 a,b 和 c 构成三角形 8.年份 Year 能被 4 整除,但不能被 100 整除,或者能被 400 整除 【解答】 1. i%j == 0 3. 1&=x && x&10 2.(n&k)&&(n%2 == 0) 4. x&z||y&z 2.n 是小于正整数 k 的偶数 4.x、y 其中有一个小于 z5. !( y&=-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 &iostream& int main() { int a = 1, b = 2; bool x, cout && (a++)+(++b) && cout && a % b && x = !a&b; y = a-- && cout && x && cout && y && } 【解答】 4 2 0 1 2. #include &iostream& int main() { int x,y,z,f; x = y = z = 1; f = --x || y-- && z++; cout &&&x = &&& x && cout &&&y = &&& y && cout &&&z = &&& z && cout &&&f = &&& f && } 【解答】 x=0 y=0 z=2 f=1 3. #include &iostream& #include&iomanip& int main() { int a=123; int &ra=a; int ?pa=&a; cout&&setw(5)&&dec&&a&&setw(5)&&oct&&ra&&setw(5)&&hex&&?pa&& } 【解答】 123 173 7b1.5 思考题1.什么是数据类型?变量的类型定义有什么作用? 【解答】 数据D类型‖是对数据的抽象。类型相同的数据有相同的表示形式、存储格式以及相关的操作。定义一 个变量时,计算机根据变量的类型分配存储空间,并以该类型解释存放的数据。 2.普通数据类型变量和指针类型变量的定义、存储和使用方式有何区别?请编写一个程序验证之。 【解答】变 量 类 型 数 指 据 针 定 义 存 储 数据值 地址值 使 用 方 式 类 型 标 识 符 类型 * 标识符 通过名访问即直接访问对变量内容操作 通过指针变量的地址值间址访问对象验证程序: #include&iostream& int main() { int a,b,c; cout&&&a,b,c= &; cin&&a&&b&&c; //对普通数据类型变量赋值 //用变量地址值初始化指针变量 /名访问,输出a,b,c的值 //输出指针变量的地址值 int *pa=&a,*pb=&b,*pc=&c; cout&&&a,b,c= &&&a&&&, &&&b&&&, &&&c&& cout&&&pa,pb,pc= &&&pa&&&, &&&pb&&&, &&&pc&& //间址访问,输出pa,pb,pc指向的变量的赋值 cout&&&*pa,*pb,*pc= &&&*pa&&&, &&&*pb&&&, &&&*pc&& } 3.什么是数据对象的引用?对象的引用和对象的指针有什么区别?请用一个验证程序说明之。 【解答】 引用是为数据对象定义别名。引用与指针有以下几点区别: (1)引用名不是内存变量,而指针变量要开辟内存空间。 (2)引用名需要在变量定义与变量名绑定,并且不能重定义;指针变量可以在程序中赋给不同的地址 值,改变指向。 (3)程序中用变量名和引用名访问对象的形式和效果一样;指针变量通过间址访问对象。 验证程序: #include&iostream& int main () { cout&&&a=&; cin&&a; int ra=a; int *pa=&a; cout&&&a 的值:&&&a&& cout&&&a 的地址:&&&&a&& cout&&&ra 的值:&&&ra&& cout&&&ra 的地址:&&&&ra&& cout&&&pa 所指向的变量的值:&&&*pa&& cout&&&pa 的地址:&&&pa&& } 4.数据对象在 C++中有几种不同的访问方式?请编写一个程序验证之。 【解答】 数据对象在 C++中的访问方式有:名访问,引用(别名)访问,间址访问。 验证程序: #include&iostream& int main() { cout&&&a=&; cin&&a; a=a+5; cout&&&a&& cout&&*(&a)&& int *pa=&a; cout&&*pa&& int &ra=a; cout&&ra&& //名访问 //输出变量地址 //地址访问,输出变量值 //说明指针变量,指向变量 a //间址访问,输出变量值 //ra 是 a 的引用 //引用访问,输出变量 a 的值} 5.为了约束对数据对象的值进行只读操作,C++采用什么方式?请进行简要归纳。 【解答】 约束数据对象只读形式如下:约 束 对 象 标 指 识 针 常 常 量 量 c o n s t 类 const 型 类 型 * 说 明 形 式类 型 常 量 标 识 符 = 常 量 表 达 式 ; * c o n s t 指 const * 针 ;指 向 常 量 的 指 针 指向常量的指针常量 常 引 用指 针 ;或 者 类 型指 针 ;const 类 型 * con st 指 针 ; c o n s t 类 型 & 引或 者 类 型 cons t * cons t 指 针 ; 用 名 = 对 象 名 ;6.什么是表达式?表达式值的类型由什么因素决定?使用不同运算符连接以下 3 个变量,请写出 5 个以上获得值为 true 的表达式。 int a=1, b=2; double x=0.5; 【解答】 表达式是由数据和运算符,按求值规则,表达一个值的式子。 表达式值的类型的决定因素为操作数的类型。 (1)如果运算符左右操作数类型相同,运算结果也是相同类型。 (2)如果运算符左右操作数类型不同,首先把类型较低(存储要求,示数能力较低)的数据转换成类 型较高的数据,然后运算。 (3)赋值表达式的类型由被赋值变量的类型决定。当把一个表达式的值赋给一个变量时,系统首先强 制把运算值转换成变量的类型,然后执行写操作。 6 个值等于 true 的表达式: (1)b&a && a&x (4)a==(b*x)(2)(a+b)!=x (5)a-b&x(3)a||(b+x) (6)(a/x==b)7.阅读以下程序,分析下面语句序列中每一个字符D?‖和D&‖的含义,并写出输出结果。 #include &iostream& int main() { int a=10, b=20; int ?p = &a, ?q = &b; ?p = ?p ??q; int & ra = ra=a; int ?& rt = ?rt = 30; cout&&&a=&&&a&&&\nb=&&&b&&&\n?p=&&&?p&&&\n?q=&&&?q &&&\nra=&&&ra&&&\n?rt=&&&?rt&& } 【解答】 字符D*‖和D&‖的意义见程序中添加的注释。 #include &iostream& int main() { int a=10, b=20; int *p = &a, *q = &b; *p = *p * *q; int & ra = ra=a; int * & rt = *rt = 30; //D*‖是指针类型说明符,D&‖是引用说明符 //D*‖是间址访问符 //D*‖是指针类型说明符,D&‖是取址运算符 //第1、2、4个D*‖是间址访问符,第3个D*‖算术乘运算符 //D&‖是引用说明符//输出语句中的D*‖是间址访问符 cout&&&a=&&&a&&&\nb=&&&b&&&\n*p=&&&*p&&&\n*q=&&&*q &&&\nra=&&&ra&&&\n*rt=&&&*rt&& } 程序输出结果为: a=200 b=30 *p=200 *q=30 ra=200 *rt=30 1.6 编程题1.输入一个 3 位整数,将它反向输出。 【解答】 #include &iostream& int main() { int x,i,j,k; cout &&&please input x:&; cin && i = x/100; j = x/10 %10; k = x%10; cout && k && j && i && } 2.输入平面上某点横坐标 x 和纵坐标 y,若该点位于图 1.11 所示的方块区域内,则输出 1;否则,输 出 0。 【解答】 #include &iostream& int main() { double x,y,b; cout &&&please input x,y:&; cin && x && b = ( -2&=x ) && ( x&=2 ) && ( -2&=y ) && ( y&=2 ); cout && b && } 3.输入 3 个整数,求出其中最小数(要求使用条件表达式) 。 【解答】 #include &iostream& int main() { int a,b,c,temp, cout &&&please input a,b,c:&; cin && a && b && temp = ( a&b ) ? a:b; min = ( temp&c ) ? temp:c; cout &&&min=&&& min && } 图 1.11 方形区域 第 2 章程序控制结构2.1 选择题1.已知 int i=0, x=1, y=0;,在下列选项中,使 i 的值变成 1 的语句是( (A)if( x&&y ) i++; (C)if( x||y ) i++;??1 ? 2.设有函数关系为 y= ? 0 ?1 ?C )。(B)if( x==y ) i++; (D)if( !x ) i++;x?0 。 x ? 0 ,下列选项中,能正确表示上述关系的是(C) x?0(A) y = 1; if( x &= 0 ) if( x == 0 ) y = 0; else y = -1; (C) if( x &= 0 ) if( x & 0 ) y = -1; else y = 0;(B) y = -1; if( x != 0 ) if( x & 0 ) y = 1; else y = 0 (D) y = -1; if( x &= 0 ) if( x & 0 ) y = -1; else y = 0;else y = 1; 3.假设 i=2,执行下列语句后 i 的值为(B) 。 switch( i ) { case 1 : i ++; case 2 : i --; case 3 : ++ case 4 : -- default : i ++; } (A)1 (B)2 (C)3(D)44.已知 int i=0,x=0;,在下面 while 语句执行时循环次数为(D) 。 while( !x && i& 3 ) { x++; i++; } (A)4 (B)3 (C)2 (D)15.已知 int i=3;,在下面 do_while 语句执行时的循环次数为(B) 。 do{ i--; cout&&i&&}while( i!= 1 ); (A)1 (B)2 (C)3 (D)无限 6.下面 for 语句执行时的循环次数为(B) 。 int i, for ( i=0, j=5;i=j; ) { cout&&i&&j&& i++; j--; } (A)0 (B)5 7.以下程序段形成死循环的是(B) 。 (A) for( x=0; x&3; ) { x++; }; (B)int k = 0; do { ++k; } while( k&=0 ); (C)int a=5; while( a ) { a--; }; (C)10 (D)无限 (D)int i=3; for(; i -- );2.2 阅读下列程序,写出运行结果1. #include&iostream& int 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 && } 【解答】 20 2. #include&iostream& int main() { int a = 0, b = 1; switch( a ) { case 0: switch( b ) { case 0 : cout&&&a=&&&a&&& b=&&&b&& case 1 : cout&&&a=&&&a&&& b=&&&b&& } case 1: a++; b++; cout&&&a=&&&a&&& b=&&&b&& } } 【解答】 a= 0 b= 1 a= 1 b= 2 3. #include&iostream&
int main() { int i = 1; while( i&=10 ) { if( ++i % 3 != 1 ) else cout && i && } } 【解答】 4 7 10 4. #include&iostream& int main() { int i = 0, j = 5; do { i++; j--; if ( i&3 ) } while ( j&0 ); cout &&&i=&&& i && endl &&&j=&&& j && } 【解答】 i= 4 j= 1 5. #include&iostream& int main() { int i,j; for( i=1, j=5; i&j; i++ ) { j--; } cout&&i&&'\t'&&j&& } 【解答】 3 6. #include&iostream& 3
int main() { int i, s = 0; for( i=0; i&5; i++ ) switch( i ) { case 0: case 1: case 2: } cout&&&s=&&&s&& } 【解答】 s= 7 7. #include&iostream& int main() { int i, j, x = 0; for( i=0; i&=3; i++ ) { x++; for( j=0; j&=3; j++ ) { if( j % 2 ) x++; } x++; } cout &&&x=&&& x && } 【解答】 x= 16 s += s += s +=default: s += 2;2.3 思考题1.C++语言中有什么形式的选择控制语句?归纳它们的语法形式、应用场合。根据一个实际问题使用 不同的条件语句编程。 【解答】语句 i f 语 句 i f ( 表 达 式 使 ) 语 用 方 句 式 1 ; 使 用 场 合 需要对给定的条件进行判断,并根据判断的结果选择不同的操作。 else 语句 2; s w i t c h 语 句 s w i t c h ( 表 达 式 )适用于复杂的条件表达式判断。 根据整型表达式的不同值决定程序分支的情况。 适用于判断表达式简单,需要多个分支处理的情况。{ case 常量表达式 1: 语句 1; case 常量表达式 2: 语句 2; …… case 常量表达式 语句 [default : 语句 n+1;] }演示程序: 程序(1) //此程序用if输出等级对应的分数段 //A-&=90,B-(90,80],C-(80,70] ,D-(70,60],,E-&60 #include&iostream& int main() { cout&&&Enter the grade:&; cin&& //直到输入有效等级,否则程序不继续运行 while(!((gd&='A'&& gd&='E')||(gd&='a'&& gd&='e'))) { cout&&&Invalid grade! Please retry:&; cin&& } if(gd=='A'||gd=='a') cout&&&\nScored 90-100!\n&; else if(gd=='B'||gd=='b') cout&&&\nScored 80-89!\n&; else if(gd=='C'||gd=='c') cout&&&\nScored 70-79!\n&; else if(gd=='D'||gd=='d') cout&&&\nScored 60-69!\n&; else if(gd=='E'||gd=='e') cout&&&\nScore under 60!\n&; else cout&&&Unexpect error!\n&; } 程序(2) //此程序用switch输出等级对应的分数段 //A-&=90,B-(90,80],C-(80,70] ,D-(70,60],,E-&60 #include&iostream& int main() { cout&&&Enter the grade:&; cin&& //直到输入有效等级,否则程序不继续运行 while(!((gd&='A'&& gd&='E')||(gd&='a'&& gd&='e'))) { cout&&&Invalid grade! Please retry:&; cin&& } //防止意外错误 switch(gd) { case 'A': case 'a': cout&&&\nScored 90-100!\n&; case 'B': case 'b': cout&&&\nScored 80-89!\n&; case 'C': case 'c':cout&&&\nScored 70-79!\n&; case 'D': case 'd':cout&&&\nScored 60-69!\n&; case 'E': case 'e':cout&&&\nScore under 60!\n&; default:cout&&&Unexpect error!\n&;//防止意外错误 } } 2.什么叫做循环控制?归纳比较 C++语言中各种循环控制语句的语法、循环条件和循环结束条件的表 示形式及执行流程。 【解答】 循环控制是在特定的条件下,程序重复执行一些特定动作。 语句while 语 句 w h i l e ( 表 循环体; 达 式 )执 行 语法 流 程使 用 场 合程序中常用于根据条件执行操作而不需关心循环次数的情况。 先判断形式循环,条件不成立时不进入循环体。循环条件:表达式值为非 0(真) 循环结束条件:表达式值为 0(假)do-while 语句d 循环体 while(表达式);o程序中常用于根据条件执行操作而不需关心循环次数。 后判断形式循环,至少执行 1 次循环体。 一般情况,while 语句和 do while 语句可以互换使用。循环条件:表达式值为非 0(真) 循环结束条件:表达式值为 0(假) for 语 句 for([ 表达式 1];[ 表达式 2];[ 表达式 3]) 循环体; for 语句称为步长循环语句,通常用于确定循环次数的情况。由于语句的 3 个表达式均可以缺省,也可以用于条件循环,(1)表达式 1 称为初始化表达式,不是循环体执行部分。 (2)表达式 3 称为后置表达式,作为循环体的最后一个执行表达式。 (3)循环条件:表达式 2 值为非 0(真) 循环结束条件:表达式 2 值为 0(假)3.根据一个实际问题,用不同的循环语句编程,分析其优缺点。 【解答】 略。 4.用 if 语句和 goto 语句组织循环,改写上面第 3 题编写的程序,并分析在什么情况下可以适当使用 goto 语句。 【解答】 在不破坏程序基本流程控制的情况下,可以适当使用 goto 语句实现从语句结构内部向外的必要跳转, 即按特定条件结束结构语句块的执行。程序略。5. 有以下程序,希望判断两个输入的整数是否相等。程序通过编译,但不能达到预期结果。请分析程 序能够通过 C++编译而不能得到期望结果的原因。 #include&iostream& int main() { int a,b; cout&&&a: &; cout&&&b: &; if( a=b ) cout&&a&&&等于&&&b&& else cout&&a&&&不等于&&&b&& } 运行程序,输入 a 的值为 4,b 的值为 9,显示结果如下: a: 4 b: 9 9 等于 9 【解答】 在 if 语句的判断表达式(a=b)中,赋值号D=‖应该是逻辑等D==‖。从语法上,C++的 if 语句把 a=b 这个 赋值表达式视为逻辑表达式,没有编译错误。a=b 的值决定于 b。若 b 的输入值不等于 0,if 语句的判断表 达式作为逻辑真(true),否则作为逻辑假(false)。所以,题目中输入 b 的值虽然不等于 a,但表达式 a=b 为逻辑 true,执行了 if 语句的第 1 个分支。 cin&&a; cin&&b;2.4 编程题1. 输入某学生成绩, 若成绩在 85 分以上, 则输出Dvery good‖; 若成绩在 60~85 分之间, 则输出Dgood‖; 若成绩低于 60 分,则输出Dno good‖。 【解答】 #include&iostream& int main() { cout &&&please input score:&; cin && if ( score&=85 ) cout &&&Very good!& ; else if ( score&=60 ) cout &&&Good!&; else cout &&&No good!&; } 2.输入 3 个整数,按从小到大的顺序输出它们的值。 【解答】 #include&iostream& int main() { int a, b, c, cout &&&a, b, c=&; cin && a && b && 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 && } 3.输入三角形的 3 条边,判别它们能否形成三角形,若能,则判断是等边、等腰三角形,还是一般 三角形。 【解答】 #include&iostream& int main() { double a, b, cout &&&a, b, c = & ; cin && a && b && if ( a+b & c && b+c & a && c+a & b ) { if ( a == b && b == c ) cout &&&等边三角形!&&& else if ( a == b || a == c || b == c ) cout &&&等腰三角形!&&& else cout &&&一般三角形!&&& } else cout &&&不能形成三角形!&&& } 4.输入百分制成绩,并把它转换成五级分制,转换公式为:? ? grade(级别)=? ? ? ? ? ? ?A(优秀) B(良好) C(中等) D(合格)90~100 80~89 70~79 60~69E(不合格) 0~59 【解答】 #include&iostream& int main() { cout &&&score=&; cin && if ( score &= 0 && score &= 100 ) { switch { case case case case case case case case case case case } } else { cout &&&数据输入错误!&&& } cout && grade && end: } 5.编程模拟剪刀、石头和布游戏。游戏规则为:剪刀剪纸,石头砸剪刀,布包石头。玩游戏者从键 盘输入 s(表示剪刀)或 r(表示石头)或 p(表示布) ,要求两个游戏者交替输入,计算机给出输赢的信息。 【解答】 #include&iostream& int main() { char first, cout &&&First input( s,r or p ):&; cin && cout &&&Second input( s,r or p ):&; ; // 分号不能省 10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0: grade = 'e'; grade = 'a'; grade = 'b'; grade = 'c'; grade = 'd'; ( int( score ) /10 ) cin && switch { case 's': ( second ) 's': cout &&&Scissor ties scissor.&&& 'r': cout &&&Scissor is crushed by rock.&&& 'p': cout &&&Scissor cuts paper.&&& cout &&&second input error!&&& { case case case } case { case case case } case 'p': switch { case case case } default : cout &&&First input error!&&& } end: ; } 6.输入一个整数,输出该整数的所有素数因子。例如,输入 120,输出为 2、2、2、3 和 5。 【解答】 #include&iostream& int main() { int m,i = 2; cout &&&please input m:&; cin && while( i&=m ) if( m % i == 0 ) { cout && i &&&,&; 's': cout &&&Paper is cut by scissor.&&& 'r': cout &&&Paper wraps the rock.&&& 'p': cout &&&Paper ties paper.&&& cout &&&second input error!&&& ( second ) 's': cout &&&Rock crushes scissor.&&& 'r': cout &&&Rock ties rock.&&& 'p': cout &&&Rock is wrapped by paper.&&& cout &&&second input error!&&& 'r': ( second ) switch switch ( first )default :default :default : m = m / } else i++; } 7.使用迭代公式 xn ?1 ? ( xn ? a / xn ) / 2 (n ? 0, 1, 2?; x0 ? a / 2) 编程求某一正整数 a 的平方根。 【解答】 #include&iostream& #include&cmath& int main() { const double eps = 1e-8; double a,x0,x; cout &&&please input a:&; cin && x0 = a / 2; x = ( x0 + a/x0 )/2; while( fabs( x-x0 )&eps ) { x0 = } cout && x && } 8.已知 x=0 , 10 , 20 , …, 180 ,求 sinx,cosx 和 tanx 的值。 【解答】 #include&iostream& #include&cmath& #include&iomanip& int main() { const double pi = 3.; double x,y1,y2,y3; cout && setw(2) &&&x&&& setw(15) &&&sin(x)&&& setw(15) &&&cos(x)&&& setw(15) &&&tg(x)&&& for( i=0; i&=18; i++ ) { x = i*10*pi/180; y1 = sin( x ); y2 = cos(x); y3 = y1/y2; cout && setw(2) && i*10&& setw(15) && y1 && setw(15)? ? ? ?x =( x0 + a/x0 )/2; && y2 && setw(15) && y3 && } } 9.在 100~200 之间找出满足用 3 除余 2,用 5 除余 3 和用 7 除余 2 的所有整数。 【解答】 #include&iostream& int main() { for( i=100; i&=200; i++ ) { if ( ( i % 3 == 2) && ( i % 5 == 3 ) && ( i % 7 == 2 ) ) cout && i && } } 10.求 100~999 之间的水仙花数。所谓水仙花数,是指一个 3 位数,它的每位数字的立方之和等于 该数。例如,因为 153=1 +5 +3 ,所以 153 为水仙花数。 【解答】 #include&iostream& int main() { int i,a,b,c; for( i=100; i&=999; i++ ) { a = i/100; b = ( i-a*100 ) / 10; c = i - a*100 - b*10; if ( i == a*a*a + b*b*b + c*c*c ) cout && i && } } 11. 求 1000 以内的所有完数。 所谓完数, 是指一个数恰好等于它的所有因子之和。 例如, 因为 6=1+2+3, 所以 6 为完数。 【解答】 #include&iostream& int main() { int i,j,s; for( i=1; i&=1000; i++ ) {3 3 3 s = 0; for( j=1; j&i; j++ ) if ( i % j == 0 ) s = s + if ( i == s ) cout && i && } } 12.编写程序显示由符号组成的三角形图案。例如,程序运行后, 屏幕显示: 用户输入: 屏幕显示: 用户输入: 则输出如下图案: ? ??? ????? ??????? ????????? 【解答】 #include&iostream& int main() { int i,j,k,n; cout&&&How many lines ?\n&; cin&&n; cout&&&What character ?\n&; cin&& for( i=1; i&=n; i++ ) { for( k=1; k&=n-i; k++ ) cout &&&&; for( j=1; j&=2*i-1; j++ ) cout && cout && } } 13.已知 XYZ+YZZ=532,其中 X,Y 和 Z 为数字,编写程序求出 X,Y 和 Z 的值。 【解答】 #include&iostream& int main() { int x,y,z,i; for( x=1; x&=9; x++ ) for( y=1; y&=9; y++ ) for( z=0; z&=9; z++ ) How many lines ? 5 What character ? ? { i = 100*x + 10*y + z + 100*y + 10*z + if ( i == 532 ) cout&&&x=&&&x&&'\t'&&&y=&&&y&&'\t'&&&z=&&&z&& } }第 3 章函数3.1 选择题1.以下正确的函数原型为(D) 。 (A)f1( int y ); (C)void f1( int x, y ); (A)int a = 21; fun2( a ); (B)void f1( x, y ); (D)void f1( int, int ); (B)int a = 15; (D)fun2( 256 ); (B)int a = 15; (D)fun3( 256 ); (B)int a = 15; (D)fun4( 256 ); fun4( a?3.14 ); fun3( a?3.14 ); fun2( a?3 );2.有函数原型 void fun2( int );,在下列选项中,不正确的调用是(C) 。 (C)int b = 100; fun2( &b ); (A)double x = 2.17; fun3( &x ); (C)int b = 100; fun3( &b );3.有函数原型 void fun3( int ? );,在下列选项中,正确的调用是(C) 。4.有函数原型 void fun4( int & );,在下列选项中,正确的调用是(C) 。 (A)int a = 2.17; fun4( &a ); (C)int b = 100; 5.有声明 void fun5( int ?& ); int a, int ?p = &a; 在下列选项中,正确的调用是(B) 。 (A)fun5(&a); 6.有声明 int fun6( int ); int (?pf)(int) = fun6; 在下列选项中,正确的调用是(C) 。 (A)int a=15; int n=fun6(&a); (C)cout&&(?pf)( 256 ); (A)函数返回值需要强类型转换 (C)函数本身没有返回值 8.函数参数的默认值不允许为(C) 。 (A)全局常量 (B)直接常量 (C)局部变量 (D)函数调用 9.使用重载函数编程序的目的是(A) 。 (A)使用相同的函数名调用功能相似的函数 (B)共享程序代码 (C)提高程序的运行速度 10.在下列的描述中, (B)是错误的。 (A)使用全局变量可以从被调用函数中获取多个操作结果 (B)局部变量可以初始化,若不初始化,则系统默认它的值为 0 (D)节省存储空间 (B)int a = 15; cout&&(&pf)(a); (D)cout &&?pf( 256 ); (B)函数不执行任何操作 (D)函数不能修改实际参数的值 (B)fun5(p); (C)fun5(?a); (D)fun5(?p); fun4( b );7.在 VC 中,若定义一个函数的返回类型为 void,则以下叙述正确的是(C) 。 (C)当函数调用完后,静态局部变量的值不会消失 (D)全局变量若不初始化,则系统默认它的值为 0 11.在下列选项中, (C)具有文件作用域。 (A)语句标号 (B)局部变量 (C)全局变量 (D)静态变量3.2 阅读下列程序,写出运行结果1. #include&iostream& #include&cmath& int f( int ); int main() { for( i = 0; i & 3; i ++ ) cout && f( i ) && } int f( int a ) { int b = 0, c = 1; b ++; c++; return int( a + pow( double(b), 2 ) + c ); } 【解答】 3 4 5 2. #include&iostream& void func(int a, int b, int c = 3, int d = 4 ); int 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&& } 【解答】 10 15 20 30 10 12 3.11 1212 34 4#include&iostream& void func( int, int, int ? ); int main() { int x, y, func( 5, 6, &x ); func( 7, x, &y ); func( x, y, &z ); cout && x &&&,&&& y &&&, &&& z && } void func( int a, int b, int ?c ) { b += } 【解答】 6, 6, 6 4. #include&iostream& void func( int, int, int & ); int 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 && } void func( int a, int b, int &c ) { b += } 【解答】 2, 1, 3 5. #include&iostream& void func( int ?, int ?, int ?& ); int main() { int a=10, b=20; int ?p=&a, ?q=&b; func( p, q, p ); c = b - ?c = b - cout &&&?p=&&&?p &&&,?q=&&&?q && } void func( int ?t1, int ?t2, int ?& rt ) { ?t1 += 5; rt = t1; ?rt += 5; cout &&&?t1=&&&?t1 &&&,?t2=&&&?t2&&&,?rt=&&&?rt && } 【解答】 *t1=20,*t2=25,*rt=20 *p=20,*q=25 6. #include&iostream& int f2( int, int ); int f1( int a, int b ) { a += b += c = f2( a+b, b+1 ); } int f2( int a, int b ) { c = b % 2; return a + } int main() { int a = 3, b = 4; cout && f1( a, b ) && } 【解答】 15 7. #include&iostream& int age( int n ) { if( n == 1 ) f = 10; else f = age( n-1 ) + 2; ?t2 += 5;
} int main() { cout &&&age : &&& age( 5 ) && } 【解答】 age:18 8. #include&iostream& int f1( int a, int b ) { return a + } int f2( int a, int b ) { return a - } int f3( int(?t )( int, int ), int a, int b ) { return (?t )( a, b ); } int main() { int (?p )( int, int ); p = f1; cout && f3( p, 4, 8 ) && p = f2; cout && f3( p, 8, 4 ) && } 【解答】 12 4 9. #include&iostream& int sub( int, int ); int a = 1; int main() { int m = 1, n = 2, f = sub( m, n ); cout && a &&'\t'&& f && f = sub( m, n ); cout && a &&'\t'&& f && } int sub( int c, int d ) { static int m = 2, n = 5; cout && m &&'\t'&& n &&'\t'&& a = ++ c = m ++; d = n ++; return c + } 【解答】 2 2 3 3 5 7 6 93.3 思考题1.函数的作用是什么?如何定义函数?什么是函数原型? 【解答】 函数的两个重要作用: (1) 任务划分, 把一个复杂任务划分为若干小任务, 便于分工处理和验证程序正确性; ( 2) 软件重用, 把一些功能相同或相近的程序段,独立编写成函数,让应用程序随时调用,而不需要编写雷同的代码。 函数的定义形式: 类型函数名( [ 形式参数表 ] ) { 语句序列 } 函数原型是函数声明,告诉编译器函数的接口信息:函数名、返回数据类型、接收的参数个数、参数 类型和参数顺序,编译器根据函数原型检查函数调用的正确性。 2. 什么是函数值的返回类型?什么是函数的类型?如何通过指向函数的指针调用一个已经定义的函数? 编写一个验证程序进行说明。 【解答】 (1)函数的返回类型是函数返回的表达式的值得类型; (2)函数类型是指函数的接口,包括函数的参数定义和返回类型; (3)若有 functionType functionN //functionType 是已经定义的函数类型 functionType *functionPointer=functionN 则可以通过函数指针调用函数: (*functionPointer)(argumentList); 或 functionPointer(argumentList); 其中 argumentList 是实际参数表。 验证程序: #include&iostream& int main() //定义函数指针并获取函数地址 { typedef int myfunc(int,int); myfunc f,* int a=10,b=6; fp=f; cout&&&Using f(a):&&&f(a,b)&& cout&&&Using fp(a):&&&fp(a,b)&& cout&&&Using (*fp)(a):&&&(*fp)(a,b)&& return 0; } int f(int i,int j) { return i*j; } 3. 什么是形式参数?什么是实际参数?C++函数参数有什么不同的传递方式?编写一个验证程序进行 说明。 【解答】 参数是调用函数与被调用函数之间交换数据的通道。函数定义首部的参数称为形式参数,调用函数时 使用的参数称为实际参数。C++有三种参数传递机制:值传递(值调用) ;指针传递(地址调用) ;引用传 递(引用调用) 。 验证程序: #include&iostream& void funcA(int i) { i=i+10; } void funcB(int *j) { *j=*j+20; } void funcC(int &k) { k=k+30; } int main() { int a=1; funcA(a);cout&&&a=&&&a&& funcB(&a);cout&&&a=&&&a&& funcC(a);cout&&&a=&&&a&& } 程序输出: a=1 a=21 a=51 //传值参数,实际参数值不变 //指针参数,形式参数通过间址修改实际参数 //引用参数,形式参数通过别名方式修改实际参数 //函数名调用函数 //函数指针调用函数 //函数指针调用函数4. C++函数通过什么方式传递返回值?当一个函数返回指针类型时, 对返回表达式有什么要求?当返 回引用类型时,是否可以返回一个算术表达式?为什么? 【解答】 C++首先计算表达式的值,然后把该值赋给函数返回类型的匿名对象,通过这个对象,把数值带回调 用点,继续执行后续代码。 当函数返回指针类型时,返回的地址值所指对象不能是局部变量。因为局部变量在函数运行结束后会 被销毁,返回这个指针是毫无意义的。 返回引用的对象不能是局部变量,也不能返回表达式。算术表达式的值被储存在匿名空间中,函数运 行结束后会被销毁,返回这个变量的引用也是无意义的。 5.变量的生存期和变量作用域有什么区别?请举例说明。 【解答】 变量的生存期是指程序运行后,变量占有内存的时间;变量作用域指的是指变量声明之后,在程序正 文中有效的那部分区域。 例如,定义函数: void count() { static int n=0; //…… } 该函数中 n 被定义为 static 变量,生存期是整个程序运行时期;但作用域只在 count 函数中。 6.静态局部变量有什么特点?编写一个应用程序,说明静态局部变量的作用。 【解答】 静态局部变量的生存期是全程的,作用域是局部的。程序开始执行时就分配和初始化存储空间(默认 初始化值为 0) 。 定义静态局部变量的函数退出时, 系统保持其存储空间和数值。 下次调用这个函数时, static 变量还是上次退出函数时的值。直至整个程序运行结束,系统才收回存储空间。 程序略。 7.在一个语句块中能否访问一个外层的同名局部变量?能否访问一个同名的全局变量?如果可以,应 该如何访问?编写一个验证程序进行说明。 【解答】 一个语句块中不能访问外层的同名局部变量。可以访问一个同名的全局变量。 验证程序: #include&iostream& int a=0; int main() { int a=1; { int a=2; } cout&&&Main a is &&&a&& } 8.有函数原型 void f (int & n); 和函数调用 //输出外层局部变量a cout&&&Global a is &&&::a&& //输出全局部变量a //内层局部变量a //输出内层局部变量a cout&&&Local a is &&&a&& //外层局部变量a //全局变量a //… f(a); 有人说,因为 n 是 a 的引用,则在函数 f 中访问 n 相当于访问 a,所以,可以在 f 的函数体内直接使用变量 名 a。这种说法正确吗?为什么?编写一个验证程序。 【解答】 形式参数 n 的作用域是 f 函数,实际参数 a 的作用域是调用 f 函数的模块(例如 main 函数) ,所以在 f 函数中可见 n 而不可见 a。因此,这种说法不正确。f 函数内不能直接使用变量名 a,只能通过别名 n 访问 a。 验证程序: #include&iostream& void f ( int&n ); int main() { int a = 1 ; f( a ); cout&&&a=&&&a&& } void f ( int&n ) { a++; n++; } 产生编译错误:error C2065: Da‖: 未声明的标识符 9.有以下程序: #include&iostream& int main() { cin&&c; if(c=='y'||c=='Y') int a=1; else int a=0; cout&&&a=&&&a&& } 编译错误为:error C2065: 'a' : undeclared identifier,指示语句行 cout&&&a=&&&a&& 变量 a 没有定义。请分析原因,并进行修改。 【解答】 if-else 语句中分别出现变量 a 的说明语句,是作用域为 if 分支和 else 分支的两个变量。在 if-else 语句 之后,这两个变量都是没有定义的,因此出现编译错误。正确的程序是: #include&iostream& //错误,直接使用a //正确 int main() { cin&&c; if(c=='y'||c=='Y') a=1; else a=0; cout&&&a=&&&a&& } 10.有函数原型 double function(int,double); 函数 function 的返回值类型是什么?函数的类型是什么?使用 typedef 定义函数的类型。 有函数调用语句 x=function(10,(2? (0.314+5)); 其中的括号D()‖与函数原型中括号有什么语义区别? 【解答】 函数 function 的返回值类型是 double 函数类型是: double (int,double) 可以定义为: 表达式运算。 11.请分析以下各语句的意义。 int ? fun(); int ? (?pf)(); fun(); pf = pf(); 【解答】 int * fun() ; int * (*pf)(); fun() ; pf = pf() ; //函数原型声明。fun 是返回 int*类型,没有参数的函数 //声明指针变量。pf 是指向函数的指针,函数类型为 int*() //调用函数语句 //向指针变量赋值。函数指针 pf 指向函数 fun //用指针变量间址调用函数 typedef double funType (int,double); 函数调用 function(10,(2*(0.314+5)) 中,外层括号表示调用函数匹配的实际参数表,里面的两层括号是3.4 编程题1. 已知 y ? 求 y 的值。 【解答】 #include&iostream&sh(1 ? shx) et ? e?t , 其中, sh 为双曲正弦函数, 即 sh(t ) ? 。 编写一个程序, 输入 x 的值, sh2 x ? sh3 x 2 #include&cmath& double sh( double t ); int main() { double x,y; cout &&&x=&; cin && y = sh( 1+sh(x) )/( sh( 2*x )+sh( 3*x ) ); cout &&&y=&&& y && } double sh( double t ) { return ( exp( t )-exp( -t ) )/2; }2.输入 m、n 和 p 的值,求 s = 【解答】 double f( long k,long num ); int 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 && } else cout&&&溢出!\n&; } double f( long k,long num ) { double sum=0;1 ? 2 ? ? ? m ? 13 ? 23 ? ? ? n3 15 ? 25 ? ? ? p5的值。注意判断运算中的溢出。for( i=1; i&=num && sum&; i++ ) { sum = sum + pow( double (i),double (k) ); } if (i&=num) return 0; //溢出时返回 } 3.输入 a、b 和 c 的值,编写一个程序求这三个数的最大值和最小值。要求:把求最大值和最小值操 作分别编写成一个函数,并使用指针或引用做为形式参数把结果返回 main 函数。 【解答】 (1)使用指针参数 #include&iostream& void fmaxmin( double,double ,double ,double *,double * ); int main() { double a,b,c,max, cout &&&a,b,c = &; cin && a && b && fmaxmin( a,b,c,&max,&min ); cout &&&max=&&& max && cout &&&min=&&&min && } void fmaxmin( double x,double y,double z,double *p1,double *p2 ) { double u,v; if ( x&y ) { u = v = } else { u = v = }; if ( z&u ) u = if ( z&v ) v = *p1 = *p2 = } (2)使用引用参数 #include&iostream& void fmaxmin( double,double ,double ,double& ,double& ); int main() { double a,b,c,max, cout &&&a,b,c=&; cin && a && b && fmaxmin( a,b,c,max,min ); cout &&&max=&&& max && cout &&&min=&&& min && } void fmaxmin( double x,double y,double z,double &p1,double &p2 ) { double u,v; if ( x&y ) { u = v = } else { u = v = }; if ( z&u ) u = if ( z&v ) v = p1 = p2 = } 4.用线性同余法生成随机数序列的公式为: rk = ( multiplier × rk?1 + increment ) % modulus 序列中的每一个数 rk 都可以由它的前一个数 rk?1 计算出来。例如,如果有: rk = ( 25 173 × rk?1 + 13 849 ) % 65 536 则可以产生 65 536 个各不相同的整型随机数。设计一个函数作随机数生成器,生成 1 位或 2 位数的随 机数。 利用这个随机数生成器,编写一个小学生学习四则运算的练习程序,要求:? ? ? ?可以进行难度选择,一级难度只用 1 位数,二级难度用 2 位数; 可以选择运算类型,包括加、减、乘、除等; 可以给出错误提示; 可以统计成绩。【解答】 #include&iostream& int Rand(int,int); int main() { int w,i,r,t = 0; char op, int a,b,d; while(1) { cout&&&现在开始?( Y 或 N )\n& ; cin&& if (answer=='N'||answer=='n') while(1) { cout &&&请输入难度( 1 或 2 ):&; cin && if ( w != 1 && w != 2 ) cout &&&输入难度错误,重新输入!&&& } while(1) { cout &&&请输入运算类型( +,-,*,/ ):& ; //练习开始 //生成指定范围的随机数 cin && if ( op != '+'&& op != '-'&& op != '*'&& op != '/' ) cout &&&输入运算符错误,重新输入!&&& } //出 10 道题,每题 10 分 t=0; 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&b ) if ( op == '/' ) if ( int( a/b ) != (a / b) ) } cout && a && op && b &&'='; cin && switch ( op ) { case '+': r = a + case '-': r = a - case '*': r = a * case '/': r = a / } if ( r == d ) { cout &&&你算对了,加 10 分!&&& t = t + 10; } else cout &&&你算错了!&&& } cout &&&你的成绩是:&&& t &&&分&&& } } //只做结果为整数的除法 // 使被减数大于减数 int Rand(int m, int n) { do { r = ( 25173*r + 13849 ) % 65536 ; } while (r&m||r&=n); } 5.已知勒让德多项式为: //静态变量保留上一个随机数?1 ? pn ( x) ? ? x ? ((2n ? 1) p ( x) ? (n ? 1) p ( x)) / n n ?1 n?2 ?编写程序,从键盘输入 x 和 n 的值,使用递归函数求 pn(x)的值。 【解答】 #include&iostream& double p( double x,int n ); int main() { cout &&&please input x and n:&; cin && x && cout &&&p(&&& x &&&,&&& n &&&)=&&& p( x,n ) && } double p( double x,int n ) { double t1,t2; if( n == 0 ) return 1; else if( n == 1 ) else { t1 = ( 2*n-1 )*p( x,n-1 ); t2 = ( n-1 )*p( x,n-2 ); return ( t1-t2 )/n; } } 6.把以下程序中的 print()函数改写为等价的递归函数。 #include &iostream& void print( int w ) { for( int i = 1; i &= i ++ )n?0 n ?1 n ?1 { for( int j = 1; j &= j ++ ) cout && i &&&&; cout && } } int main() { print( 5 ); } 程序运行结果: 1 2 3 4 5 【解答】 2 3 4 5 3 4 4 5 5 5#include&iostream& void print(int w) { if( w ) { print( w-1 ); for( i=1; i&=w; i++ ) cout && w &&&&; cout && } } void main() { print( 5 );}7.已知用梯形法求积分的公式为: Tn ?n ?1 h( f (a) ? f (b)) ? h f (a ? ih) ,其中,h = ( b?a ) / n,n 为积 2 i ?1?分区间的等分数,编程求如下积分的值。要求:把求积分公式编写成一个函数,并使用函数指针作为形式 参数。调用该函数时,给出不同的被积函数作为实际参数求不同的积分。 ①? 2 sin xdx 0?0 1 ? x14dx ② 2?121 ? x 2 dx ③?【解答】 #include&iostream& #include&cmath& 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; t = ( ( *fun )(a) + ( *fun )( b ) ) / 2.0; h = ( b - a ) / for( i=1; i&=n-1; i++ ) t += ( *fun )( a + i * h ); t *= } int main() { double t1,t2,t3; t1 = trap( f1,0,1,10000 ); cout &&&t1=&&& t1 && t2 = trap( f2,1,2,10000 ); cout &&&t2=&&& t2 && t3 = trap( sin,0,3.,10000 ); cout &&&t3=&&& t3 && } 8.编写一个程序,包含三个重载的 display 函数和一个主函数。要求:第一个函数输出 double 值,前 面用字符串Da double:‖引导;第二个函数输出一个 int 值,前面用字符串Da int:‖引导;第三个函数输出一个 char 字符值, 前面用字符串Da char:‖引导; 在主函数中, 分别用 double、 int 和 char 型变量作为实参调用 display 函数。 【解答】 #include&iostream& void display( double d ) { cout &&&a double:&&& d && } void display( int i ) { cout &&&a int:&&& i && } void display( char c ) { cout &&&a char:&&& c && } int main() { double d = 1.5; int i = 100; char c = 'a'; display( d ); display( i ); display( c ); } 9.使用重载函数编程序分别把两个数和三个数从大到小排列。 【解答】 #include&iostream& void sort( double x,double y ); void sort( double x,double y,double z ); int main() { sort( 5.6, 79 ); sort( 0.5, 30.8, 5.9 ); } void sort(double x,double y) { if ( x&y ) cout && x &&'\t'&& y && else cout && y &&'\t'&& x && } void sort( double x,double y,double z ) { if( y&z ) { t = y = z = } if( x&z ) { t = x = z = } if( x&y ) { t = x =y = } cout && x &&'\t'&& y &&'\t'&& z &&'\t'&& }n 10.给定求组合数公式为: Cm ?m! n ,编一程序,输入 m 和 n 的值,求 Cm 的值。注意优化算 n!(m ? n)!n //返回 Cm 的值法,降低溢出可能。要求:主函数调用以下函数求组合数: int Fabricate( int m, int n ); Fabricate 函数内需调用 Multi 函数: int Multi( int m, int n ); 和 Multi 函数的定义。 【解答】 // 返回 m ? ( m ? 1) ??n程序由 4 个文件组成。头文件用于存放函数原型,作为调用接口;其他 3 个 cpp 文件分别是 main、Fabricate… n 分析计算公式 Cm ?m! 得到: n!(m ? n)!m!/(n!*(m-n)!)=(m*(m-1)*…*(m-n+1)*(m-n)!)/ (n!*(m-n)!) =(m*(m-1)*…*(m-n+1)/ (n!) 因此可以避免求较大数据 m 的阶乘,减少溢出的可能性。 //Fabricate.h #ifndef FABRICATE_H #define FABRICATE_H int Fabricate( int m,int n ); int Multi( int m, int n ); #endif //main.cpp #include&iostream& #include &Fabricate.h& int main() { int m ,n; cout &&&input m and n:&; cin && m && cout &&&Fabricate(&&& m &&&,&&& n &&&)=&&& Fabricate( m, n ) && } //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 * } 第 4 章数组4.1 选择题1.以下对一维数组 a 的定义正确的是(C) 。 (A)int n = 5, a[n]; (C)const int N = 5; int a[N]; (A)int a[3] = { 0, 1, 2, 3 }; (B)int a(5); (D) cin&&n; int a[n]; (B)int a[] = { 0, 1, 2 };2.下列数组定义语句中,不合法的是(A) 。 (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 中元素的式子是(C) 。 .. (A)?a (A)? (p++) (B)?p (B)?(++p) (C)a (C)?(p??) (D)a[ p?a ] (D)?(??p) 4.已知 int a[] = { 0,2,4,6,8,10 }, ?p = a+1; 其值等于 0 的表达式是(D) 。 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 (A)&a[2][1] (A)t (B)2 (B)?(a[2]+1) (B)一个地址值 (C)6 (C)a[2]+1 (C)java (D)7 (D)?(a+2)+1 (D)javac++ 7.已知 int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 不能表示数组元素 a[2][1]的地址是(B) 。 8.已知 char ?a[]={ &fortran&, & basic&, &pascal&, &java&, &c++& }; ,则 cout&&a[3];的显示结果是(C) 。 9.若用数组名作为调用函数的实参,则传递给形参的是(A) 。 (A)数组存储首地址 (C)数组中全部元素的值 (A)strcmp(s1,s2) (A)s1=s2 (C)strcpy(s1,s2)==0 (A)strcmp(s1,s2)&0 (C)strcmp(s1,s2)&0 (B)数组的第一个元素值 (D)数组元素的个数 (C)strcmp(s2,s3)==-1(D)strcmp(s1,s3) ==010.设有 char ?s1=&ABCDE&, ?s2=&ABCDE&,?s3=s1;,下列表达式中值等于 true 的是(D) 。 (B)strcmp(s1,s3)==1 11.设 char ?s1, ?s2;分别指向两个字符串,可以判断字符串 s1 和 s2 是否相等的表达式为(D) 。 (B)strlen(s1,s2) (D)strcmp(s1,s2)==0 (B)strcmp(s1,s2)==0 (D)strcmp(s2,s1)&012.设 char ?s1, ?s2;分别指向两个字符串,可以判断字符串 s1 是否大于字符串 s2 的表达式为(C) 。4.2 阅读下列程序,写出运行结果1. #include &iostream& int main() { int i, count=0, sum=0; int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for( i=0; i&10; i++ ) { if( a[i] % 2 == 0 ) sum += a[ i ]; count ++; } average = sum/ cout &&&count = &&& count &&'\t'&&&average = &&& average && } 【解答】 conut = 5 2. #include &iostream& int main() { int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int ?p = a, sum = 0; for(; p&a+9; p++ ) if(?p % 2 == 0 ) sum += ?p; cout &&&sum = &&& sum && } 【解答】 sum = 20 3. #include &iostream& #include &iomanip& const int N = 5; int main() { int a[N][N]={ 0 }, i, j, for( k=1, i=0; i&N; i++ ) for( j=i; j&= 0; j--, k++ ) a[j][i - j ] = for( i=0; i&N; i++ ) { for( j=0; j&N; j++ ) cout && setw( 3 ) && a[i][j]; average = 5 cout && } } 【解答】 1 2 4 11 4. #include &iostream& int f(int [],int); int main() { int a[] = { -1, 3, 5, -7, 9, -11 }; cout && f( a, 6 ) && } int f( int a[], int size ) { int i, t=1; for( i=0; i& i ++ ) if( a[i]&0 ) ?= a[i]; } 【解答】 135 5. #include &iostream& int f( int [][3], int, int ); int main() { int a[][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; cout && f( a, 3, 3 ) && } int f( int a[][3], int row, int col ) { int i, j, t=1; for( i=0; i& i ++ ) for( j=0; j& j++ ) { a[i][j]++; if( i == j ) } t ?= a[i][j]; 3 5 6 10 15 9 14 0 0 0 0 0 0 0 0 08 13 07 12 } 【解答】 45 6. #include &iostream& void test1( int ?a1 ) { a1 = new int( 5 ); cout &&&?a1 = &&&?a1 && } void test2(int ?& a2) { a2 = new int( 5 ); cout &&&?a2 = &&&?a2 && } int main() { int ?p = new int( 1 ); test1( p ); cout &&&test1: ?p1 = &&&?p && test2( p ); cout &&&test2: ?p2 = &&&?p && } 【解答】 *a1= 5 test1: *p1= 1 *a2= 5 test2: *p2= 5 7. #include &iostream& int main() { char s[] = &abccda&; for( i = 1; ( c=s[i] ) != '\0'; i ++ ) { switch( c ) { case 'a' : cout &&'%'; case 'b' : cout &&'$'; case 'c' : cout &&'?'; case 'd' : } cout &&'#'&& } } 【解答】 $# *# *# % 8. #include &iostream& int main() { char ?str[] = { &c++&, &basic&, &pascal& }; char ??p; p = for( i=0; i&3; i++ ) cout &&? ( p+i ) && } 【解答】 c++ basic pascal 9. #include &iostream& int main() { char s1[] = &Fortran&, s2[] = &Foxpro&; char ?p, ?q; p = s1; q = s2; while(?p &&?q ) { if (?p == ?q ) cout &&?p; p ++; q ++; } cout && } 【解答】 For 10. #include &cstring& #include &iostream& int main() { char str[][10] = { &vb&, &pascal&, &c++& }, s[10]; strcpy_s( s, ( strcmp( str[0], str[1] ) & 0 ? str[0] : str[1] ) ); if( strcmp( str[2], s ) & 0 ) strcpy_s( s, str[2] ); cout && s && } 【解答】 C++4.3 思考题1.数组说明语句要向编译器提供什么信息?请写出一维数组、二维数组说明语句的形式。 【解答】 数组说明语句要向编译器提供数组名 (标识符) , 数组元素的类型、 数组维数、 数组长度 (元素的个数) 等信息。 一维数组说明语句为:类型数组名[表达式] 二维数组说明语句为:类型数组名[表达式 1] [表达式 2] 2.数组名、数组元素的区别是什么?归纳一维数组元素地址、元素值不同的表示形式。若有说明 int aa [3], ?pa= 请使用 aa 或 pa,写出 3 个以上与 aa[2] 等价的表达式。 【解答】 数组名是一个标识符,执行代码中代表数组的地址,即指向数组起始位置的指针;而数组元素是下标 变量,性质相当于普通变量。 对一维数组 aa 第 i 个元素的地址可以表示为: 对一维数组 aa 第 i 个元素的值可以表示为: 与 aa[2]等价的表达式: *(aa+2) *(&a[2]) *(pa+2) pa[2] 3.要把一维数组 int a[m?n] 的元素传送到二维数组 int b[m][n]中,即在程序中要执行 b[i][j]=a[k]; 请写出 k→i,j 的下标变换公式,并用程序进行验证。 【解答】 转换公式: i=k/n 验证程序: #include &iostream& int main() { const int M=3,N=4; int k,a[M*N]={1,2,3,4,5,6,7,8,9,10,11,12},b[M][N]; int i,j; cout&&&array a:&&& j=k%n &aa[i] a[i] aa+i; *(a+i); for( k=0; k&M*N; k++ ) b[k/N][k%N] = a[k]; for( k=0; k&M*N; k++ ) cout&&a[k]&&'\t'; cout&& cout&&&**After convert**&&& cout&&&array b:&&& for(i=0;i&M;i++) { for(j=0;j&N;j++) cout&&b[i][j]&&'\t'; cout&& } } 4.有以下函数: void query() { int ?p; p=new int[3]; //… delete []p; p=new double[5]; //… delete []p; } 出现了编译错误。请分析错误的原因,并把上述程序补充完整,上机验证你的判断。 【解答】 在语句 p=new double[5]; 中企图把动态浮点型数组的地址写入整型指针 p,造成错误。错误为: error C2440: D=‖: 无法从Ddouble *‖转换为Dint *‖。 改正方法:增加一个 double*q 指针。 void query() { int *p; p=new int[3]; delete [] //…… double *q; q=new double[5]; //…… delete []q; } 5.有以下程序,设计功能是调用函数 create 建立并初始化动态数组,令 a[i]=i。但该程序运行后不能 得到期望结果,请分析程序的错误原因并进行修改。 #include &iostream& void create(int ?, int); int main() { int ?a = NULL, cin&& create(a,len); for( int i = 0; i& i++ ) cout && a[i] &&&&; cout && delete []a; a = NULL; } void create(int ?ap, int n) { ap=new int[n]; for(int i=0; i&n; i++) ap[i]=i; } 【解答】 函数create中,指针参数int*ap是传地址值的参数。调用函数时接受实际参数a的值(地址值)作为初 始值。ap仅是局部变量,ap=new int[n]获得新的地址值,函数执行完毕返回,ap被释放,完全与实际参数a 无关。程序没有编译错误,但main不能获得动态数组。修改方法是把ap改为指针引用参数。 void create(int *&,int); void create(int *&ap,int n) { ap=new int[n]; for(int i=0;i&n;i++) ap[i]=i; } //函数原型声明,使用引用参数 //函数定义4.4 编程题1.已知求成绩的平均值和均方差公式: ave ?? sii ?1nn, dev ?? (si ? ave)2i ?1nn,其中,n 为学生人数,si为第 i 个学生成绩。求某班学生的平均成绩和均方差。 【解答】 #include&iostream& #include&cmath& void aveMsd( double [], int, double &, double & ); int main() { double s[] = { 76, 85, 54, 77, 93, 83, 90, 67, 81, 65 }; double ave, int i,n; //求平均值和均方差值函数 n = sizeof( s )/sizeof( double ); cout&&&学生成绩:&; for( i=0; i&n; i++ ) cout&&s[i]&&&&; cout&& aveMsd( s, n, ave, msd );//求数组元素的个数cout &&&平均值:&&& ave && endl &&&均方差值:&&& msd && } void aveMsd( double s[], int n, double &ave, double &msd ) { double sum1=0, sum2=0; for( i=0; i&n; i++ ) sum1 += s[i]; ave = sum1/n; for( i=0; i&n; i++ ) msd = sqrt( sum2/n ); } 2.用随机函数产生 10 个互不相同的两位整数存放到一维数组中,并输出其中的素数。 【解答】 #include&iostream& #include&cmath& #include &cstdlib& #include&ctime& int main() { int a[10],i,j; srand( int( time(0)) ); for( i=0; i&10; i++ ) { l: a[i] = rand(); if ( a[i]&10 || a[i]&=100 ) for( j=0; j&i; j++ ) if( a[i]==a[j] ) } for( i=0; i&10; i++ ) cout && a[i] &&&&; cout && for( i=0; i&10; i++ ) { double m=sqrt( double (a[i]) ); //排除相同数据 //产生随机数存放到数组中 //获取指定范围数据 //为随机数生成器设置种子值 //求均方差 sum2 += pow( s[i]-ave, 2 ); //求平均值 for( j=2; j&=m; j++) if( a[i] % j == 0 ) if( j&m ) cout && a[i] &&&&; } cout &&&是素数!&&& } 3.将一组数据从大到小排列后输出,要求显示每个元素及它们在原数组中的下标。 【解答】 #include&iostream& int main() { int a[] = { 38, 6, 29, 1, 25, 20, 6, 32, 78, 10 }; int index[10]; int i,j, for( i=0; i&10; i++ ) index[i] = for( i=0; i&=8; i++ ) for( j=i+1; j&=9; j++ ) if( a[i] & a[j] ) { temp = a[i]; a[i] = a[j]; a[j] = temp = index[i]; index[i] = index[j]; index[j] = } for( i=0; i&10; i++ ) cout && a[i] &&'\t'&& index[i] && } 4.从键盘输入一个正整数,判别它是否为回文数。所谓回文数,是指正读和反读都一样的数。例如, 123321 是回文数。 【解答】 #include&iostream& int main() { int b[10], i, j, k, long num, cout &&&num=& ; k = 0; n = do { b[k++] = n % 10; n = n/10; } while( n != 0); //拆分整数,把各数字放入数组 b cin && //记录下标的数组 flag=1; i=0; j=k-1; while(i&j) if( b[i++] != b[j--] ) { flag = 0; }//判断标志 //设置指示下标的指针 //对称位置元素不相等if( flag ) cout && num &&&是回文数!&&& else cout && num &&&不是回文数!&&& } 5.把两个升序排列的整型数组合并为一个升序数组。设计好算法,以得到较高的运行效率。 【解答】 #include&iostream& void merge(const int a[],int na, const int b[],int nb, int c[],int nc); int main() { int a[4] = { 1, 2, 5, 7 }; int b[8] = { 3, 4, 8, 8, 9, 10, 11, 12 }; int c[12]; merge( a,4,b,8,c,12 ); for( i=0; i&12; i++ ) cout && c[i] &&&&; cout && } void merge(const int a[],int na, const int b[],int nb, int c[],int nc) { int i,j,k; i = j = k = 0; while( i&na && j&nb ) { if( a[i] & b[j] ) { c[k] = b[j]; else { c[k] = a[i]; } while( i&na ) { c[k] = a[i]; } while( j&nb ) { i++; k++; //把数组a的剩余元素写入数组c k++; i++; } k++; j++; } //当a[i]&=b[j],把a[i]写入数组c //当a[i]&b[j],把b[i]写入数组c c[k] = b[j]; } }k++;j++;//把数组b的剩余元素写入数组c6. 输入一个表示星期几的数,然后输出相应的英文单词。要求:使用指针数组实现。 【解答】 #include&iostream& int main() { char *weekday[7] = { &sunday&, &monday&, &tuesday&,&wednesday&, &thursday&, &friday&, &saturday& }; cout &&&please input week day: &; cin && if( d&=0 && d&=6 ) cout && d &&&---&&& *( weekday + d ) && else cout &&&input error!&&& } 7.编写以下函数: (1)在一个二维数组中形成以下形式的 n 阶矩阵:? ? ? ? ? ? ? ?1 2 3 4 51 1 2 3 41 1 1 2 31 1 1 1 21 1 1 1 1? ? ? ? ? ? ? ?(2)去掉靠边的元素,生成新的 n?2 阶矩阵; (3)求矩阵主对角线下元素之和; (4)以方阵形式输出数组。 在 main 函数中调用以上函数进行测试。 【解答】 #include&iostream& 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 ); int main() { int *ap = NULL, *bp = NULL, cout &&&输入矩阵的阶:&; cin && create( ap,n ); cout &&&\n 形成矩阵:\n&; output( ap, n ); cout &&&去掉靠边元素生成的矩阵:\n&; del( ap,bp,n ); output( bp,n-2 ); cout &&&主对角线元素之和:&&& maindiagonal( ap, n ) && } //形成 n 阶矩阵函数 void create( int * &app, int n ) { app = new int[ n*n ]; int i,j,k = 0; for( i=0; i&n; i++ ) for( j=0; j&n; j++ ) { if( i&=j ) app[k] = 1; else app[k] = i-j+1; k++; } } //去掉靠边元素生成 n-2 阶矩阵函数 void del( int *&app, int *&bpp, int n ) { int i,j,k = 0; bpp = new int[ ( n-2 )*( n-2 ) ]; for ( i=0; i&n; i++ ) { for( j=0; j&n; j++ ) if ( i && j && i&n-1 && j &n-1 ) { bpp[k]= *( app + i*n + j ); k++; } } } //求主对角线元素之和函数 int maindiagonal( int *&app, int n ) { int i,j,k = 0,sum = 0; for ( i=0; i&n; i++ ) { for( j=0; j&n; j++ ) if( i==j ) sum += *( app + i*n + j); } } //以方阵的形式输出数组函数 void output( int *&app, int n ) { int i,j; for ( i=0; i& i++ ) { for( j=0; j&n; j++ ) cout && *( app + i*n + j) &&'\t'; cout&& } cout&& } 8.设某城市 3 个百货公司某个季度销售电视机的情况和价格如下所示。编写程序,将表数据用数组 存放,求各百货公司的电视机营业额。牌号 公司 第 一 百 货 公 司 第 二 百 货 公 司 第 三 百 货 公 司 康 3 2 2 0 0 8 佳 0 0 0 T 2 2 2 C 5 4 1 L 0 0 0 牌号 长 1 2 1 5 0 8 虹 0 0 0 康佳 TCL 长虹 价格 00【解答】 #include&iostream& int main() { long s[][3] = { { 300, 250, 150 }, { 200, 240, 200},{ 280, 210, 180 } }; long p[] = { , 3800 }; int i,j; for( i=0; i&3; i++ ) { sum=0; for( j=0; j&3; j++) sum += s[i][j] * p[j]; cout &&&第&&& i+1 &&&百货公司的电视机营业额: } } 9. 设计函数求一整型数组的最小元素及其下标。 在主函数中定义和初始化该整型数组, 调用该函数, 并显示最小元素值和下标值。 【解答】 #include&iostream& &&& sum && int fmin(int [], int); int main() { int a[ ] = { 73, 85, 62, 95, 77, 56, 81, 66, 90, 80 }; index = fmin( a, sizeof(a)/sizeof(int) ); cout &&&The minnum number is : &&& a[index] && cout &&&The index is : &&& index && } int fmin( int a[], int size ) { int i,min = a[0], index = 0; for( i=0; i& i++ ) if( a[i]&min ) { min = a[i]; index = }; } 10.假设有从小到大排列的一组数据存放在一个数组中,在主函数中从键盘输入一个在该数组的最小 值和最大值之间的数,并调用一个函数把输入的数插入到原有的数组中,保持从小到大的顺序,并把最大 数挤出。然后在主函数中输出改变后的数组。 【解答】 #include&iostream& void insert( int a[],int,int ); int main() { int a[] = { 10, 12, 23, 25, 48, 48, 53, 58, 60, 78 }; int x,n,i; cout &&&please input insert data: &; cin && n = sizeof(a)/sizeof(int); insert( a, n, x ); for( i=0; i&n; i++ ) cout && a[i] &&&&; cout && } void insert( int a[],int n,int x ) { int i,p,j; if ( x&a[n-1] ) { //求数组长度 //插入元素 for( i=1; i&n; i++ ) if( x&a[i] ) { p=i; } for( j=n-1; j&=p; j-- ) a[j] = a[j-1]; a[p] = } }//查找插入位置//后移元素,挤出最大值 //插入元素11.一个整型数组的每个元素占 4 字节。编写一个压缩函数 pack,把一个无符号小整数(0~255)数 组进行压缩存储,只存放低 8 位;再编写一个解压函数 unpack,把压缩数组舒展开来,以整数形式存放。 主函数用随机函数生成数据初始化数组,测试 pack 和 unpack 函数。 【解答】 #include&iostream& #include &cstdlib& #include&ctime& void pack(int *a, unsigned char *p, int n); void unpack(unsigned char *p, int *a, int n); int main() { int *ary, n, unsigned char * cout&&&请输入数组长度:&; cin&&n; ary=new int [n]; packary=new unsignedchar[n]; srand(int(time(0))); for(i=0;i&n;i++) ary[i]=rand()/255; pack( ary, packary, n); cout&&&\n输出压缩数组:&; for( i=0; i&n; i++ ) { if (i %10 == 0) cout&& cout && int(packary[i] ) &&&&; } unpack( packary, ary, n); cout&&&\n输出解压数组:&; for( i=0; i&n; i++ ) { if (i %10 == 0) cout&& cout&&ary[i]&&&&; //控制一行输出10个数据 //产生随机数并存放到动态数组中 //建立动态数组 //压缩数组 } } void pack(int *a, unsigned char *p, int n) { for(int i=0; i&n; i++) { p[i]= unsignedchar(a[i]); } } void unpack(unsigned char *p, int *a, int n) { for(int i=0; i&n; i++) { a[i]=int(p[i]); } } 12.编写程序,按照指定长度生成动态数组,用随机数对数组元素进行赋值,然后逆置该数组元素。 例如,数组 A[5]的初值为{6, 3, 7, 8, 2},逆置后的值为{2, 8, 7, 3, 6}。要求:输出逆置前、后的数组元素序 列。 【解答】 #include&iostream& #include &cstdlib& #include&ctime& void printarray(int *p,int n); void adverse(int *p,int n); int main() { int *p,n,i; cout&&&请输入数组长度:&; cin&&n; p=new int [n]; srand(int(time(0))); for(i=0;i&n;i++) *(p+i)=rand()%1000; cout&&&动态数组:&; printarray(p,n); adverse(p,n); cout&&&逆置数组:&; printarray(p,n); } // 输出数组函数 void printarray(int *p,int n) { // 输出逆置数组 // 输出动态数组 // 对数组逆置 //产生随机数并存放到动态数组中 //建立动态数组
for( i=0; i&n; i++ ) { if (i % 5==0) cout&& } cout&& } // 对数组逆置函数 void adverse(int *p,int n) { int i,t; for (i=0;i&n/2;i++) { t=*(p+i); *(p+i)=*(p+n-i-1); *(p+n-i-1)=t; } } 13.把一个字符串插到另一个字符串中指定的位置。 【解答】 #include&iostream& int main() { int p, i, j, char s1[40], s2[40], s3[80]; cout &&&s1=&; cin && s1; cout &&&s2=&; cin && s2; cout &&&input insert position:&; cin && for( i=0; i&p; i++ ) s3[i] = s1[i]; for( j=0; s2[j] != '\0'; j++ ) s3[i+j] = s2[j]; for( k=p; s1[k] != '\0'; k++ ) s3[j+k] = s1[k]; s3[j+k] = '\0'; cout &&&s3=&&& s3 && } 14.把某班学生的姓名和学号分别存放到两个数组中,从键盘输入某位学生的学号,查找该学生是否 在该班,若找到该学生,则显示出相应的姓名。 // 输入插入位置 // 控制一行输出 5 个数据 cout&&&ary[&&&i&&&]=&&&*(p+i)&&&\t&; 【解答】 #include&iostream& int main() { char name[5][20] = { &li ming&, &zhang qing&, &liu xiao ping&, &wang ying&, &lu pei& }; long num[5] = { 030010 }; cout &&&please input studentnumber:&; cin && for( i=0; i&5; i++ ) { if( num[i] == snumber ) { cout &&&Found! The name is :&&& name[i] && } } if( i==5 ) cout &&&Can\'t found!&&& } 15.将一组 C++关键字存放到一个二维数组中,并找出这些关键字中的最小者。 【解答】 #include&iostream& #include &cstring& int main() { char string[10]; char str[][10]={ &while&, &break&, &if&, &extern&, &void&, &auto&, &long&, &static&, &do&, &const& }; strcpy( string, str[0] ); for( i=0; i&10; i++ ) if( strcmp(str[i],string)&0 ) strcpy( string, str[i] ); cout &&&The minimum string is:&&& string && } 16.使用指针函数编写程序,把两个字符串连接起来。 【解答】 #include&iostream& char *strcat( char *str1,char *str2 ) { char *p = str1; while( *p != '\0' ) p++; *p = *str2; do { p++; str2++; *p = *str2; } while( *str2 != '\0' ); return( str1 ); } int main() { char str1[80], str2[80]; cout &&&input str1:&; cin && str1; cout &&&input str2:&; cin && str2; cout &&&str1+str2=&&& strcat( str1, str2 ) && } 17.使用 string 类,编写一个简单的文本编辑程序,能够实现基本的插入、删除、查找、替换等功能。 【解答】 略。第 5 章集合与结构5.1 选择题1.语句 cout&&(1&2)&&&, &&&(1&&2)&& 的输出结果是(B) 。 (A)0, 0 (A)0, 0 (B)0, 1 (B)1, 1 (C)1, 0 (C)2, 0 (C)1, 1 (D)3, 1 2. 语句 cout&&(1|2)&&&, &&&(1||2)&& 的输出结果是(D) 。 3. 有以下说明语句: struct point { }p; 则正确的赋值语句是(C) 。 (A)point.x = 1; point.y = 2; (C)p.x = 1; p.y = 2; (B)point={ 1, 2 }; (D)p = { 1, 2 };4.已知有职工情况结构变量 emp 定义为: struct Date { }; strnct Employee { char name[20]; Date birth }; E 下列对 emp 的 birth 正确赋值方法是(D) 。 (A)year=1980; month=5; day=1; emp.month=5; emp.day=1; emp.birth.day=1; (B)birth.year=1980; birth.month=5; birth.day=1; (C)emp.year=1980; 5.有以下说明语句: struct Student { }; Student stu[3]={{1001,80}, {1002,75}, {1003,91}}, ?p= 则下面引用形式错误的是(B) 。 (A)p?&num 6.有以下说明语句: struct { int }; Worker w, ?p =&w; 则下列错误的引用是(D) 。 (A)w.no (B)p-&no (C)(?p).no (D)?p.no 7.s1 和 s2 是两个结构类型变量,若要使赋值 s1=s2 合法,则它们的说明应该是(C) 。 (A)s1 只能接收相同类型的数据成员 (C)同一结构类型的变量 (B)结构中的成员相同 (D)存储字节长度一样的变量 char name[20]; Worker (B)(p++).num (C)(p++)?&num (D)(?p).num (D)emp.birth.year=1980; emp.birth.month=5;5.2 阅读下列程序,写出运行结果。1. #include &iostream& struct Data { }; int main() { Data a[3] = { 2,72,1003,90 }, ?p = cout && (p++)-&n && cout && (p++)-&n && cout && p-&n++ && cout && (?p).n++ && } 【解答】 03 1004 2. #include &iostream& struct Employee { char name[ 20 ]; }; void fun( Employee ?p ) { if( (?p).sex == 'm' ) cout && (?p).name && } int main() { Employee emp[5] = { &Liming&, 'm', &Wangxiaoping&, 'f', &Luwei&, 'm' }; for( i=0; i&3; i++ ) fun( emp+i ); } 【解答】 Liming Luwei 3. #include &iostream& struct Node { char ? Node ? }; int main() { Node a[ ] = { { &Mary&, a+1 }, { &Jack&, a+2 }, { &Jim&, a } }; Node ?p = cout && p-&s && cout && p-&q-&s && cout && p-&q-&q-&s && cout && p-&q-&q-&q-&s && } 【解答】 Mary Jack Jim Mary5.3 思考题1.判断一个整数 n 的奇偶性,可以利用位运算判断吗?请你试一试。 【解答】 可以。一个整数当最低位为 1 时,它是奇数,否则为偶数。以下函数返回对参数 k 的奇偶判断。 bool odd( int k ) { return 1&k; } 2.长度为 N 的数组可以表示 N 个元素的集合,若有: S[i]==1,表示对应元素在集合中 如何实现集合的基本运算?请你试一试。并从内存和处理要求上与 5.2.2 节中集合的实现方法进行比较。 【解答】 长度为 N 的数组 S 可以表示有 N 个元素的集合。当 S[i]==1,表示元素 i+1 在集合中;当 S[i]==0,表 示元素 i+1 不在集合中。集合运算通过对数组元素操作完成。 用数组实现集合运算的空间和时间消耗高于用无符号整数和位运算实现集合运算。 用数组实现集合运算程序如下。 #include&iostream& void setPut( unsigned *S ); void setDisplay( const unsigned *S ); bool putX( unsigned *S, unsigned x ); //输入集合S的元素 //输出集合S中的全部元素 //元素x并入集合 //求并集C=A∪B //求交集C=A∩B //求差集C=A-B //判蕴含 //判属于x∈S //判空集void Com( unsigned *C, const unsigned *A, const unsigned *B); void setInt( unsigned *C, const unsigned A, const unsigned B); void setDif( const unsigned A, const unsigned B ); bool Inc( const unsigned *A, const unsigned *B ); bool In( const unsigned *S, const int N=32; //输入集合元素 void setPut(unsigned *S) const unsigned x ); bool Null( const unsigned *S ); { cin && while( x&0&&x&=N) { putX( S, x ); cin && } } //输出集合S中的全部元素 void setDisplay( const unsigned *S ) { cout &&&{ &; if (Null(S)) cout&&& else { for( int i=0; i&N; i++ ) { if( S[i] ) cout && i+1 &&&, &; } cout &&&\b\b }\n&; } } //元素x并入集合S bool putX( unsigned *S, unsigned x ) { if(x&0&&x&=N) { S[x-1] = 1; } } //求并集C=A∪B void Com( unsigned *C, const unsigned *A, const unsigned *B ) { for( int i=0; i&N; i++ ) C[i]= int( A[i] || B[i] ) ; } //求交集C=A∩B void setInt( unsigned *C, const unsigned *A, const unsigned *B ) //擦除最后的逗号 //输出元素 }\n&; //把输入元素并入集合S { for( int i=0; i&N; i++ ) C[i]= int( A[i]&&B[i] ) ; } //求差集C=A-B void setDif( { for( int i=0; i&N; i++ ) C[i]= int( A[i]&&!(A[1]&&B[i]) ) ; } //判蕴含,A蕴含于B时返回true bool Inc( const unsigned *A, const unsigned *B ) { for(int i=0; i&N; i++) { if(A[i]&&!B[i]) } } //判属于,x∈S时返回true bool In( const unsigned *S, const unsigned x ) { return S[x-1]; } //判空集,S为空集时返回true bool Null( const unsigned *S ) { for( int i=0; i&N; i++) { if( S[i]) } } int main() { unsigned A[N]={0}, B[N]={0}, C[N]={0}; cout &&&Input the elements of set A, 1-&&&N&&&, until input 0 :\n&; setPut( A ); cout &&&Input the elements of set B, 1-&&&N&&&, until input 0 :\n&; setPut( B ); unsigned *C, const unsigned *A, const unsigned *B ) cout&&&A = &; setDisplay( A ); cout&&&B = &; setDisplay( B ); cout &&&Input x: &; cin&&x; cout &&&Put &&& x &&& in A = &; putX( A, x) ; setDisplay(A); cout &&&C = A+B = &; Com( C, A, B ); setDisplay( C ); cout &&&C = A*B = &; setInt( C, A, B ); setDisplay( C ); cout &&&C = A-B = &; setDif( C, A, B ); setDisplay( C ); if( Inc( A, B ) ) cout &&&A &= B is true\n&; else cout &&&not A &= B\n&; cout &&&Input x: &; cin && if( In( A, x ) ) cout && x &&& in A\n&; else cout && x &&& not in A\n&; } 3.分析以下说明结构的语句: struct Node { N Node ? //错误 //正确}; error 和 ok 分别属于什么数据类型?有什么存储要求?error 出错的原因是什么? 【解答】 error 是 Node 结构类型数据成员,错误。原因是结构定义的数据成员若为本身的结构类型,是一种无 穷递归。ok 是指向 Node 类型的指针,定义正确,占 4 字节。 4.本章例 5-8 中用辅助数组对结构数组进行关键字排序,有定义: person ?index[100]; index 数组存放结构数组元素的地址。如果把 index 定义改为: int index[100]; 用于存放结构数组元素的下标,可以实现对结构数组的索引排序吗?如何修改程序?请你试一试。 【解答】 可以。关键是通过整型索引数组元素作为下标访问结构数组。表示为: all[pi[i]].name all[pi[i]].id 有关程序如下: #include&iostream& struct person { char name[10]; }; void Input( person[], const int ); void Sort( person[], int[],const int ); void Output( const person[], int[],const int ); int main() { person allone[100] ; int index[100]; for(int i=0; i&100; i++) index[i]= cout&&&输入职工人数:&; cin&& cout&&&输入职工信息:\n&; Input(allone,total); cout&&&以工资做关键字排序\n&; Sort(allone,index, total); cout&&&输出排序后信息:\n&; Output(allone,index,total); } void Input( person all[], const int n ) { for( i=0; i&n; i++ ) { cout&&i&&&: 姓名: &; cin&&all[i]. cout&&&编号: &; cin && all[i]. cout&&&工资: &; cin && all[i]. } } // 输入数据 //索引数组元素值初始化为结构数组元素下标 //说明结构数组 //说明索引数组 //说明结构类型 all[pi[i]].salary void Sort(person all[], int pi[], const int n) { int i,j; for(i=1; i&n; i++) { for(j=0; j&=n-1-i; j++) if(all[pi[j]].salary&all[pi[j+1]].salary) //通过索引数组访问结构数组元素 { t=pi[j]; pi[j]=pi[j+1]; pi[j+1]= } } } void Output(const person all[], int pi[], const int n) { for( int i=0; i&n; i++ ) } 5.有以下结构说明和遍历单向链表的函数。函数内有错误吗?是什么性质的错误?请上机验证你的 分析。 struct Node { Node ? }; void ShowList( Node ?head ) { while( head ) { cout && head-&date &&'\n'; head ++; } } 【解答】 head++错误, 原因是动态链表的结点存放不是连续顺序的内存空间, 它们是逐个结点通过 new 建立的, 所以不能用++做地址偏移运算。应该用: head=head-& // 输出排序后数据 cout&&all[pi[i]].name&&'\t'&&all[pi[i]].id&&'\t'&&all[pi[i]].salary&& //交换索引数组元素值 //交换用中间变量 //以成员salary做关键字排序5.4 编程题1.编写程序,将一个整型变量右移 4 位,并以二进制形式输出该整数在移位前和移位后的数值。观 察系统填补空缺的数位情况。 #include&iostream&
void bitDisplay(unsigned value); int main() { cout &&&Enter an unsigned integer: &; cin && bitDisplay(x); x&&=4; cout&&&Right 4-bit\n&; bitDisplay(x); } void bitDisplay(unsigned value) { unsigned bitmask = 1&&31; cout && value &&& = \t&; for( c=1; c&=32; c++ ) { cout && ( value&bitmask ? '1' : '0' ); value &&= 1; if( c%8 == 0 ) cout &&''; } cout && } 2.整数左移一位相当于将该数乘以 2。编写一个函数 unsigned power2( unsigned number, unsigned pow ); 使用移位运算计算 number*2pow,并以整数形式输出计算结果。注意考虑数据的溢出。 【解答】 unsigned power2( unsigned number, unsigned pow ) { unsigned c=1; unsigned bitmask = 1&&31; while(c&31) { if( number&bitmask ) c++; bitmask&&=1; } if(pow&c) return number&& else { //查找最高位的1 //溢出判断 cout&&&overflow!\n&; return 0; } } 3.使用按位异或(^)运算,可以不使用中间变量,快速交换两个变量的值。设计一个函数,实现快 速交换两个整型变量的值。 【解答】 void Swap( int &A, int &B) { A=A^B; B=A^B; A=A^B; } 4. 集合的元素通常是字符。设计程序,用无符号整数表示 ASCII 码字符集合,用位运算实现各种基 本集合运算。 【解答】 ASCII 码是 0~127 的整数, 可以用长度为 4 的无符号整型数组表示集合, 如教材例 5-4 所示。 区别是, 在输入集合元素时,需要把字符转换成整型数据,在输出操作中,把整型集合元素转换成字符型数据。 程序略。 5.使用结构类型表示复数。设计程序输入两个复数,可以选择进行复数的+、-、 × 或÷ 运算,并输 出结果。 【解答】 #include &iostream& #include &iomanip& struct complex { double re, }; int main() { complex a,b,c; cout &&&输入复数 a 的实部和虚部: &; cin && a.re && a. cout &&&输入复数 b 的实部和虚部:&; cin && b.re && b. cout &&&输入运算符: &; cin && switch ( oper ) { case '+': c.re=a.re+b. c.im=a.im+b. case '-': c.re=a.re-b. c.im=a.im-b. case '*': c.re=a.re*b.re-a.im*b. c.im=a.im*b.re+a.re*b. 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); default: cout &&&input error!&&& return 0; } cout &&&c=&&& c. cout && setiosflags( ios::showpos ); cout && c.im &&&i&&& return 0; } 6.把一个班的学生姓名和成绩存放到一个结构数组中,寻找并输出最高分者。 【解答】 #include &iostream& int main() { struct data { char name[12]; }a[ ] = {&李小平&,90,&何文章&,66,&刘大安&,87,&汪立新&,93,&罗建国&,78, &陆丰收&,81,&杨勇&,85,&吴一兵&,55,&伍晓笑&,68,&张虹虹&,9

我要回帖

更多关于 编写程序控制小灯闪烁 的文章

 

随机推荐