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

本文共 3329 字,大约阅读时间需要 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/zhangyingai/p/7087387.html

你可能感兴趣的文章
BZOJ 1083: [SCOI2005]繁忙的都市
查看>>
Maven 编译
查看>>
《学习之道》第十章学习方法29还记得散步的好处嘛
查看>>
Git常用命令总结
查看>>
iOS获取设备IP地址
查看>>
JavaSE| String常用方法
查看>>
NRF51822配对绑定要点
查看>>
C语言博客作业—数据类型
查看>>
[leetcode]Count and Say
查看>>
cookie、session和token的概念入门
查看>>
保护网站页面内容+版权
查看>>
Golang模拟客户端POST表单功能文件上传
查看>>
重启进程
查看>>
js 进度条效果
查看>>
RelativeLayout
查看>>
distinct!!!!!!!!!!!!!!!!!!
查看>>
input type
查看>>
Javascript脚本 :Function 对象的定义和使用
查看>>
工作疑问之AWS ubuntu16.04 修改网卡名
查看>>
data:image/png;base64
查看>>