1、华工C+第二章习题及其解答习题 2及其解答2.1选择题1已知 int i=0, x=1, y=0 ; 在下列选项使i的值变成1的语句是( c )。(a) if( x&y ) i+ ; (b) if( x=y ) i+ ;(c) if( x|y ) i+ ; (d) if( !x ) 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 =
2、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) 44已知int i=0,x=0; 下面while语句执行时循环次数为( d )。while( !x & i
3、3 ) x+ ; i+ ; (a) 4 (b) 3 (c) 2 (d) 15已知int i=3;下面do_while 语句执行时循环次数为( b )。do i-; coutiendl;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 endl;i+; j-;(a) 0 (b) 5 (c) 10 (d) 无限7以下死循环的程序段是( b )。(a) int x; for( int x=0 ; x=0 ) ;(c) int a=5 ; while( a
4、) a- ; ;(d) int i=3 ; for( ; i ; i - ) ;2.2阅读下列程序,写出执行结果1#includeusing namespace std;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 endl;【解答】 202#includeusing namespace std;int main() int a = 0, b = 1; switch( a ) case 0: swit
5、ch( b ) case 0 : couta=a b=bendl; break; case 1 : couta=a b=bendl; break; case 1: a+; b+; couta=a b=bendl; 【解答】a= 0 b= 1a= 1 b= 23#includeusing namespace std;int main() int i = 1; while( i=10 ) if( +i % 3 != 1 ) continue; else cout i endl;【解答】 4 7 104#include using namespace std; int main() int i =
6、0 , j = 5; do i+; j-; if ( i3 ) break; while ( j0 ); cout i= i t j= j endl;【解答】 i= 4 j= 15#includeusing namespace std;int main() int i,j; for( i=1, j=5; ij; i+ ) j-; coutitjendl;【解答】 3 36#includeusing namespace std;int main() int i, s = 0; for( i=0; i5; i+ ) switch( i ) case 0: s += i; break; case 1
7、: s += i; break; case 2: s += i; break; default: s += 2; couts=sendl;【解答】 s= 77#includeusing namespace std;int 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 =90,B-(90,80,C-(80,70 ,D-(70,60,E-60#includeusing namespace std;int main() char g
8、d;coutgd;/直到输入有效等级,否则程序不继续运行while(!(gd=A & gd=a & gd=e) coutgd;if(gd=A|gd=a) coutnScored 90-100!n; else if(gd=B|gd=b) coutnScored 80-89!n;else if(gd=C|gd=c) coutnScored 70-79!n; else if(gd=D|gd=d) coutnScored 60-69!n; else if(gd=E|gd=e) coutnScore under 60!n; else cout=90,B-(90,80,C-(80,70 ,D-(70,60
9、,E-60#includeusing namespace std;int main() char gd;coutgd;/直到输入有效等级,否则程序不继续运行while(!(gd=A & gd=a & gd=e) coutgd; switch(gd) case A: case a: coutnScored 90-100!n;break;case B:case b: coutnScored 80-89!n;break;case C:case c:coutnScored 70-79!n;break;case D:case d:coutnScored 60-69!n;break;case E:case
10、 e:coutnScore under 60!n;break;default:coutUnexpect error!n;/防止意外错误2. 什么叫循环控制?归纳比较C+中各种循环控制语句的语法、循环条件和循环结束条件的表示形式及执行流程。【解答】循环控制是在特定的条件下,程序重复执行一些特定动作。语句语法执行流程使用场合while语句while(表达式)循环体;循环条件:表达式值为非0(真)循环结束条件:表达式值为0(假)程序中常用于根据条件执行操作而不需关心循环次数的情况。先判断形式循环,条件不成立时不进入循环体。do-while语句do循环体while(表达式);循环条件:表达式值为非0(
11、真)循环结束条件:表达式值为0(假)程序中常用于根据条件执行操作而不需关心循环次数。后判断形式循环,至少执行1次循环体。一般情况,while语句和do while语句可以互换使用。for 语句for(表达式1;表达式2;表达式3) 循环体;(1)表达式1称为初始化表达式,不是循环体执行部分。(2)表达式3称为后置表达式,作为循环体的最后一个执行表达式。(3)循环条件:表达式2值为非0(真)循环结束条件:表达式2值为0(假)for语句称为步长循环语句,通常用于确定循环次数的情况。由于语句的3个表达式均可以缺省,也可以用于条件循环,即循环次数不确定的情况。3. 根据一个实际问题,用不同的循环语句编
12、程,分析其优缺点。【解答】略。4. 用if语句和goto语句组织循环,改写思考题的第3小题编写的程序。分析在什么情况下可以适当使用goto语句。【解答】在不破坏程序基本流程控制的情况下,可以适当使用goto语句实现从语句结构内部向外的必要跳转,即按特定条件结束结构语句块的执行。程序略。5.有以下程序#includeusing namespace std;int main() char c; cinc; if(c=y|c=Y) int a=1; else int a=0; couta=aendl;编译错误为:error C2065: a : undeclared identifier,指示变量a
13、没有定义。请分析原因,并做出修改。【解答】变量a的定义不应该放在if-else语句体中。说明语句和执行语句的执行时机不同。变量说明要求在编译时定义存储空间,而if-else是执行语句,程序运行后才执行。正确的程序是:#includeusing namespace std;int main() char c; int a; cinc; if(c=y|c=Y) a=1; else a=0; couta=aendl;6. 有以下程序,希望判断两个输入的整数是否相等。程序通过编译,但不能达到预期结果。请分析程序能够通过C+编译而不能得到期望结果的原因。#includeusing namespace s
14、td;int main() int a,b; couta; coutb; if( a=b ) couta等于bendl; else couta不等于bendl;程序运行后,输入a的值为4,b的值为9,显示结果如下:a: 4b: 99等于9 【解答】在if语句的判断表达式(a=b)中,赋值号“=”应该是逻辑等“=”。从语法上,C+的if语句把a=b这个赋值表达式视为逻辑表达式,没有编译错误。a=b的值决定于b。若b的输入值不等于0,if作为逻辑真(true),否则作为逻辑假(false)。所以,题目中输入b的值虽然不等于a,但表达式a=b为逻辑true,执行了if语句的第1个分支。2.4编程题1
15、输入某学生成绩,若成绩在85分以上输出“very good”,若成绩在60分到85分之间输出“good”,若成绩低于60分输出“no good”。【解答】#includeusing namespace std;int main() double score; cout score; if ( score=85 ) cout =60 ) cout Good!; else cout No good!;2输入三个整数,按从小到大的顺序输出它们的值。【解答】#includeusing namespace std;int main() int a, b, c, t;cout a b c;if(ab) t
16、=a; a=b; b=t; if(ac) t=a; a=c; c=t; if(bc) t=b; b=c; c=t; cout a t b t c endl;3输入三角形的三条边,判别它们能否形成三角形,若能,则判断是等边、等腰、还是一般三角形。【解答】#includeusing namespace std;int main() double a, b, c ; cout a b c ; if ( a+b c & b+c a & c+a b ) if ( a = b & b = c ) cout 等边三角形! endl; else if ( a = b | a = c | b = c ) cou
17、t 等腰三角形! endl; else cout 一般三角形! endl; else cout 不能形成三角形! endl ;4输入百分制成绩,并把它转换成五级分制,转换公式为: 【解答】#includeusing namespace std;int main() double score; char grade; cout score; if ( score = 0 & score = 100 ) switch ( int( score ) /10 ) case 10: case 9: grade = a; break; case 8: grade = b; break; case 7: g
18、rade = c; break; case 6: grade = d; break; case 5: case 4: case 3: case 2: case 1: case 0: grade = e; break; else cout 数据输入错误! endl; goto end; cout grade endl;end: ; /分号不能省5编程序模拟剪刀、石头和纸游戏。游戏规则为:剪刀剪纸,石头砸剪刀,纸包石头。玩游戏者从键盘上输入s(表示剪刀)或r(表示石头)或p(表示纸),要求两个游戏者交替输入,计算机给出输赢的信息。【解答】#includeusing namespace std;in
19、t main() char first,second; cout first; cout second; switch ( first ) case s: switch ( second ) case s: cout Scissor ties scissor. endl; goto end; case r: cout Scissor is crushed by rock. endl; goto end;case p: cout Scissor cuts paper. endl; goto end; default : cout second input error! endl ; goto e
20、nd; case r:switch ( second ) case s: cout Rock crushes scissor. endl; goto end; case r: cout Rock ties rock. endl; goto end; case p: cout Rock is wrapped by paper. endl; goto end; default : cout second input error! endl; goto end; case p: switch ( second ) case s: cout Paper is cut by scissor. endl;
21、 goto end; case r: cout Paper wraps the rock. endl; goto end; case p: cout Paper ties paper. endl; goto end; default : cout second input error! endl; goto end; default : cout First input error! endl; goto end; end: ;6.输入一个整数,输出该整数的所有素数因子。例如,输入120,输出为2、2、2、3和5。【解答】#includeusing namespace std;int main
22、() int m,i = 2; cout m; while( i=m ) if( m % i = 0 ) cout i ,; m = m / i; else i+;7.使用迭代公式编程序求某一正整数a的平方根。【解答】#include#include using namespace std;int main() const double eps = 1e-8;double a,x0,x;cout a;x0 = a / 2;x = ( x0 + a/x0 )/2;while( fabs( x-x0 )eps ) x0 = x; x =( x0 + a/x0 )/2; cout x endl;8.
23、已知X=0,10,20,180,求sinx,cosx和tanx的值。【解答】#include#include #includeusing namespace std;int main() const double pi = 3.14159265; int i; double x,y1,y2,y3; cout setw(2) x setw(15) sin(x) setw(15) cos(x) setw(15) tg(x) endl; for( i=0; i=18; i+ ) x = i*10*pi/180; y1 = sin( x ); y2 = cos(x); y3 = y1/y2; cout setw(2) i setw(15) y1 setw(15) y2 setw(15) y3 endl; 9.在100到200中找出同时满足用3除余2,用5
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1