【C语言学习】《C Primer Plus》第7章 C控制语句:分支与跳转

 

学习总结

 

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

 

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

 

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

 

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

 

5、编程题(题1):

复制代码
 1 #include <stdio.h>
 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 <stdio.h>
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值