else
{if(tall>=170)printf(“A”); else printf(“B”); } }
4.根据变量i的值,输出字符。 #include
printf(“please input i:”); scanf(“%d”,%i); switch(i)
{case 1:printf(“A/n”); case 2:printf(“B/n”); case 3:printf(“C/n”); case 4:printf(“D/n”); default:printf(“E\\n); } }
5.根据输入的学生成绩判断等级。当成绩score>=90时,为A等;成绩70=
scanf(“%d”,&score); switch(score/10) {case 10:
case 9:printf(“%d:A\\n”,score);break; case 8:
case 7:printf(“%d:B\\n”,score);break; case 6:printf(“%d:C\\n”,score);break; default:printf(“%d:D\\n”,score); } }
6.求方程a*x*x+b*x+c=0的实数解。 #include
{float a,b,c,x1,x2,disc; printf(“input a b c:”); scanf(“%f %f”,&a,&b,&c); if(fabs(a)<1e-6)
printf(“The equation has not real root”); else
if(fabs(disc)<1e-6)
printf(“”The equation has two equal roots:%8.4f”.-b/(2*a)); else
{x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a);
printf(“The equation has distinct real roots:%8.4f”,x1,x2); } }
}
7.输入年号,判断它是否闰年。 #include
{unsigned int year,leap; printf(“Enter year:”); scanf(“%d”,&year); if(year@0==0) leap=1;
else
{if(year@0==0&&year0!=0) leap=1;
else leap=0; }
if(leap==1)printf(“%d:is a leap year.”,year); else printf(“%d is not a leap year.”,year); 8.输入一个由两个数据和一个算术运符组成的表达式,根据运算符完成相应的运算,并将结果输出。
void main()
{int a,b;char c;
scanf(“%d%c%d”,&a,&c,&b); switch(c)
{case ‘+’:printf(“a+b=%d”,a+b);break; case ‘-‘:printf(“a-b=%d”,a-b);break; case ‘*’:printf(“a*b=%d”,a*b);break; case ‘/’:{if(b!=0)printf(“a/b=%d”,a/b); else printf(“b=0”);
case ‘%’:{if(b!=0)printf(“a mod b=%d”,a%b); else printf(“b=0”);}break; default:printf(“Data out of range”); } }
9.输入两个字符,若这两个字符的序号(ASCII码)之差为偶数,则输出它们的后继字符,否则输出它们的前驱字符。 main() {
char a,b;
printf(\ scanf(\ if((a-b)/2==0)
printf(\
else printf(\
}
10.输入整数 a 和 b ,如果 a 能被 b 整除,就输出算式和商,否则输出算式、整数商和余数。 main() {
int a,b;
printf(\ scanf(\ if(b!=0&&a%b==0)
printf(\ else
printf(\}
11.输入某个点 A 的平面坐标 (x,y),判断(输出)A点是在圆内、圆外还是在圆周上,其中圆心坐标为 (2,2),半径为 1。 include\
main() {
float x,y,d;
printf(\
scanf(\
d=sqrt((x-2)*(x-2)+(y-2)*(y-2)); if(d>1)printf(\
else if(d<1)printf(\ else printf(\
}
12.输入年号和月份,输出这一年该月的天数(一个年份,先判断是否闰年)。 main() {
int y,m,d,f;
printf(\ scanf(\
f=(y%4==0&&y0!=0||y@0==0); if(m==2)
d=28+f; /*依据是否闰年决定2月份的实际天数,若是闰年,则f=1,d即为29天*/ else
d=31-(m==4)-(m==6)-(m==9)-(m==11);/*m==4时其值为1,则本月为30天,典型算法,重要啊*/
printf(\
}
13.有一函数
x-1 -5 编写一程序,要求输入 x 的值,输出 y 的值。分别用不嵌套的 if 语句,嵌套的 if 语句,switch语句编写。 答 使用不嵌套的 if 语句程序如下: main() { float x,y; printf(\ scanf(\ if(x>-5&&x<0)y=x-1; if(x==0)y=0; if(x>0&&x<8)y=x+1; printf(\} 使用嵌套的 if 语句程序如下: main() { float x,y; scanf(\ if(x>-5&&x<8) if(x<0)y=x-1; else if(x==0)y=0: else y=x+1; printf(\} main() float x,y; scanf(\ if(x<8) if(x>0)y=x+1; else if(x==0)y=0; else if(x>-5)y=x-1; printf(\} 使用多分支语句程序如下: main() { float x,y; int m; printf(\ scanf(\ if(x>-5&&x<8) if(x<0)m=-1; else if(x==0)m=0: else m=1; switch(m) { case -1:y=x-1;break; case 0:y=0;break; case 1:y=x+1;break; } printf(\} 以下为假设对int类型的变量进行处理,但毕竟不算完美 main() { int x,y; printf(\ scanf(\ switch(x) { case -4: case -3: case -2: case -1: y=x-1; break; case 0: y=0; break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: y=x+1; break; } printf(\} 第五章