C程序设计ch04 判定、分支和循环.ppt_第1页
C程序设计ch04 判定、分支和循环.ppt_第2页
C程序设计ch04 判定、分支和循环.ppt_第3页
C程序设计ch04 判定、分支和循环.ppt_第4页
C程序设计ch04 判定、分支和循环.ppt_第5页
已阅读5页,还剩51页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第四章 判定、分支和循环,4.1 分支 简单if语句 (p111115) 格式:if (expression) statement 执行过程:,例 计算(a+b)/(c-d)。 if (c-d!=0) ratio=(float)(a+b)/(c-d); printf(“ Ratio=%fn”,ratio); ,/* absolute value */ #include main() int x,y; printf(Enter an integer:); scanf(%d, ,例 求一个数的绝对值,运行:Enter an integer:-12 integer:-12-absolute valu

2、e :12,abs(int n) labs(long) fabs(double x) cabs(complex z),引入复合语句:用 括起来的一组语句 一般形式: 数据说明部分; 执行语句部分; ,例 对x与y两数处理使x值较y的大。,if else语句 p115 格式:if (expression) statement1 else statement2 执行过程:,例 if (c-d!=0) ratio=(float)(a+b)/(c-d); printf(“ Ratio=%fn”,ratio); else printf(“c-d is zeron”);,例 if (xy) max=x;

3、else max=y;,转为表达式: max = (xy) ? x : y 这里引入了运算符 p129 条件运算符 ?:,优先级: 13 结合方向:自右向左 如 ab?a:cd?c:d ab?a:(cd?c:d,应用扩展,if ( expr1 ) statement1 else if (expr2 ) statement2 else if (expr3 ) statement3 . else statementn ,执行过程:,if (value1000) takeoff=0.25; else if (value800) takeoff=0.2; else if (value600) take

4、off=0.15; else if (value400) takeoff=0.1; else takeoff=0;,例 百分制成绩转换为五级制 p122 80 to 100 Honours 60 to 79 First Division 50 to 59 Second Division 40 to 49 Third Division 0 to 39 Fail 例 累进电费 p124,例 根据税前收入计算税后收入,2000元起征 0% 不超过500的 5% 超过5002000的部分 10% 超过20005000的部分 15% 超过500020000的部分 20% 超过2000040000的部分

5、25% 超过4000060000的部分 30% 超过6000080000的部分 35% 超过80000100000的部分 40% 超过100000的部分 45%,Actual=(Income-Base)*对应税率对应速算扣除额,如 Income=8000 8000-2000=6000要纳税: 500 * 5% 1500 *10% 3000 *15% 1000 *20% =500*5%+1500*10%+3000*15%+2000*20%,/*char type*/ #include main() char c; printf(Enter a character:); c=getchar();

6、if(c=0 ,例 判断输入字符的类别,运行:Enter a character: The character is a control character,运行:Enter a character:8 The character is a digit,运行: Enter a character: D The character is a capital letter,运行: Enter a character: h The character is a lower letter,运行: Enter a character:F1 The character is other character

7、,#include ctype.h iscntrl() isalpha() isdigit() ispunct() isprint() islower() isupper() isalnum(),例: if (a=b) if(b=c) printf(“a=b=c”); else printf(“a!=b”);,实现if else 正确配对方法:加 ,/*Select Label*/ #include main() int c; printf(Enter m or n or h or other:); c=getchar(); switch(c) case m: printf(nGood mor

8、ning!n);break; case n: printf(nGood night!n); break; case h: printf(nHello!n); break; default : printf(n?n); break; ,例 根据输入字母输出字符串,4.2 switch语句(开关分支语句) p125,switch( 表达式) case E1: 语句组 1; break; case E2: 语句组 2; break; . case En: 语句组 n; break; default: 语句组 ; break; ,例 百分制成绩转换为五级制 p122 80 to 100 Honours

9、 60 to 79 First Division 50 to 59 Second Division 40 to 49 Third Division 0 to 39 Fail 用Switch Statement.,例 用if 和goto语句构成循环,求1+2+100,/*loop using goto statement*/ #include main() int i,sum=0; i=1; loop: if(i=100) sum+=i; i+; goto loop; printf(%d,sum); ,sum=0+1 sum=1+2=3 sum=3+3=6 sum=6+4 sum=4950+10

10、0=5050,4.3 循环 高级语言的初始阶段是用goto语句控制循环,例 从键盘输入一组数据,以0结束输入,求数据和,/*sum of data*/ #include main() int number,sum=0; read_loop: scanf(%d, ,例 输入10个男孩的身高和体重,统计身高超过170体重少于50公斤的人数。,例pp135 RANGE of Numbers: A survey of the computer macket show that personal computers are sold at varying costs bythe vendors.The

11、following is the list of costs(in hundreds)quoted by some vendors: 35.00, 40.50, 25.00, 31.25, 68.15, 47.00, 26.65, 29.00, 53.45, 62.50 Determine the average cost and the range of values.,例pp136 Pay-Bill Calculations: A manufacturing company has classified its executives into four levels for the ben

12、efit of certain perks. The levels and corresponding perks are shown below: Perks Level - Conveyance Entertainment allowance allowance 1 1000 500 2 750 200 3 500 100 4 250 - An executives gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks. Income tax is withheld

13、 from the salary on a percentage basis as follows: Gross salary Tax rate Gross5000 8% Write a program that will read an executives job number, level number, and basic pay and then computer the net salary after withholding income tax. The problem is detailed in the program.,for语句一般应用形式:,例 用for循环求1+2+

14、100,#include main() int i,sum=0; for(i=1;i=100;i+) sum+=i; printf(%d,sum); ,for(expr1 ; expr2 ; expr3) 循环体语句;,引入逗号运算符 p154,#include #include #include main() int i,sum; srand( (unsigned)time(NULL) ); for(sum=0,i=1;i=100;i+) sum+=rand(); printf(%dnn,sum); ,各人各次 运行结果不一,形式:表达式1,表达式2,表达式n,结合性:从左向右 优先级: 1

15、5 逗号表达式的值:等于表达式n的值,例 用for循环生成10个随机数并累加,#include #include #include main() int i, tmp, sum=0; /* Seed the random-number generator with current time so that the numbers will be different every time we run. */ srand( (unsigned)time(NULL) ); for(i=1;i=10;i+) tmp=rand(); sum+=tmp; printf(%dnn,sum); ,例p161

16、 求1/(1-x)=1+x+x2+x3+xn, x1, 算到通项小于0.0001,#include stdio.h int main() float sum=1,x,term; printf(Input the value of x:); scanf(%f, 这个例子若x的值超过1,则出现列循环。,4.5 辅助控制语句 p159165 break语句( p159)用在循环体中,起终止并跳出循环体的作用,例 break举例:输出圆面积,面积大于100时停止,#define PI 3.14159 main() int r; float area; for(r=1;r100) break; prin

17、tf(r=%d,area=%.2fn,r,area); ,#include stdio.h /*compute 1/(1-x) */ int main() float sum,x,term; int n; printf(Input the value of x:); scanf(%f, ,break用到前面的例子(为解决列循环)却不方便: 求1/(1-x)=1+x+x2+x3+xn, x1, 算到通项小于0.0001,continue语句 p163 功能:结束本次循环,跳过循环体中一部分尚未执行的语句,进行下一次是否执行循环体的判断 仅用于循环语句中,例(continue) 求输入的十个整数中

18、正数的个数及其平均值,/*using continue statement*/ #include main() int i,num=0,a; float sum=0; for(i=0;i10;i+) scanf(%d, ,Case Studies,先介绍下一章:算法的表示,分子:1,-1,1,-1 分母:1,3,5,7,.,例 求Fibonacci数列:1,1,2,3,5,8,的前40个数,/*Be prime or not*/ #include #include main() int m,i,k; scanf(%d, ,例 判断m是否素数,例 梯形法求数值积分,循环的嵌套 p156 三种循环

19、可互相嵌套,层数不限 外层循环可包含两个以上内循环,但不能相互交叉 嵌套循环的执行流程,(1) for(;) while() . ,(2) do do while( ); . while( );,(3) while() do while( ); . ,(4) for( ; ;) for(;) while() . ,内循环,内循环,嵌套循环的跳转 禁止: 从外层跳入内层 跳入同层的另一循环 向上跳转,例 循环嵌套,输出九九表 p151,/*Multiplication Table*/ #include main() int i,j; for(i=1;i10;i+) printf(%4d,i);

20、printf(n-n); for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?%4dn:%4d,i*j); ,for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?%4dn:%4d,i*j);,下面这个例子既是循环语句的应用,也体现计算机逻辑运算与算术运算的统一 例 有红、黄、兰、白、紫色的珠子分别放在个包里。 甲说:第二个包是紫色珠子,第四个包是黄色珠子. 乙说:第一个包是红色珠子,第五个包是白色珠子. 丙说:第三个包是白色珠子,第四个包是兰色珠子. 丁说:第二个包是黄色珠子,第五个包是紫色珠子. 每个人都说对一句,说错一

