版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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 int ma
2、in() int a,b,c; printf(请输入三个整数:); scanf(%d,%d,%d,&a,&b,&c); if (ab) if (bc) printf(max=%dn,c); else printf(max=%dn,b); else if (ac) printf(max=%dn,c); else printf(max=%dn,a); return 0;4-4-2#include int main() int a,b,c,temp,max; printf(请输入三个整数:); scanf(%d,%d,%d,&a,&b,&c); temp=(ab)?a:b; /*将a和b中的大者存入
3、temp中*/ max=(tempc)?temp:c; /*将a和b中的大者与c比较,取最大者*/ printf(三个整数的最大数是%dn,max); return 0;4-5-2#include #include #define M 1000int main() int i,k; printf(请输入一个小于%d的整数i:,M); scanf(%d,&i); while (iM) printf(输入的数不符合要求,请重新输入一个小于%d的整数i:,M); scanf(%d,&i); k=sqrt(i); printf(%d的平方根的整数部分是:%dn,i,k); return 0;4-5#i
4、nclude #include #define M 1000int main() int i,k; printf(请输入一个小于%d的整数i:,M); scanf(%d,&i); if (iM) printf(输入的数不符合要求,请重新输入一个小于%d的整数i:,M); scanf(%d,&i); k=sqrt(i); printf(%d的平方根的整数部分是:%dn,i,k); return 0;4-6.#include int main() int x,y; printf(输入x:); scanf(%d,&x); if(x1) /* x1 */ y=x; printf(x=%3d, y=x=
5、%dn ,x,y); else if(x10) /* 1=x=10 */ y=3*x-11; printf(x=%d, y=3*x-11=%dn,x,y); return 0;4-7-1#include int main() int x,y; printf(enter x:); scanf(%d,&x); y=-1; if(x!=0) if(x0) y=1; else y=0; printf(x=%d,y=%dn,x,y); return 0;4-7-2#include int main() int x,y; printf(please enter x:); scanf(%d,&x); y=0
6、; if(x=0) if(x0) y=1; else y=-1; printf(x=%d,y=%dn,x,y); return 0;4-8#include int main() float score; char grade; printf(请输入学生成绩:); scanf(%f,&score); while (score100|score0)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;
7、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 #include int main() int num,indiv,ten,hundred,thousand,ten_thousand,place; /分别代表个位,十位,百位,千位,万位和位数 printf(请输入一个整数(0-99999):); scanf(%d,&num); if (num9999) plac
8、e=5; else if (num999) place=4; else if (num99) place=3; else if (num9) 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*
9、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,hun
10、dred,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); print
11、f(n反序数字为:); printf(%dn,indiv); break; return 0; 4-10-1#include int main() int i; double bonus,bon1,bon2,bon4,bon6,bon10; bon1=100000*0.1; bon2=bon1+100000*0.075; bon4=bon2+100000*0.05; bon6=bon4+100000*0.03; bon10=bon6+400000*0.015; printf(请输入利润i:); scanf(%d,&i); if (i=100000) bonus=i*0.1; else if (
12、i=200000) bonus=bon1+(i-100000)*0.075; else if (i=400000) bonus=bon2+(i-200000)*0.05; else if (i=600000) bonus=bon4+(i-400000)*0.03; else if (i=1000000) bonus=bon6+(i-600000)*0.015; else bonus=bon10+(i-1000000)*0.01; printf(奖金是: %10.2fn,bonus); return 0; 4-10-2#include int main() int i; double bonus
13、,bon1,bon2,bon4,bon6,bon10; int branch; bon1=100000*0.1; bon2=bon1+100000*0.075; bon4=bon2+200000*0.05; bon6=bon4+200000*0.03; bon10=bon6+400000*0.015; printf(请输入利润i:); scanf(%d,&i); branch=i/100000; if (branch10) branch=10; switch(branch) case 0:bonus=i*0.1;break; case 1:bonus=bon1+(i-100000)*0.075
14、;break; case 2: case 3: bonus=bon2+(i-200000)*0.05;break; case 4: case 5: bonus=bon4+(i-400000)*0.03;break; case 6: case 7: case 8: case 9: bonus=bon6+(i-600000)*0.015;break; case 10: bonus=bon10+(i-1000000)*0.01; printf(奖金是 %10.2fn,bonus); return 0; 4-11#include int main() int t,a,b,c,d; printf(请输入
15、四个数:); scanf(%d,%d,%d,%d,&a,&b,&c,&d); printf(a=%d,b=%d,c=%d,d=%dn,a,b,c,d); if (ab) t=a;a=b;b=t; if (ac) t=a;a=c;c=t; if (ad) t=a;a=d;d=t; if (bc) t=b;b=c;c=t; if (bd) t=b;b=d;d=t; if (cd) t=c;c=d;d=t; printf(排序结果如下: n); printf(%d %d %d %d n ,a,b,c,d); return 0; 4-12#include 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):);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026校招:江西交通投资集团面试题及答案
- 2026校招:活动策划面试题及答案
- 2026校招:护士题目及答案
- 2026校招:杭州联合银行试题及答案
- 2026校招:福建投资开发集团试题及答案
- 2025-2026学年意大利字体教学设计
- 2025-2026学年湖南省长沙一中高三(上)期末地理试卷
- 2026年山西职业技术学院单招职业技能测试题库附参考答案详解(研优卷)
- 2026年广西信息职业技术学院单招职业技能测试题库及答案详解(真题汇编)
- 2026年广东女子职业技术学院单招职业适应性考试题库附答案详解(模拟题)
- 2025年内蒙古建筑职业技术学院单招职业技能考试试题及答案解析
- 2026年官方标准版离婚协议书
- 化验室复工安全培训课件
- 空间数据类型及表示
- 葫芦岛九江220千伏输变电工程环评报告
- 高速公路项目质量管理办法实施细则
- 半导体物理与光电器件课件
- 04第四章-火箭导弹的气动布局
- 世界现代设计史第二版第三章现代设计的前奏课件
- 齐鲁医学妊娠期急腹症
- 【部编版】六年级道德与法治下册全册课件
评论
0/150
提交评论