第05章_选择详解.pptVIP

  • 2
  • 0
  • 约 38页
  • 2017-04-15 发布于湖北
  • 举报
主要内容 5.1 关系运算符和关系表达式 5.2 逻辑运算符和逻辑表达式 5.3 if语句 5.4 switch语句 5.5 程序举例 §5.1 关系运算符和关系表达式 §5.1 关系运算符和关系表达式(续) §5.2 逻辑运算符和逻辑表达式 §5.2 逻辑运算符和逻辑表达式(续) §5.2 逻辑运算符和逻辑表达式 §5.2 逻辑运算符和逻辑表达式(续) §5.2 逻辑运算符和逻辑表达式(续) §5.2 逻辑运算符和逻辑表达式(续) §5.3 if语句 §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.3 if语句(续) §5.4 switch语句 §5.4 switch语句(续) §5.4 switch语句(续) §5.4 switch语句(续) §5.5 程序举例(续) §5.5 程序举例(续) §5.5 程序举例(续) §5.5 程序举例(续) 本章结束 例5.6 求ax2+bx+c=0方程的解。 基本的算法: ① a=0,不是二次方程。 ② b2-4ac=0,有两个相等实根。 ③ b2 -4ac>0,有两个不等实根。 ④ b2 -4ac<0,有两个共轭复根。 #include stdio.h #include math.h void main ( ) {float a,b,c,disc,x1,x2,realpart,imagpart; scanf(%f,%f,%f,a,b,c); printf(the equation ); if(fabs(a)=1e-6) printf(is not a quadratic\\n); else { disc=b*b-4*a*c; if(fabs(disc)=1e-6) printf(has two equal roots:%8.4f\n,-b/(2*a)); else if(disc1e-6) {x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf(“has distinct real roots:%8.4f and %8.4f\n”,x1,x2); } else {realpart=-b/(2*a); imagpart=sqrt(-disc)/(2*a); printf(“ has complex roots∶\n”); printf(“%8.4f+%8.4fi\n”,realpart,imagpart); printf(“%8.4f-%8.4fi\n”,realpart,imagpart); } }} 例5.7 运输公司对用户计算运费。 路程(s)越远,每公里运费越低。标准如下:    s<250 没有折扣 250≤s<500 2%折扣 500≤s<1000 5%折扣 1000≤s<2000 8%折扣 2000≤s<3000 10%折扣 3000≤s 15%折扣 设每公里每吨货物的基本运费为p,货物重为w,距离为s, 折扣为d,则总运费f的计算公式为: f=p*w*s*(1-d)  分析折扣变化的规律性: 折扣的“变化点”都是250的倍数 在横轴上加一种坐标c,c的值为s/250。c代表250的倍数。 c1,无折扣; 1≤c<2,折扣d=2%; 2≤c<4,d=5%; 4≤c<8,d=8%; 8≤c<12,d=10%; c≥12,d=15%。 #include stdio.h void main ( ) {int c,s; float p,w,d,f; scanf(%f,%f,%d,p,w,s); if(s=3000) c=12; else c=s/250; switch(c){ case 0:d=0;break; case 1:d=2;break; case 2:case 3:d=5;break; case 4:case 5:case 6:case 7:d=8;brea

文档评论(0)

1亿VIP精品文档

相关文档