《c语言大学教程》ppt课件

上传人:自*** 文档编号:80907649 上传时间:2019-02-20 格式:PPT 页数:69 大小:834.50KB
返回 下载 相关 举报
《c语言大学教程》ppt课件_第1页
第1页 / 共69页
《c语言大学教程》ppt课件_第2页
第2页 / 共69页
《c语言大学教程》ppt课件_第3页
第3页 / 共69页
《c语言大学教程》ppt课件_第4页
第4页 / 共69页
《c语言大学教程》ppt课件_第5页
第5页 / 共69页
点击查看更多>>
资源描述

《《c语言大学教程》ppt课件》由会员分享,可在线阅读,更多相关《《c语言大学教程》ppt课件(69页珍藏版)》请在金锄头文库上搜索。

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:

2、烤蛋糕(Baking 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、咖啡 3. End,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.

4、 End,Divide 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 proces

5、ses and algorithm?,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 termina

6、te at some point,算法的特性,有穷性 在合理的时间内完成 确定性,无歧义 如果x0,则输出Yes 如果x0,则输出No 有效性 能有效执行 负数开平方 没有输入或有多个输入 有一个或多个输出,算法的表示方法,自然语言描述 传统流程图(Flowchart) 在1966年,Bohra 与 Jacopini 提出 N-S结构化流程图 1973年,美国学者I.Nassi 和 B.Shneiderman 提出 伪码(Pseudocode)表示,流程图(Flowchart),Flowchart represents algorithm graphically.,问题求解步骤 (Proble

7、m 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 per kg are given.,quantity pricePerkg,price,price = quantity * pricePerkg,Input,Process,Output,流程图(Flow

8、chart): Calculate Price of Apples,Input quantity,Start,price quantity * pricePperkg,Input pricePerkg,Output price,End,If necessary, use Divide & Conquer strategy to decompose the problem into smaller and manageable sub problems.,main() scanf(“%d“, ,C Program: Calculate Price of Apples,main() int qua

9、ntity,price_per_kg,price; scanf(“%d“, ,C Program: Calculate Price of Apples,main() 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 prog

10、ram that will be implemented later. However, it cannot be compiled nor executed.,伪码(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 t

11、he variables Arithmetic operations Relational operations,顺序( Sequence )结构的NS图,给变量赋值 赋值表达式语句 赋值表达式 ; 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图,传

12、统流程图,Flowchart: Calculate the Minimum,Input num1 Input num2,Output min,num1num2?,min 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,选择结构(分

13、支结构) (Selection Structure),单分支选择结构 (Single Selection),step a,condition,step m,step n,step b,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) statemen

14、t1; statement2; ,if Statement,The structure is similar to single selection (flowchart),Syntax: 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

15、step k end_if,if Statement: if (expression) statement 1 statement 2 statement k ,if Statement,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 outpu

16、t be if the mark is 65?,if Statement,Example: main() int mark; scanf(“%d”, ,What will the output 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 z,true,false,Step x,Step y,if - else Statement,The structure is similar to double selection (flowch

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号