第二章 选择结构 ·29·
f=(++a>b--);
请问a,b,c,d,e,f的结果是:_3 2 1 1 0 0_______。 7. 以下程序的运行结果是__2 0 0______。 #include ”stdio.h” main() {
int a,b,c,s,w,t;
s=w=t=0;
a= -1; b=3; c=3; if (c>0) s=a+b; if (a<=0) { if (b>0)
if (c<=0) w=a-b;
}
else if (c>0) w=a-b;
else t=c;
printf(”%d %d %d”, s,w,t); }
8. 以下程序的运行结果是__100______。 #include ”stdio.h” main() {
int a,b,c,d,e; a=c=1; b=20; d=100;
if (!a) d=d++; else if (!b)
if (d) d= --d;
else d= d--;
printf(”%d\\n\\n”, d); }
第二章 选择结构 ·30·
9. 以下程序的运行结果是_6 250 (换行)5 1 0 0______。
#include ”stdio.h” main() {
int a, b= 250, c; if ((c=b)<0) a=4; else if (b=0) a=5; else a=6;
printf(”\\t%d\\t%d\\n”,a,c); if (c=(b==0))
a=5;
printf(”\\t%d\\t%d\\n”,a,c); if (a=c=b) a=4;
printf(”\\t%d\\t%d\\n”,a,c); }
10.下面程序根据以下函数关系,对输入的每个值。请在【】内填空。
x y 2 int x,y; scanf(”%d”, &x); if (【1】) y=x*(x+2); else if (【2】) y=1/x; x值,计算出y 第二章 选择结构 ·31· else if (x<=-1) y=x-1; else 【3】; if (y!= -1) printf(”%d”,y); else printf(”error”); } 11.以下程序的功能是计算一元二次方程ax2+bx+c=0的根。请在【】内填入正确内容。 #include ”stdio.h” #include ”math.h” main() { float a,b,c,t,disc,w,term1,term2; printf(”enter a,b,c:”); scanf(%f%f%f”,&a,&b,&c); if (【1】) if (【2】) printf(”no answer due to input error\\n”); else printf(”the single root is %f\\n”, - c/b); else { disc=b*b-4*a*c; w=2*a; term1= -b/w; t=abs(disc); term2=sqrt(t)/w; if (【3】) printf(”complex root\\n real part=%f imag part =%f\\n”, term1,term2); else printf(”real roots\\n root1=%f root2=%f\\n”, term1+term2,term1-term2); } } 第二章 选择结构 ·32· 12.以下程序根据输入的三角形的三边判断是否能组成三角形, 若可以则输出它的面积和三角形的类型。请在【】内填入正确内容。 #include ”math.h” #include ”stdio.h” main() { float a,b,c,s,area; printf(”please input three edges of a triangle:”); scanf(”%f%f%f”,&a,&b,&c); if (【1】) { s=(a+b+c)/2; area=sqrt(s*(s-A*(s-B*(s-c)); printf(”\\nthe area of the triangle is: %f”,area); if ((a==b)&&(b==c)) printf(”等边三角形”); else if (【2】) printf(”等腰三角形”): else if (【3】) printf(”直角三角形”): else printf(”一般三角形”): } else printf(”不能组成三角形”); } 13.以下程序的功能是判断输入的年份是否是闰年。请在【】内填入正确内容。 #include ”stdio.h” main() { int year, flag;
相关推荐: