版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第4章 程序的控制结构,C语言大学实用教程,哈尔滨工业大学 计算机科学与技术学院 苏小红,本章学习内容,算法的描述方法 常用算法(累加累乘、统计、递推迭代、穷举) 选择结构及相关控制语句 循环结构及相关控制语句 结构化程序设计的基本思想 Skill: Map problem to solution in flowchart and pseudocode forms Be able to develop a program containing selection and loop control structure,Consider the following . Problem: 烤蛋糕(B
2、aking a Cake) How to solve: Start 将烤箱预热 准备一个盘子 在盘子上抹上一些黄油 将面粉、鸡蛋、糖和香精混合在一起搅拌均匀 将搅拌好的面粉团放在盘子上 将盘子放到烤箱内 End,实际生活中的算法Algorithm in Real Life,Divide and Conquer Strategy (分治策略)in Algorithm,Problem: 准备早餐( Prepare a Breakfast),1.Start 2.准备早餐 3.End,1. Start 2. 准备早餐 2.1 准备一个金枪鱼三明治 2.2 准备一些薯条 2.3 冲一杯咖啡 3. End
3、,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.3 冲一杯咖啡 3. End,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡 3. End,Divi
4、de and Conquer Strategy (分治策略)in Algorithm,Divide and Conquer Strategy (分治策略)in Algorithm,1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡 2.3.1 烧些开水放入杯中 2.3.2 在水杯中加入一些咖啡和糖 3. End,What is the connection between these real life processes and a
5、lgorithm?,Something to ponder ,算法( Algorithm )的概念,数据结构 + 算法 = 程序 只对面向过程的语言(C)成立 面向对象程序 = 对象 + 消息 算法: 为解决一个具体问题而采取的确定的有限的操作步骤,仅指计算机能执行的算法 A specific and step-by-step set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at som
6、e point,算法的特性,有穷性 在合理的时间内完成 确定性,无歧义 如果x0,则输出Yes 如果x0,则输出No 有效性 能有效执行 负数开平方 没有输入或有多个输入 有一个或多个输出,算法的表示方法,自然语言描述 传统流程图(Flowchart) 在1966年,Bohra 与 Jacopini 提出 N-S结构化流程图 1973年,美国学者I.Nassi 和 B.Shneiderman 提出 伪码(Pseudocode)表示,流程图(Flowchart),Flowchart represents algorithm graphically.,Are the steps in the al
7、gorithm discussed earlier specific enough to be executed by computer?,Something to ponder ,问题求解步骤(Problem Solving Process),Input,Process,Output,First identify the input and output of the problem.,Example 1:买苹果,计算价钱,Calculate and display the price of a number of apples if the quantity in kg and price
8、 per kg are given.,quantity pricePerkg,price,price = quantity * pricePerkg,Input,Process,Output,流程图(Flowchart): Calculate Price of Apples,Input quantity,Start,price quantity * pricePperkg,Input pricePerkg,Output price,End,If necessary, use Divide scanf(%d, ,C Program: Calculate Price of Apples,main(
9、) int quantity,price_per_kg,price; scanf(%d, ,C Program: Calculate Price of Apples,An outline of a program, written in a form that can easily be converted into real programming statements. It resembles the actual program that will be implemented later. However, it cannot be compiled nor executed.,伪码
10、(Pseudocode),1. Start 2. Read quantity 3. Read price_per_kg 4. price quantity * price_per_kg 5. Print price 6. End,Pseudocode normally codes the following actions: Initialisation of variables Assignment of values to the variables Arithmetic operations Relational operations,顺序( Sequence )结构的NS图,给变量赋值
11、 赋值表达式语句 赋值表达式 ; price = quantity*pricePerkg; 输入输出数据 标准库函数调用语句 scanf(%d, ,NS图,B,A,Example 2: Calculate the Minimum,计算两个数中的最小者.,num1 num2,min,?,Input,Process,Output,B,N,A,Y,条 件P,选择结构(分支结构) (Selection Structure),NS图,传统流程图,Flowchart: Calculate the Minimum,Input num1 Input num2,Output min,num1num2?,min
12、num2,min num1,Start,End,scanf(%d%d,if (num1 num2) min = num1; else min = num2; printf(%d, min);,C Program: Calculate the Minimum,main() int num1, num2, min; scanf(%d%d, ,C Program: Calculate the Minimum,选择结构(分支结构) (Selection Structure),单分支选择结构(Single Selection),step a,condition,step m,step n,step b,
13、true,false,Pseudocode Structure step a if start step m step n end_if step b,if Statement,The structure is similar to single selection (flowchart),Syntax: if (expression) statement; or if (expression) statement1; statement2; ,if Statement,The structure is similar to single selection (flowchart),Synta
14、x: if (expression) statement; or if (expression) statement1; statement2; ,if Statement,The similarity between single selection structure and if statement:,Single Selection Pseudocode : if start step 1 step 2 step k end_if,if Statement: if (expression) statement 1 statement 2 statement k ,if Statemen
15、t,main() int num1, num2, min; printf(“Input 2 numbers: “); scanf(“%d%d”, ,Input 2 numbers: _,20,15,Input 2 numbers: 20 15 Smallest: 15 _,if Statement,Example: main() int mark; scanf(“%d”, ,What will the output be if the mark is 65?,if Statement,Example: main() int mark; scanf(“%d”, ,What will the ou
16、tput be if the mark is 35?,双分支选择结构( Double Selection),Pseudocode Structure Step a if start Step m Step n end_if else start Step x Step y end_else Step z,Step a,condition,Step m,Step n,Step b,true,false,Step x,Step y,if - else Statement,The structure is similar to double selection (flowchart),Syntax:
17、 if (expression) statement; else statement; or if (expression) statement1; statement2; else statement3;,or if (expression) statement1; statement2; else statement3; statement4; ,if - else Statement,The similarity between double selection structure and if - else statement:,Double Selection Pseudocode:
18、 if start step 1 step k end_if else start step 1 step n end_else,if Statement: if (expression) statement 1 statement k else statement 1 statement n ,if - else Statement,Example: if (num1 num2) min = num1; else min = num2; printf(“Smallest: %dn”, min);,_,10,Smallest: 10 _,if - else Statement,Example:
19、 if (num1 num2) min = num1; else min = num2; printf(“Smallest: %dn”, min);,_,15,Smallest: 15 _,if - else Statement,if (num1 num2) min = num1; max = num2; else min = num2; max = num1; printf(“Min = %d, Max = %dn”, min, max);,_,Min = 125, Max = 700 _,125,700,条件运算符,计算最小值,if (num1 num2) min = num1; else
20、 min = num2; printf(“Smallest: %dn”, min);,min = num1 num2 ? num1 : num2; printf(“Smallest: %dn”, min);,Lets recap ,Single Selection Statement step a if (expression) step m step n step b,Single Selection,Lets recap ,Double Selection Statement step a if (expression) step m step n else step x step y s
21、tep b,Double Selection,Guesshow does multiple selection look like ?,多分支选择结构(Multiple Selection),Multi-way if Step a if (expression1) Step m if (expression2) Step n Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,多分支选择结构(Multiple Selection),Cascaded if Step a if (expr
22、ession1) Step m else if (expression2) Step n else Step x Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,Step x,例4.5 :体型判断,按“体指数”对肥胖程度进行划分: 体指数t = w / h2 (体重w单位为公斤,身高h单位为米) 当t 18时,为低体重; 当18 t 25时,为正常体重; 当25 t 27时,为超重体重; 当t 27时,为肥胖。 编程从键盘输入你的身高h和体重w,判断你的体重属于何种类型。,例4.5
23、,#include main() float h, w, t; printf(Please enter h,w:); scanf(%f, %f, ,当t 18时,为低体重; 当18 t 25时,为正常体重; 当25 t 27时,为超重体重; 当t 27时,为肥胖。,#include main() float h, w, t; printf(Please enter h,w:); scanf(%f, %f, ,18 25 27,例4.5,当t 18时,为低体重; 当18 t 25时,为正常体重; 当25 t 27时,为超重体重; 当t 27时,为肥胖。,多路选择(switch) and brea
24、k,The structure is similar to multiple selection (flowchart),switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX; break; ,Important Rule !,switch (expression) case value1 : statement1; break; case value2 : statement2; break; default : statementX
25、; break; ,多路选择(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,January _,January End _,多路选择(switch) and break,Example: switch (month) case 1: printf(“Ja
26、nuaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,March _,March End _,多路选择(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); break; case 3: printf(“Marchn”);
27、break; default: printf(“Othersn”); break; printf(“End”);,多路选择(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,多路选择(switch) and break,Example: switch (month) ca
28、se 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: printf(“Othersn”); break; printf(“End”);,February _,March _,End _,多路选择(switch) and break,Example: switch (month) case 1: printf(“Januaryn”); break; case 2: printf(“Februaryn”); case 3: printf(“Marchn”); break; default: p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年长安医院医护人员招聘笔试参考试题及答案详解
- 2025年温州市第三人民医院医护人员招聘考试题库附答案详解
- 2026年徐州医学院附属第三医院医护人员招聘笔试参考试题及答案详解
- (2026年)预防校园性侵害工作制度
- 2026年武警部队广东省总队医院医护人员招聘笔试备考试题及答案详解
- 2026年湖南省中医学院附属一医院医护人员招聘考试参考题库及答案详解
- 2026年四川成都市中考地理真题卷(含答案与解析)
- 2026年宜兴市人民医院医护人员招聘考试备考试题及答案详解
- 2026年中国工商银行(海南分行)人员招聘考试备考试题及答案详解
- 2026年四川省肿瘤医院医护人员招聘笔试备考试题及答案详解
- PSA测定在前列腺癌早期诊断中的应用()
- 2025陕西农业发展集团有限公司招聘(200人)笔试参考题库附带答案详解
- 大一下期末物理试卷及答案
- 2024-2025学年北京市东城区五下数学期末检测试题含答案
- 浙江省宁波市九校联考2023-2024学年高二下学期6月期末联考考试+物理试卷
- 遗传学(中国农业大学)智慧树知到答案2024年中国农业大学
- YD-T 1484.1-2023 无线终端空间射频辐射功率和接收机性能测量方法 第1部分:要求
- 2024年中国热带农业科学院热带生物技术研究所招考聘用(高频重点提升专题训练)共500题附带答案详解
- JJG 643-2024标准表法流量标准装置
- 患者心理支持与护理沟通
- 2023【】二次供水水箱清洗合同正规范本(通用版)
评论
0/150
提交评论