21、句.各个包里的珠子分别是什么颜色的?编制求解程序.,#include stdio.h int main() int x,y,z,u,v; for(x=1;x%d|bag2-%d|bag3-%d|bag4-%d|bag5-%dn,x,y,z,u,v); return 0; ,例pp168 Bonomial Coeffcients are used in the study of bonomial distributions. It is given by B(m,x)=m!/(x!(m-x)!), m=x A table of bonomial Coeffcients is required t

22、o be printed as follows: Mx 0 1 2 3 4 5 6 7 8 9 10 - 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 7 1 7 21 35 35 21 7 1 8 1 8 28 56 70 56 28 8 1 9 1 9 36 84 126 126 84 36 9 1 10 1 10 45 120 210 252 210 120 45 10 1 Problem Analysis: B(m,0) = 1; B(m,x) = B(m,x-1)(m-x+1)/x

23、 , x = 1,2,3,.,m B(0,0) =1;,例pp169 In an organization , the employees are grouped according to their basic pay for purpose of certain perks. The pay-range and the number of employees in each group are as follows: GROUP PAY-RANGE NUMBER OF EMPLOYEES 1 750-1500 12 2 1501-3000 23 3 3001-4500 35 4 4501-

24、6000 20 5 above6000 11 Draw a histogram to highlight the group sizes.,例pp171 Minimum Cost: The cost of operation of a unit consists of two components C1 and C2 which can be expressed as functions of a parameter p as follows: C1=30-8p C2=10+p2 The parameter p ranges from 0 to 10. Determine the value

25、of p with an accuracy of +0.1 where the cost of operation would be minimum . Problem Analysis: Total cost =C1+C2=40-8p+p2 The cost is 40 when p =0,and 33 when p = 1 and 60 when p = 10. The cost , therefore , decreases first and then increases.,例pp172 Plotting of Two Functions: y1 = exp(-ax) y2 = exp

26、(-ax2/2) x varying from 0 to 5.0 Program Analysis: Initially when x = 0, y1 = y2= 1 and the graphs start from the same point. The curves cross when they are again equal at x = 2.0. The program should have appropriate branch statements to print the graph points at the following three condition: 1.y1 y2 2.y1 y2 3.y1 = y2 The functions y1 and y2 are normalized

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论