最新c语言-选择控制结构教学讲义ppt

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

分治策略 ("Divide and Conquer" Strategy )
Problem: 准备早餐( Prepare a Breakfast)
1. Start 2. 准备早餐 3. End
分治策略 ( "Divide and Conquer" Strategy )
1. Start
2.准备早餐
or if (expression) { statement1; statement2; }
复合语句 compound statement 被当作一条语句看待
if Statement
Syntax: if (expression) statement;
or if (expression) { statement1; statement2; }

给变量赋值
◦ 赋值表达式语句
赋值表达式 ;
A
price = quantity*pricePerkg;
输入输出数据
B
◦ 标准库函数调用语句
scanf("%d", &pricePerkg);
printf("%d", price);
C
计算两整数的最大值
Input
• num1 • num2
Process
算法的描述方法
自然语言描述 传统流程图(Flowchart)
在1966年,Bohra 与 Jacopini 提出
N-S结构化流程图
1973年,美国学者I.Nassi 和 B.Shneiderman 提出
伪码(Pseudocode)表示
流程图(Flowchart)
Flowchart represents algorithm graphically.
2.1 准备一个金枪鱼三明治
2.1.1 拿来两片面包
2.1.2 准备一些金枪鱼酱
2.2 准备一些薯片
2.2.1 将土豆切成片
2.2.2 油炸这些土豆片
2.3 冲一杯咖啡
3. End
2.3.1 烧些开水放入杯中 2.3.2 在水杯中加入一些咖啡和糖
算法的概念及其描述方法
面向过程的程序 = 数据结构 + 算法
Input a,b: 20 15
max = 20 _
单分支(2):求绝对值
输入1个整数,输出它的绝对值 (实现fabs函数) 当number < 0时,number = -number; 当number >= 0时,保持不变
Input
Process
Output
• quantity • pricePerkg
price = quantity * pricePerkg
price
First identify the input and output of the problem.
顺序结构( Sequence Structure
????
Output
max
选择结构(分支结构) (Selection Structure)
Single Selection
Multiple Selection
Double Selection
If
if - else - if
if - else
关系运算符与关系表达式
Relational Operation
Sstep a
true
ccoonnddiittiioonn
false
Sstep m Sstep n
Sstteepp bx
if Statement
The structure is similar to single selection
(flowchart)
表达式非0为真
Syntax: if (expression) statement;
Symbo l
Semantic Start/End
Process
Input/Output
Test Connector Flow of activities
计算机中的问题求解过程
Example :买苹果,计算价钱 Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.
Description
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
!=
ห้องสมุดไป่ตู้
Not equal to
Examples of Expression
6<9 5 <= 5 2>6 9 >= 5 7 == 5 6 != 5
计算机中的算法( Algorithm )
◦ 为解决一个具体问题而采取的、确定的、有限的操作 步骤,仅指计算机能执行的算法
算法的特性
◦ 有穷性
在合理的时间内完成
◦ 确定性,无歧义
如果x≥0,则输出Yes;如果x≤0,则输出No
◦ 有效性
能有效执行 负数开平方
◦ 没有输入或有多个输入 ◦ 有一个或多个输出
c语言-选择控制结构
生活中的问题求解 (Problem Solving Process)
生活中的问题求解:
Problem: 烤蛋糕(Baking a Cake) How to solve:
1. Start 2. 将烤箱预热 3. 准备一个盘子 4. 在盘子上抹上一些黄油 5. 将面粉、鸡蛋、糖和香精混合在一起搅拌均匀 6. 将搅拌好的面粉团放在盘子上 7. 将盘子放到烤箱内 8. End
Value
1 (true) 1 (true) 0 (false) 1 (true) 0 (false) 1 (true)
用于单分支控制的条件语句 (Single Selection)
Pseudocode Structure step a if <condition is true> start
step m step n end_if step b
Don’t forget the parentheses !!
Don’t forget the braces !!
单分支(1):计算两整数的最大值
#include <stdio.h> main() {
int a, b, max; printf("Input a,b:"); scanf("%d%d", &a, &b); if (a > b) max = a; if (a <= b) max = b; printf("max = %d\n", max); }
相关文档
最新文档