C语言英文课件4:选择结构
(1)sequence (4)until loop
2020/5/12
(2)selective
(3)while loop
S block until P
[example 4.1] algorithm description——N-S flow chart
first level algorithm
read in weight
A figure describs steps of an algorithm with kinds of prescriptive graphic symbols and lines wchart:
start
I/O
operation
end
analysis: not overweight 0.35*weight cost overweight 0.35*50+0.5*(weight-50)
2020/5/12
1、variables:weight,pay,are all real.
2、program list:
main ( )
{ float weight,pay;
printf(weight= ); scanf(%f , &weight);
if (weight<=50) pay=weight*0.35 ;
else pay=50*0.35+(weight-50)*0.5;
printf( pay=%-7.2f , pay);
}
2020/5/12
basic steps of solving a problem: 0.analysis 1.setting variables 2.design the algorithm 3.writing the program list
comput pay based on weight’s value
Output pay
second level detailed algorithm
T
weight ≤50
F
pay=weight*0.35 pay=50*0.35+(weight-50)*0.5
2020/5/12
[ex. 4.1] Algorithm description—programming language
• describing algorithm:natural language,flow chart,pslanguage
[example 4.1]algorithm description——natural language 1.input weight put value of pay based on weight 3.output value of pay
start
inputweight
true
false
weight≤50
pay=weight*0.35
pay=50*0.35+(weight-50)*0.5
2020/5/12
outputpay
end
2. N-S structural flow chart
Main feature:cancel flow line,adapt to structural programming,
Chapter 4 Selective Structure Programming
4.0 Foreword 4.1 Algorithm and Description 4.2 Conditional Operations and Condiyional Expression 4.3 Logical Operations and Logical Expression 4.4 if Statement 4.5 switch Statement 4.6 Examples of Selective Structure Programming
Can show the logical execute process of algorithm better
basic shape of N-S flow chart:
S1 block
S2 block is true
condition P
T
F
S1 block S2 block
while P is true S block
Start frame
End frame
I/O frame
operation frame
true
false
condition
distinguishing frame
2020/5/12
connect point
flow line
【example 4.1】algorithm description—traditional flow chart
details of step 2 : if weight<=50 则 pay=weight*0.35
else 2020/5/12 pay=50*0.35+(weight-50)*0.5
4.1.1 Program Flow Chart 1.traditional flow chart(程序流程图)
2020/5/12
4.0 Foreword
【 ex. 4.1】Compute the cost of baggage carried by train.Input the weight of the baggage, compute the cost. standard of cost: (1) lighter then 50 kg(include 50 kg), 0.35 yuan/kg (2) weighter then 50 kg,50 kg like (1),the other excess, 0.50yuan/kg
4.1 Algorithm and Description
•Algroithm:Some restricted steps are taked inorder to solve a special problem.
•features :1. Limited(有穷性) 2. facticity(确定性)3. ≥0 input 4. >0 output 5. validity(有效性)