博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转
阅读量:5908 次
发布时间:2019-06-19

本文共 3327 字,大约阅读时间需要 11 分钟。

 

学习总结

 

1、if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else。

 

2、根据个人几年的编程经验,太多的if…else…嵌套会加大代码的可读性和维护难度。个人认为代码最好不要超过三层if…else…的嵌套,否则最好使用布尔值控制流程。

 

3、逻辑运算符优先级:!>&&>||

 

4、运行到continue语句将导致剩余的迭代部分被忽略,开始下一次迭代。continue仅用于循环,而break语句用于循环和switch中。

 

5、编程题(题1):

1 #include 
2 3 int main(){ 4 int space=0,newline=0,other=0; 5 char ch; 6 printf("please enter something:\n"); 7 while((ch=getchar())!='#'){ 8 if(ch=='\n'){ 9 newline+=1;10 }else if(ch==' '){11 space+=1;12 }else{13 other+=1;14 }15 }16 printf("space is %d\n",space);17 printf("newline is %d\n",newline);18 printf("other is %d\n",other);19 return 0;20 }

运行结果:

please enter something:

hello world!

hi nihao.

#ABC

space is 2

newline is 2

other is 19

 

6、编程题(题11):

1 #include 
2 #define ARTICHOKE_UNIT_PRIC 1.25 3 #define BEET_UNIT_PRICE 0.65 4 #define CAROTA_UNIT_PRICE 0.89 5 #define DISCOUNT 0.05 6 #define T_0_5 3.50 7 #define T_5_20 10.00 8 #define T_20_ 0.1 9 10 int main(){11 double a,b,c,ap,bp,cp,ac,bc,cc,sc,dc,tc;12 ap=ARTICHOKE_UNIT_PRIC;13 bp=BEET_UNIT_PRICE; 14 cp=CAROTA_UNIT_PRICE; 15 printf("how many artichoke you want(pound):");16 scanf("%lf",&a);17 if(a==0)return 0;18 19 printf("how many beet you want(pound):");20 scanf("%lf",&b);21 if(b==0)return 0;22 23 printf("how many carota you want(pound):");24 scanf("%lf",&c);25 if(c==0)return 0;26 27 printf("\n------UNIT PRICE------\n");28 printf("artichoke's unit price is $%.2f(one pound) \n",ap);29 printf("beet's unit price is $%.2f(one pound)\n",bp);30 printf("carota'unit price is $%.2f(one pound)\n",cp);31 32 printf("\n------ORDER------\n");33 printf("artichoke:%.2fpound\n",a);34 printf("beet:%.2fpound\n",b);35 printf("carota:%.2fpound\n",c);36 37 printf("\nartichoke is $%.2f",a*ap);38 printf("\nbeet is $%.2f",b*bp);39 printf("\ncarota is $%.2f\n",c*cp);40 sc=a*ap+b*bp+c*cp;41 printf("\ntotal cost is $%.2f",sc);42 dc=sc>100?sc*DISCOUNT:0;43 printf("\ndiscount is $%.2f",dc);44 45 printf("\ntotal weight is %.2f",a+b+c);46 if(0<(a+b+c)<=5){47 tc=T_0_5;48 }49 if(5<(a+b+c) && (a+b+c)<=20){50 tc=T_5_20;51 }52 if((a+b+c)>20){53 tc=8+(a+b+c)*0.1;54 }55 printf("\nttransport cost is $%.2f",tc);56 printf("\norder cost is $%.2f\n",sc-dc+tc);57 58 return 0;59 }

运行结果:

how many artichoke you want(pound):123

how many beet you want(pound):234

how many carota you want(pound):343

 

------UNIT PRICE------

artichoke's unit price is $1.25(one pound)

beet's unit price is $0.65(one pound)

carota'unit price is $0.89(one pound)

 

------ORDER------

artichoke:123.00pound

beet:234.00pound

carota:343.00pound

 

artichoke is $153.75

beet is $152.10

carota is $305.27

 

total cost is $611.12

discount is $30.56

total weight is 700.00

ttransport cost is $78.00

order cost is $658.56

转载于:https://www.cnblogs.com/wcd144140/p/4512421.html

你可能感兴趣的文章
centos7环境安装ElasticSearch
查看>>
VidLoc: A Deep Spatio-Temporal Model for 6-DoF Video-Clip Relocalization
查看>>
SpringMVC RESTful风格URL处理带点的参数
查看>>
面试DB优化
查看>>
ORA-03137: TTC 协议内部错误: [12333] [4] [49] [51] [] [] [] []
查看>>
【laravel5.4】自定义404、503等页面
查看>>
如何的退出无响应的 SSH 连接
查看>>
Java对日期Date类进行加减运算,年份加减,月份加减
查看>>
使用Windows 2008R2中的NFS替代Samba协议,解决Windows 与Linux共享文件的问题
查看>>
深入理解Apache Flink核心技术
查看>>
SpringBoot系列: Json的序列化和反序列化
查看>>
移动端爬虫工具与方法介绍
查看>>
Linux Makefile 编译速度的优化【转】
查看>>
防止表单重复提交的几种思路
查看>>
IOS开发基础知识--碎片22
查看>>
heredoc和nowdoc的区别
查看>>
java学习笔记2015-6-6
查看>>
MAC 如何使用Github Desktop 客户端
查看>>
盗墓笔记——路由器密码破解
查看>>
akka---Getting Started Tutorial (Java): First Chapter
查看>>