c语言-C程序设计(第四版)谭浩强-课后习题答案第4章(共8页).doc
精选优质文档-倾情为你奉上C程序设计(第四版)谭浩强_课后习题答案第4章第4章选择结构程序设计854.1选择结构和条件判断854.2用if语句实现选择结构874.2.1用if语句处理选择结构举例874.2.2if语句的一般形式 894.3关系运算符和关系表达式914.3.1关系运算符及其优先次序914.3.2关系表达式924.4逻辑运算符和逻辑表达式924.4.1逻辑运算符及其优先次序934.4.2逻辑表达式944.4.3逻辑型变量964.5条件运算符和条件表达式974.6选择结构的嵌套994.7用switch语句实现多分支选择结构1024.8选择结构程序综合举例105习题1114-4-1#include <stdio.h>int main() int a,b,c; printf("请输入三个整数:"); scanf("%d,%d,%d",&a,&b,&c); if (a<b) if (b<c) printf("max=%dn",c); else printf("max=%dn",b); else if (a<c) printf("max=%dn",c); else printf("max=%dn",a); return 0;4-4-2#include <stdio.h>int main() int a,b,c,temp,max; printf("请输入三个整数:"); scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)?a:b; /*将a和b中的大者存入temp中*/ max=(temp>c)?temp:c; /*将a和b中的大者与c比较,取最大者*/ printf("三个整数的最大数是%dn",max); return 0;4-5-2#include <stdio.h>#include <math.h>#define M 1000int main() int i,k; printf("请输入一个小于%d的整数i:",M); scanf("%d",&i); while (i>M) printf("输入的数不符合要求,请重新输入一个小于%d的整数i:",M); scanf("%d",&i); k=sqrt(i); printf("%d的平方根的整数部分是:%dn",i,k); return 0;4-5#include <stdio.h>#include <math.h>#define M 1000int main() int i,k; printf("请输入一个小于%d的整数i:",M); scanf("%d",&i); if (i>M) printf("输入的数不符合要求,请重新输入一个小于%d的整数i:",M); scanf("%d",&i); k=sqrt(i); printf("%d的平方根的整数部分是:%dn",i,k); return 0;4-6.#include <stdio.h>int main() int x,y; printf("输入x:"); scanf("%d",&x); if(x<1) /* x<1 */ y=x; printf("x=%3d, y=x=%dn" ,x,y); else if(x<10) /* 1=<x<10 */ y=2*x-1; printf("x=%d, y=2*x-1=%dn",x,y);else /* x>=10 */ y=3*x-11; printf("x=%d, y=3*x-11=%dn",x,y); return 0;4-7-1#include <stdio.h>int main() int x,y; printf("enter x:"); scanf("%d",&x); y=-1; if(x!=0) if(x>0) y=1; else y=0; printf("x=%d,y=%dn",x,y); return 0;4-7-2#include <stdio.h>int main() int x,y; printf("please enter x:"); scanf("%d",&x); y=0; if(x>=0) if(x>0) y=1; else y=-1; printf("x=%d,y=%dn",x,y); return 0;4-8#include <stdio.h>int main() float score; char grade; printf("请输入学生成绩:"); scanf("%f",&score); while (score>100|score<0)printf("n 输入有误,请重输");scanf("%f",&score); switch(int)(score/10) case 10:case 9: grade='A'break;case 8: grade='B'break;case 7: grade='C'break;case 6: grade='D'break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E' printf("成绩是 %5.1f,相应的等级是%cn ",score,grade);return 0;4-9#include <stdio.h>#include <math.h>int main() int num,indiv,ten,hundred,thousand,ten_thousand,place; /分别代表个位,十位,百位,千位,万位和位数 printf("请输入一个整数(0-99999):"); scanf("%d",&num); if (num>9999) place=5; else if (num>999) place=4; else if (num>99) place=3; else if (num>9) place=2; else place=1; printf("位数:%dn",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) case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv); printf("n反序数字为:"); printf("%d%d%d%d%dn",indiv,ten,hundred,thousand,ten_thousand); break; case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv); printf("n反序数字为:"); printf("%d%d%d%dn",indiv,ten,hundred,thousand); break; case 3:printf("%d,%d,%d",hundred,ten,indiv); printf("n反序数字为:"); printf("%d%d%dn",indiv,ten,hundred); break; case 2:printf("%d,%d",ten,indiv); printf("n反序数字为:"); printf("%d%dn",indiv,ten); break; case 1:printf("%d",indiv); printf("n反序数字为:"); printf("%dn",indiv); break; return 0; 4-10-1#include <stdio.h>int main() int i; double bonus,bon1,bon2,bon4,bon6,bon10; bon1=*0.1; bon2=bon1+*0.075; bon4=bon2+*0.05; bon6=bon4+*0.03; bon10=bon6+*0.015; printf("请输入利润i:"); scanf("%d",&i); if (i<=) bonus=i*0.1; else if (i<=) bonus=bon1+(i-)*0.075; else if (i<=) bonus=bon2+(i-)*0.05; else if (i<=) bonus=bon4+(i-)*0.03; else if (i<=) bonus=bon6+(i-)*0.015; else bonus=bon10+(i-)*0.01; printf("奖金是: %10.2fn",bonus); return 0; 4-10-2#include <stdio.h>int main() int i; double bonus,bon1,bon2,bon4,bon6,bon10; int branch; bon1=*0.1; bon2=bon1+*0.075; bon4=bon2+*0.05; bon6=bon4+*0.03; bon10=bon6+*0.015; printf("请输入利润i:"); scanf("%d",&i); branch=i/; if (branch>10) branch=10; switch(branch) case 0:bonus=i*0.1;break; case 1:bonus=bon1+(i-)*0.075;break; case 2: case 3: bonus=bon2+(i-)*0.05;break; case 4: case 5: bonus=bon4+(i-)*0.03;break; case 6: case 7: case 8: case 9: bonus=bon6+(i-)*0.015;break; case 10: bonus=bon10+(i-)*0.01; printf("奖金是 %10.2fn",bonus); return 0; 4-11#include <stdio.h>int main() int t,a,b,c,d; printf("请输入四个数:"); scanf("%d,%d,%d,%d",&a,&b,&c,&d); printf("a=%d,b=%d,c=%d,d=%dn",a,b,c,d); if (a>b) t=a;a=b;b=t; if (a>c) t=a;a=c;c=t; if (a>d) t=a;a=d;d=t; if (b>c) t=b;b=c;c=t; if (b>d) t=b;b=d;d=t; if (c>d) t=c;c=d;d=t; printf("排序结果如下: n"); printf("%d %d %d %d n" ,a,b,c,d); return 0; 4-12#include <stdio.h>int main() int h=10; float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4; printf("请输入一个点(x,y):"); scanf("%f,%f",&x,&y); d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1); d3=(x-x2)*(x-x2)+(y-y2)*(y-y2); d4=(x-x3)*(x-x3)+(y-y3)*(y-y3); if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf("该点高度为 %dn",h); return 0; 专心-专注-专业