选择结构程序设计.docx
- 文档编号:30093481
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:17
- 大小:27.61KB
选择结构程序设计.docx
《选择结构程序设计.docx》由会员分享,可在线阅读,更多相关《选择结构程序设计.docx(17页珍藏版)》请在冰豆网上搜索。
选择结构程序设计
第4章选择结构程序设计
习题与解答
4.1设有:
inta=1,b=2,c=3,d=4,m=2,n=2;则下列表达式的值应为多少?
(1)a||b+c&&d-c
(2)!
((a
c||1)
(3)(m=a>b)&&(n=c>d)
(4)!
(a+b)+c-1&&b+c/2
解:
(1)1
(2)0(3)0(4)1
4.2写出下列各程序的输出。
(1)main()
{inta,b,s;
scanf("%d%d",&a,&b);
s=a;
if(a
s*=s;
printf("%d\n",s);
}_
(2)main()
{inta=2,b=-1,c=2;
if(a
if(b<0)c=0;
elsec+=1;
printf("%d\n",c);
}_
(3)main()
{inta=1,b=2,c=3;
if(a>b)
if(a>c)printf("%d",a);
elseprintf("%d",b);
printf("%d\n",c);
}_
(4)main()
{intx=1,y=0,a=0,b=0;
switch(x)
{case1:
switch(y)
{case0:
a++;break;
case1:
b++;break;}
case2:
a++;b++;break;
}
printf("a=%d,b=%d",a,b);
}_
解:
(1)输出变量a,b中最大值的平方。
(2)2
(3)3
(4)a=2,b=1
4.3阅读下列程序,并回答问题。
(1)当a=1,b=3,c=5,d=4时,执行完下面程序段后x的值是多少?
if(a
if(c else if(a if(b elsex=3; elsex=6; elsex=7; 解: 执行完程序段后x的值是2。 程序段中if与else的匹配情况如下: if(a if(c else if(a if(b elsex=3; elsex=6; elsex=7; (2)main() {inta,b,m,n; scanf("%d%d",&a,&b); m=n=1; if(a>0)m=m+n; if(a elseif(a==b)n=5; elsen=n+m; printf("m=%dn=%d\n",m,n); }_ 当输入为–1└┘-2时,程序的运行结果是什么? 当输入为1└┘0时,程序的运行结果是什么? 为了输出n=4,变量a和b应具备什么条件? 解: 当输入为–1└┘-2时,程序的运行结果是: m=1n=2 当输入为1└┘0时,程序的运行结果是: m=2n=3 若要输出n=4,需要a>0且a 4.4有一函数: x y=x-1(x=0) x+1(0 分别用if-else语句和嵌套的if语句编写程序,要求输入x的值,输出y的值。 解: 方法1: main() {floatx,y; printf("inputx: "); scanf("%f",&x); y=x; if(x==0)y=x-1; if(0 printf("y=%f\n",y); }_ 运行结果: inputx: 0 -1 inputx: 9 10 inputx: 12 12 方法2: main() {floatx,y; printf("inputx: "); scanf("%f",&x); if(x==0)y=x-1; elseif(0 elsey=x; printf("y=%f",y); }_ 运行结果: inputx: 0 -1 inputx: 8 9 inputx: 20 20 4.5编写程序,输入一个字符,如果是大写字母,则把它变成小写字母;如果是小写字母,则把它变成大写字母;其它字符不变。 解: main() {charch; scanf("%c",&ch); if(ch>=’A’&&ch<=’z’)ch=ch+32;/*小写字母与大写字母的ASCII值之差为32。 */ elseif(ch>='a'&&ch<='z')ch=ch-32; printf("%c",ch); } 4.6试编程判断输入的正整数是否既是5又是7的整倍数。 若是,则输出yes;否则输出no。 解: main() {intx; printf(“inputx: ”); scanf("%d",&x); if(x%5==0&&x%7==0) printf("yes"); else printf("no"); } 运行结果: inputx: 35 yes inputx: 25 no 4.7编写程序,对任意输入的两个实型数以及一个运算符(+、-、*、/),能够输出算式和计算结果,如: 输入: 3+5 显示: 3+5=8 (提示: 用switch语句实现)。 解: #include"stdio.h" #include"stdio.h" voidmain(void) {floatdata1,data2,data3;charop; printf("\nTypeinyourexpression: "); scanf("%f%c%f",&data1,&op,&data2); switch(op) {case'+': data3=data1+data2;break; case'-': data3=data1-data2;break; case'*': data3=data1*data2;break; case'/': if(data2==0) {printf("\nDivisionbyzero1"); exit (1);} data3=data1/data2;break; } printf("Thisis%6.2f%c%6.2f=%6.2f\n",data1,op,data2,data3); } 运行结果: Typeinyourexpression: 2*3 Thisis2.00*3.00=6.00 4.8某服装店经营套服,也单件出售。 若买的不少于50套,每套80元;不足50套的每套90 元;只买上衣每件60元;只买裤子每条45元。 编写程序,对输入的件数计算应付金额。 解: main() {intc,t,m; printf("inputthenumberofcoatandtrousersyourwantbuy: \n"); scanf("%d%d",&c,&t); if(c==t)/*服装成套,分两种情况: 大于等于50和小于50*/ if(c>=50)m=c*80; elsem=c*90; else/*服装不成套,分两种情况: 上衣多于或少于裤子*/ if(c>t)/*上衣多于裤子,分两种情况: 成套的数量多于或少于50*/ if(t>=50)m=t*80+(c-t)*60; elsem=t*90+(c-t)*60; else/*上衣少于裤子,分两种情况: 成套的数量多于或少于50*/ if(c>=50)m=c*80+(t-c)*45; elsem=c*90+(t-c)*45; printf("%d",m); } 运行结果: inputthenumberofcoatandtrousersyourwantbuy: 105 750 4.9编一程序,对于给定的一个百分制成绩,输出相应的五分制成绩。 设: 90分以上为‘A’, 80-89分为‘B’,70-79分为‘C’,60-69分为‘D’,60分以下为‘E’(用switch实现)。 解: main() { intscore,temp,log;chargrade; log=1; while(log) {printf("enterscore: "); scanf("%d",&score); if((score>100)||(score<0)) printf("\nerror,tryagain! \n"); elselog=0; } if(score==100)temp=9; elsetemp=(score-score%10)/10; switch(temp) {case0: case1: case2: case3: case4: case5: grade='E';break; case6: grade='D';break; case7: grade='C';break; case8: grade='B';break; case9: grade='A'; } printf("score=%d,grade=%c\n",score,grade); } 运行结果: enterscore: 85 score=85,grade=B 补充题与题解 4.1求ax2+bx+c=0方程的解。 解: #include“math.h” main() {floata,b,c,disc,x1,x2,realpart,imagpart; scanf(“5f,%f,5f”,&a,&b,&c); print(“Theequation”); if(fabs(a)<=1e-6)/*二次系数a为0.*/ printf(“isnotquadratic”); else {disc=b*b-4*a*c; if(fabs(disc)<=1e-6)/*判别式为零.*/ printf(“hastwoequalroots: %8.4f\n”,-b/(2*a)); elseif(disc>1e-6)/*判别式大于0*/ {x1=(-b+sqrt(disc))/(2*a); x2=(=b-sqrt(disc))/(2*a); printf(“hasdistinctrealroots: %8.4fand%8.4f\n”,x1,x2); } else/*判别式小于0,方程无实根*/ {realpart=-b/(2*a);/*实部*/ imagpart=sqrt(-disc)/(2*a);/*虚部*/ printf(“hascomplexroots: \n); printf(“%8.4f+%8.4fi\n”,realpart,imagpart); printf(“%8.4f-%8.4fi\n”,realpart,imagpart); } } } 运行结果: 1,2,1 Theequationhastwoequalroots: -1.0000 1,2,2 Theequationhascomplexroots: -1.0000+1.0000i -1.0000-1.0000i 2,6,1 Theequationhasdistinctrealroots: -0.1771and–2.8229 4.2编程序要求输入一个小写字母,将字母循环后移5个位置后输出。 如’a’变成’f’,’w’变成’b’. 解: #include"stdio.h" main() {charc; c=getchar(); if(c>='a'&&c<='u') c=c+5; elseif(c>='v'&&c<='z') c=c-21;/*26个小写字母,每个字母的ASCII码值相差1.*/ putchar(c); } 运行结果: v a b g 4.3根据以下函数关系,对输入的每个x值,计算出相应的y值。 解: main() {intx,c,m; floaty; printf(“inputx: ”); scanf("%d",&x); if(x<0)c=-1; elsec=x/10; switch(c) {case-1: y=0;break; case0: y=x;break; case1: y=10;break; case2: case3: y=-0.5*x+20;break; default: y=-2;/*x在>=40的范围即不在函数自变量的范围内*/ } if(y! =-2)printf("y=%f\n",y); elseprintf("error\n"); }_ 运行结果: inputx: -1 0.000000 inputx: 8 8.00000 inputx: 12 12.000000 inputx: 25 7.500000 4.4输入年份year和月month,求该月有多少天。 判断是否为润年,可用如下C语言表达式: year%4==0&&year%100! =0||year%400==0. 若表达式成立(即表达式为1),则year为润年;否则,表达式不成立(即值为零0),year为平年。 解: #include"stdio.h" main() {intyear,month,days; printf("Enteryearandmonth: "); scanf("%d%d",&year,&month); switch(month) {case1: case5: case7: case8: case10: case12: /*处理大月*/ days=31;break; case4: case6: case9: case11: /*处理小月*/ days=30;break; case2: /*处理平月*/ if(year%4==0&&year%100! =0||year%400==0) days=29;/*如果是润年*/ else days=28;break;/*不是润年*/ default: printf("Inputerror! \n");/*月份错误*/ days=0; } if(days! =0) printf("%d,%dis%ddays\n",year,month,days); } 运行结果: Enteryearandmonth: 200110 200110is31days Enteryearandmonth: 20002 20002is29days 4.5给定一个不多于5位的正整数,要求: ①求它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字。 例如原数为321,应输出123。 解: main() { longintnum; intindiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位、十位、百位、千位、万位和位数。 */ printf("请输入一个整数(0~99999): "); scanf("%ld",&num); if(num>9999) place=5; elseif(num>999) place=4; elseif(num>99) place=3; elseif(num>9) place=2; elseplace=1; printf("place=%d\n",place); printf("每位数字为: "); ten_thousand=num/10000; thousand=(int)(num-ten_thousand*10000)/1000; hundred=(int)(num-ten_thousand*10000-thousand*1000)/100; ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10; indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10); switch(place) {case5: printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv); printf("\n反序数字为: "); printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand); break; case4: printf("%d,%d,%d,%d",thousand,hundred,ten,indiv); printf("\n反序数字为: "); prinft("%d%d%d%d\n",indiv,ten,hundred,thousand); break; case3: printf("%d,%d,%d",hundred,ten,indiv); printf("\n反序数字为: "); printf("%d%d%d\n",indiv,ten,hundred); break; case2: printf("%d,%d",ten,indiv); printf("\n反序数字为: "); printf("%d%d\n",indiv,ten); break; case1: printf("%d",indiv); printf("\n反序数字为: "); printf("%d\n",indiv); break; } } 运行结果: 请输入一个整数(0~99999): 98765 位数=5 每位数字为: 9,8,7,6,5 反序数字为: 56789 _
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 选择 结构 程序设计