C语言基础练习题(1-3) 下载本文

内容发布更新时间 : 2024/4/30 21:34:58星期一 下面是文章的全部内容请认真阅读。

第二部分 选择结构 ·16·

2.2 填空题

1. 若从键盘输入58,则输出结果是________。

#include ”stdio.h” main() {

int a;

scanf(”%d”, &a);

if (a>50) printf(”%d”, a);

if (a>40) printf(”%d”,a); if (a>30) printf(”%d”,a); }

2. 设int x=9,y=8;表达式x==y+1的结果是________。

3. 定义int x,y;执行y=(x=1,++x,x+2);后, y的值是________。

4. 定义int x=10,y,z; 执行y=z=x; x=y==z;后, x的结果是________。 5. 设int a=1,b=2,c,d,e; 执行

c=(-a++)+(++b); d=(b--)+(++a)-a; e=(a/(++b))-(a/(--a));

请问a,b,c,d,e的结果是:________。 6. 设int a=2,b=3,c,d,e,f; 执行

c=(a++>= --b); d=(a==++b); e=(a--!=b); f=(++a>b--);

请问a,b,c,d,e,f的结果是:________。 7. 以下程序的运行结果是________。 #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. 以下程序的运行结果是________。 #include ”stdio.h” main()

{

int a,b,c,d,e; a=c=1;

第二部分 选择结构 ·17·

b=20; d=100;

if (!a) d=d++; else if (!b)

if (d) d= --d;

else d= d--;

printf(”%d\\n\\n”, d);

}

9. 以下程序的运行结果是________。

#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值。请在【】内填空。

x y 2

int x,y;

scanf(”%d”, &x); if (【1】) y=x*(x+2); else if (【2】) y=1/x; 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】)

第二部分 选择结构 ·18·

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); }

}

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;

printf(”please input the year to jude whether it is a leap year:”); scanf(”%d”,&year);

if (year@0==0) flag=1; else if (【1】) flag=1; else 【2】;

if (flag) printf(”%d is a leap year\\n”,year);

第二部分 选择结构 ·19·

else printf(”%d is not a leap year!\\n”,year);

}

14.以下程序是对用户输入的字母进行大小写转换。请在【】内填入正确内容。

#include ”stdio.h” main() {

char ch;

printf(”please input a letter:”); scanf(”%c”,&ch); if (【1】) ch=ch+32;

else if (ch>=’a’ && ch<=’z’) 【2】;

printf(” the converted letter is: %c\\n”,ch); }

15.以下程序是对从键盘输入的任何三个整数,求出其中的最小值。请在【】内填入正确内

容。

#include ”stdio.h” main()

{

int a,b,c,min;

printf(”please input three numbers:”); scanf(”%d%d%d”,&a,&b,&c); if (【1】) min=b; else

min=a; if (min>c)

【2】;

printf(”min=%d\\n”,min); }

16.以下程序实现这样的功能:商店卖西瓜,10斤以上的每斤0.15元,8斤以上的每斤0.3

元,6斤以上的每斤0.4元,4斤以上的每斤0.6元,4斤以下的每斤0.8元,从键盘输入西瓜的重量和顾客所付钱数,则输出应付款和应找钱数。请在【】内填入正确内容。 #include ”stdio.h” main()

{

float weight, money, rate;

printf(”the paid money of the client is:”); scanf(”%f”,&money);

printf(”the weight of the watermelon is:”); scanf(”%f”,&weight); if (【1】)

rate=0.15; else if (weight>8) rate=0.3; else if (weight>6) 【2】; else if (weight>4)

rate=0.6;

第二部分 选择结构 ·20·

【3】

rate=0.8;

printf(”the account payable of the watermelon is %f\\n”, weight*rate); printf(”the change for client is %f\\n”,money-weight*rate); }

17.以下程序段的运行结果是________。

#include ”stdio.h” main() {

char ch1=’a’,ch2=’A’; switch (ch1) { case ’a’:

switch (ch2)

{case ’A’: printf(”good!\\n”); break; case ’B’: printf(”bad!\\n”); break; }

case ’b’: printf(”joke\\n”); } }

18.根据以下函数关系,对输入的每个x值,计算出相应的y值。请在【】内填入正确内容。

x y x<0 0 0<=x<10 x 10<=x<20 10 20<=x<40 -0.5x+20 #include ”stdio.h” main()

{

int x, rate;

float y;

printf(”please input the value of x:”); scanf(”%d”,&x); if (【1】) rate= -1; else rate=【2】; switch(rate)

{ case –1: y=0; break;

case 0: y=x; break; case 1: y=10; break; case 2:

case 3: y=-0.5*x+20; break; default: y= -2;

}

if (【3】) printf(”y=%f\\n”,y);

else printf(”the value of x is invalid!\\n”);

}

19.以下程序实现的功能是:从键盘输入某年某月,输出该年份该月的天数。请在【】内填

入正确内容。 #include ”stdio.h” main()