学习总结
1、if…else…从语义上看就能出用途,跟其他语言没差多少,只需要记住,世界上最遥远的距离之一:我走if你却走else。
2、根据个人几年的编程经验,太多的if…else…嵌套会加大代码的可读性和维护难度。个人认为代码最好不要超过三层if…else…的嵌套,否则最好使用布尔值控制流程。
3、逻辑运算符优先级:!>&&>||
4、运行到continue语句将导致剩余的迭代部分被忽略,开始下一次迭代。continue仅用于循环,而break语句用于循环和switch中。
5、编程题(题1):
#include <stdio.h> int main(){
int space=,newline=,other=;
char ch;
printf("please enter something:\n");
while((ch=getchar())!='#'){
if(ch=='\n'){
newline+=;
}else if(ch==' '){
space+=;
}else{
other+=;
}
}
printf("space is %d\n",space);
printf("newline is %d\n",newline);
printf("other is %d\n",other);
return ;
}
运行结果:
please enter something:
hello world!
hi nihao.
#ABC
space is 2
newline is 2
other is 19
6、编程题(题11):
#include <stdio.h>
#define ARTICHOKE_UNIT_PRIC 1.25
#define BEET_UNIT_PRICE 0.65
#define CAROTA_UNIT_PRICE 0.89
#define DISCOUNT 0.05
#define T_0_5 3.50
#define T_5_20 10.00
#define T_20_ 0.1 int main(){
double a,b,c,ap,bp,cp,ac,bc,cc,sc,dc,tc;
ap=ARTICHOKE_UNIT_PRIC;
bp=BEET_UNIT_PRICE;
cp=CAROTA_UNIT_PRICE;
printf("how many artichoke you want(pound):");
scanf("%lf",&a);
if(a==)return ; printf("how many beet you want(pound):");
scanf("%lf",&b);
if(b==)return ; printf("how many carota you want(pound):");
scanf("%lf",&c);
if(c==)return ; printf("\n------UNIT PRICE------\n");
printf("artichoke's unit price is $%.2f(one pound) \n",ap);
printf("beet's unit price is $%.2f(one pound)\n",bp);
printf("carota'unit price is $%.2f(one pound)\n",cp); printf("\n------ORDER------\n");
printf("artichoke:%.2fpound\n",a);
printf("beet:%.2fpound\n",b);
printf("carota:%.2fpound\n",c); printf("\nartichoke is $%.2f",a*ap);
printf("\nbeet is $%.2f",b*bp);
printf("\ncarota is $%.2f\n",c*cp);
sc=a*ap+b*bp+c*cp;
printf("\ntotal cost is $%.2f",sc);
dc=sc>?sc*DISCOUNT:;
printf("\ndiscount is $%.2f",dc); printf("\ntotal weight is %.2f",a+b+c);
if(<(a+b+c)<=){
tc=T_0_5;
}
if(<(a+b+c) && (a+b+c)<=){
tc=T_5_20;
}
if((a+b+c)>){
tc=+(a+b+c)*0.1;
}
printf("\nttransport cost is $%.2f",tc);
printf("\norder cost is $%.2f\n",sc-dc+tc); return ;
}
运行结果:
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