程序设计方法04.pptVIP

  • 2
  • 0
  • 约1.27万字
  • 约 36页
  • 2018-01-21 发布于湖北
  • 举报
程序设计方法04

例4.9 根据输入的字母输出相应的字符串 #include stdio.h void main() { int c; printf(Enter m or n or h or other:); c=getchar(); switch(c) { case m: printf( Good morning!\n);break; case n: printf( Good night!\n); break; case h: printf( Hello!\n); break; default : printf( ????????\n); } } m? Good morning! 例4.10 求ax2+bx+c=0方程的解 #include stdio.h #includemath.h void main() { float a,b,c,d,disc,x1,x2,realpart,imagpart; scanf(“%f,%f,%f”,a,b,c); printf(“The equation”); if(fabs(a)=1e-6) printf(“is not a quadratie”); 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); } } } 例4.11 计算运费 #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;break; case 8: case 9: case 10: case 11: d=10;break; case 12: d=15;break; } f=p*w*s*(1-d/100.0); printf(“freight=%15.4f”,f); } 本章要求 关系运算符、逻辑运算符,运算符的优先级。 if语句、switch语句的概念及其应用。 补充习题 编程实现:输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出a+b之和。 编程实现:输入一个整数,判断它能否被3、5、7整除,并输出以下信息之一: 能同时被3、5、7整除; 能被其中两数(要指出哪两个)整除; 能被其中一个数(要指出哪一个)个整除; 不能被3、5、7任一个整除。 * * * * * * * * * * * * * * * * * * * * * * * * C语言程序设计 第四章 选择结构程序设计

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档