第三单元 选择程序设计
一、 选择题
1. 若x=0,y=3,z=3,以下表达式值为0的是 A. !x B.x 2. 以下运算符中优先级最低的运算符为 ,优先级最高的为 。 A. && B. ! C. != D. || E. ?: F. == 3. 若w=1,x=2,y=3,z=4,则条件表达式w 4. 若w,x,z均为int型变量,则执行以下语句后的输出为 。 w=3;z=7;x=10; printf(\printf(\printf(\printf(\ A. 0 B. 1 C. 0 D. 0 1 1 1 1 1 1 0 0 1 1 1 0 5. 分析以下程序, 下列说法正确的是 。 main() { int x=5,a=0,b=0; if(x=a+b) printf(\else printf(\ } A. 有语法错,不能通过编译 B. 通过编译,但不能连接 C. 输出* * * * D. 输出# # # # 6. 分析以下程序, 下列说法正确的是 。 main() { int x=5,a=0,b=3; if(x=a+b) printf(\else printf(\ } A. 有语法错,不能通过编译 B. 通过编译,但不能连接 C. 输出* * * * D. 输出# # # # 7. 分析以下程序, 下列说法正确的是 。 main() { int x=0,a=0,b=0; if(x=a+b) printf(\else printf(\ } A. 有语法错,不能通过编译 B. 通过编译,但不能连接 C. 输出* * * * D. 输出# # # # 8. 分析以下程序, 下列说法正确的是 。 main() { int x=0,a=0,b=0; if(x==a+b) printf(\else printf(\ } A. 有语法错,不能通过编译 B. 通过编译,但不能连接 C. 输出* * * * D. 输出# # # # 1. include main() { int a=-1,b=4,k; k=(a++<=0)&&(!(b--<=0)); printf(\ } 2. main() { int x=4,y=0,z; x*=3+2; printf(\ x*=(y==(z=4)); printf(\ } 3. main() { int x,y,z; x=3; y=z=4; printf(\ printf(\} 4. main() { int x=1,y=1,z=10; if(z<0) if(y>0) x=3; 二、 读程序写结果 else x=5; printf(\if(z=y<0) x=3; else if(y==0) x=5; else x=7; printf(\printf(\ } 5. main() { char x=‘B’; switch(x) { case ‘A’: printf(“It is A.”); case ‘B’: printf(“It is B.”); case ‘C’: printf(“It is C.”); default: printf(“other.”); } } 6. main() { int x=1,y=0,a=0,b=0; switch(x) { case 1: switch(y) { case 0: a++;break; case 1: b++;break; } case 2: a++;b++;break; case 3: a++;b++; } printf(\ } 三、 填空题 1. 若a=5,b=6,c=7,d=8,则表达式d=a/2&&b==c||!a的值为 2. 定义 int x=10,y,z;执行y=z=x;x=y==z后,变量x的值为 。 3. 分段函数:输入x,计算y值,输出y,其中: x<0 y=2x+3 x=0,y=0 x>0,y=(x+7)/3 #include { int x,y; scanf(\ if(x<0) (1) ; (2) y=0; (3) y=(x+7)/3; printf(“%d”,y); } 4. 由键盘输入三个数,计算以这三个数为边长的三角形面积。 (1) main() { (2) ; printf(\ scanf(\ if( (3) ) { s=(a+b+c)*0.5; s1=s*(s-a)*(s-b)*(s-c); s= (4) ; printf(\ } (5) printf(\ } 5. 有一方程ax+bx+c=0,a,b,c的值由键盘输入,请编程序,打印出以下情况时方程的 解。 (1) a=0,b≠0 (2) a=0,b=0,c=0 (3) a=0,b=0,c≠0 (4) a≠0,b-4ac≥0 (5) a≠0,b-4ac≤0 222#include \main() { float a,b,c,d,pr,pi,x1,x2; scanf(\ printf(\ if(a==0) { if( (1) ) printf(\ else if( (2) )printf(\ else printf(\ } else { d=b*b-4*a*c; if( (3) ) { x1=(-b+sqrt(d))/ (4) ; x2=(-b-sqrt(d))/ (5) ; printf(\ } else { pr=-b/(2*a); (6) ; printf(\ printf(\ } } } 6. 投票表决器: – 输入Y、y,打印agree – 输入N、n,打印disagree – 输入其他,打印lose main() { char c; scanf(\ (1) { } case ‘Y’: case ‘y’: printf(“agree”); (2) ; case ‘N’: case ‘n’: printf(“disagree”); (3) ; (4) :printf(“lose”);