C语言谭浩强版本第5章课后练习题答案
第5章课后练习题
一 单选题
假定所有变量均已正确说明,下列程序段运行后x的值是:
int a=0,b=0,x=35;
if(!a) x--;
if(b) x=3;
else x=4;
34
4
35
3
与 y=(x>0?1:x<0?-1:0);的功能相同的if语句是:
if (x>0) y=1;
else if(x<0)y=-1;
else y=0;
if(x)
{if(x>0)y=1;
else if(x<0)y=-1;
else y=0; }
y=-1;
if(x)
{ if(x>0)y=1;
else if(x==0)}
else y=-1;
y=0;
if(x>=0)
{ if(x>0)y=1;
else y=-1;}
为了避免嵌套的条件分支语句if-else的二义性,C语言规定:C程序中的else总是与(??? )组成配对关系。
缩排位置相同的if??
在其之前未配对的if
在其之前未配对的最近的if??
同一行上的if
能将变量 u 、s中的最大值赋给变量t 的是:
if(u>s) t=u;t=s;??????
t=u; if(t ) ?t=s;
if(u>s) t=s;else t=u;? ??
?t=s; if(u ) ?t=u;
对下面三条语句(其中s1和s2为内嵌语句),正确的论断是
?? (1)if (a) s1;else s2;? ????(2) if (a==0) s2;else s1;?
? (3)if (a!=0) s1;else s2;
三者相互等价
1和2等价,但与 3不等价
三者互不等价 ?
1和3等价,但与2不等价
若执行下面的程序时从键盘上输入 5 ,则输出是:
int x; scanf("%d",&x);
if(x++>5) printf("%d\n",x); else printf("%d\n",x--);
7
6
5
4
判断题
??if (a = 1)? printf(“One\n”) ; ?语句是否正确? ( 正确 )
选择结构可以使用嵌套形式. (?? )
填空题
结构化程序设计所规定的三种基本控制结构是 ________,__________,________
分析题
以下程序的执行结果是:
int a=25,b=14,c=19;
if(a++<=25&&b--<=2&&c++) printf("***a=%d,b=%d,c=%d\n",a,b,c); else???printf("###a=%d,b=%d,c=%d\n",a,b,c);
以下两条if语句可合并成一条if语句为:
if(a<=b)? x=1; else???? y=2;
if(a<=b) printf("**** y=%d\n",y);
else?? printf("#### x=%d\n",x);
以下程序的执行结果是:
? main()
? {? int a,b,c;
???? a=2;b=3;c=1;
???? if (a>b)
???????? if (a>c) printf("%d\n",a);
????????? else ????????? printf("%d\n",b);
???? printf("end\n");
? }
以下程序的执行结果是:
?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;
???? printf("d=%d\n",d);
? }
以下程序在输入5,2之后的执行结果是:
? main()
? { int s,t,a,b;
??? scanf("%d,%d",&a,&b);
? ?? s=1;
??? t=1;
??? if(a>0) s=s+1;
??? if(a>b)t= s + t;
??? else if(a==b) t=5;
??? else t=2*s;
??? printf("s=%d,t=%d\n",s,t);
? }
写出下列程序的输出结果:
main()
{ int a=100;
if(a>100)? printf("%d\n",a>100);
else??? printf("%d\n",a<=100);
}