Program design tools · 程序设计工具
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| structure chart/ˈstrʌktʃə tʃɑːt/ | 结构图 | jié gòu tú |
| state-transition diagram/steɪt trænˈsɪʃn ˈdaɪəɡræm/ | 状态转换图 | zhuàng tài zhuǎn huàn tú |
| pseudocode/ˈsuːdəʊkəʊd/ | 伪代码 | wěi dài mǎ |
| decomposition/ˌdiːkɒmpəˈzɪʃn/ | 分解 | fēn jiě |
| subroutines/ˈsʌbruːtiːnz/ | 子程序 | zi chéng xù |
| parameters/pəˈræmɪtəz/ | 参数 | cān shù |
| top-down design/tɒp daʊn dɪˈzaɪn/ | 自顶向下设计 | zì dǐng xiàng xià shè jì |
| states/steɪts/ | 状态 | zhuàng tài |
The year software became engineering
- In October 1968, fifty of the world's leading programmers met in Garmisch, Germany, to discuss why large programs were failing: late, over budget, unreliable. They coined a phrase for what was missing: software engineering.
- The complaint was simple. Builders draw before they build. Engineers calculate before they cut. Programmers were typing code before anyone had drawn what the program was.
- The drawings that came out of that decade are the ones you will use in the design stage: the structure chart 结构图, which shows how a program is broken into parts, and the state-transition diagram 状态转换图, which shows how it behaves.
- This lesson is how to read them, how to construct them, and how to turn a structure chart into pseudocode.
软件成为工程的那一年
- 1968 年 10 月,世界上五十位顶尖程序员在德国加米施开会,讨论为什么大型程序总在失败:延期、超支、不可靠。他们为缺失的东西造了一个词:软件工程。
- 抱怨很简单。建筑工人先画图再施工。工程师先计算再下刀。程序员却在任何人画出程序是什么之前就开始敲代码。
- 那十年产生的图,正是你将在设计阶段使用的:显示程序怎样被分成部分的结构图(structure chart),和显示它怎样行为的状态转换图(state-transition diagram)。
- 这一课讲怎样读它们、怎样构造它们,以及怎样把结构图变成伪代码。
What design decides
- Analysis said what the program must do. Design decides how: the data structures, the algorithms, the modules and the interface.
- The design stage produces drawings a programmer can code from: a flowchart for the logic of one algorithm, pseudocode 伪代码 for the same thing in text, a structure chart for the modules, and a state-transition diagram for the behaviour.
- Each tool answers a different question, and the exam asks which one fits.
One algorithm's logic, drawn before it is coded
设计决定什么
- 分析说了程序必须做什么。设计决定怎样做:数据结构、算法、模块和界面。
- 设计阶段产出程序员可以据此编码的图:表示单个算法逻辑的流程图,用文字表达同一内容的伪代码(pseudocode),表示模块的结构图,以及表示行为的状态转换图。
- 每种工具回答不同的问题,考试会问哪一种合适。
一个算法的逻辑,在编码前画出
Software process lab · 软件过程实验
Classify development examples by the stage or tool they belong to. · 根据阶段或所属工具对开发示例进行分类。
The structure chart
- A structure chart shows the hierarchical decomposition 分解 of a program into modules, its subroutines 子程序, and the parameters 参数 passed between them. This top-down design 自顶向下设计 breaks one large problem into smaller sub-problems, each of which becomes a module.
- Each module is a rectangle. A line joins a caller, above, to the module it calls, below. Modules on the same level are called from left to right.
- Small arrows beside the lines carry data: a parameter passed down into the module, a result returned up to the caller. A diamond marks a selection, a curved arrow a loop.
Hierarchy on the lines, data on the arrows
结构图
- 结构图显示程序向模块——它的子程序(subroutines)——的层次分解(decomposition),以及它们之间传递的参数(parameters)。这种自顶向下设计(top-down design)把一个大问题分成更小的子问题,每个子问题成为一个模块。
- 每个模块是一个矩形。一条线连接上方的调用者和下方被它调用的模块。同一层的模块从左到右依次被调用。
- 线旁的小箭头携带数据:向下传入模块的参数,向上返回给调用者的结果。菱形标记选择,弯曲的箭头标记循环。

线上是层次,箭头上是数据
A structure chart shows: · 结构图显示:
A structure chart is the hierarchical breakdown into modules, with parameters down and results up. · 结构图是模块的层次分解,参数向下,结果向上。
Breaking a problem into modules from the top down is called: · 从顶层向下将问题分解为模块称为:
Top-down design produces a modular solution. · 自顶向下设计产生模块化解决方案。
Worked example: read the signatures off the chart
GetEmployeereceives nothing and returns an employee ID:FUNCTION GetEmployee() RETURNS INTEGER.CalculateBonustakes the sales figure down and sends the bonus back:FUNCTION CalculateBonus(Sales : REAL) RETURNS REAL.CalculateTaxtakes the gross pay and returns the tax. Every arrow on the chart is a parameter or a return value in the header; a header with a parameter the chart does not show is wrong.
例题:从图上读出签名
CalculatePay
/ | \
GetEmployee CalculateBonus CalculateTax
returns: takes: sales takes: gross
employeeID returns: bonus returns: tax
GetEmployee不接收任何东西,返回员工 ID:FUNCTION GetEmployee() RETURNS INTEGER。CalculateBonus接收向下传的销售额,把奖金送回去:FUNCTION CalculateBonus(Sales : REAL) RETURNS REAL。CalculateTax接收总工资并返回税额。图上每个箭头都是头部里的一个参数或返回值;头部里有图上没有的参数就是错的。
On a structure chart, a small arrow pointing down from the caller into a module shows a ____ passed to it. · 在结构图上,从调用者指向模块的小向下箭头表示传入的 ____。
Downward arrows are parameters going in; upward arrows are results returned. Together they give the module's header. · 向下箭头是输入的参数;向上箭头是返回的结果。它们共同构成模块的头信息。
Worked example: construct a structure chart
- A program reads a student's marks, calculates the average, and outputs a grade. Draw a structure chart.
- Top module:
ProcessStudent. Below it, left to right:ReadMarks, which returns the array of marks;CalculateAverage, which takes the marks array down and returns the average;OutputGrade, which takes the average down and returns nothing. - Three things score: the hierarchy with the main task at the top, the sub-tasks in the order they run, and named parameters on the arrows in the right direction. An unlabelled arrow is half a mark at best.
例题:构造一张结构图
- 一个程序读入学生的分数、计算平均分并输出等级。画出结构图。
- 顶层模块:
ProcessStudent。其下从左到右:ReadMarks,返回分数数组;CalculateAverage,接收向下传的分数数组并返回平均分;OutputGrade,接收向下传的平均分,不返回任何东西。 - 三样东西得分:主任务在顶端的层次,按运行顺序排列的子任务,以及箭头上命名的、方向正确的参数。没有标签的箭头最多半分。
From structure chart to pseudocode
- The top module becomes the main program; each rectangle below it becomes a procedure or function whose header is read off the arrows; the main program calls them in left-to-right order.
- A returned value means a
FUNCTION … RETURNS; a module that returns nothing is aPROCEDURE. The parameter list is exactly the downward arrows.
从结构图到伪代码
- 顶层模块成为主程序;下面每个矩形成为一个过程或函数,头部从箭头读出;主程序按从左到右的顺序调用它们。
PROCEDURE ProcessStudent()
DECLARE Marks : ARRAY[1:10] OF INTEGER
DECLARE Average : REAL
Marks ← ReadMarks()
Average ← CalculateAverage(Marks)
CALL OutputGrade(Average)
ENDPROCEDURE
- 有返回值就是
FUNCTION … RETURNS;不返回任何东西的模块是PROCEDURE。参数列表恰好就是向下的箭头。
Put the steps of designing a program with a structure chart in order. · 按顺序排列使用结构图设计程序的步骤。
Top down: task, sub-tasks, data flow, headers, calls. The chart is finished before the first line of code. · 自顶向下:任务、子任务、数据流、头文件、调用。在第一行代码编写之前,图表就已完成。
A module whose structure-chart arrows show a value returned upwards should be written as a PROCEDURE. · 如果一个模块的结构图箭头显示返回值向上返回,则该模块应编写为 PROCEDURE(过程)。
A returned value makes it a FUNCTION … RETURNS. A PROCEDURE returns nothing. · 返回一个值使其成为 FUNCTION(函数)……RETURNS。PROCEDURE(过程)不返回值。
The state-transition diagram
- A state-transition diagram documents the behaviour of a system: the states 状态 it can be in and the events that move it from one state to another.
- Each state is a circle or rounded box; each transition is an arrow labelled with the event that causes it, sometimes with the action taken. A marker shows the start state.
- It suits systems that wait for events and react: a vending machine, a traffic light, a door lock, a user interface.
Every state, every event, every arrow
状态转换图
- 状态转换图记录一个系统的行为:它可能处于的状态(states),以及让它从一个状态进入另一个状态的事件。
- 每个状态是一个圆或圆角框;每个转换是一个标注了引发它的事件的箭头,有时还标注所采取的动作。一个标记表示起始状态。
- 它适合等待事件并作出反应的系统:自动售货机、交通灯、门锁、用户界面。

每个状态、每个事件、每个箭头
A state-transition diagram shows: · 状态转换图显示:
States are circles; transitions are event-labelled arrows. Ideal for vending machines, locks, traffic lights. · 状态是圆圈;转换是带有事件标签的箭头。适用于自动售货机、锁和交通灯。
What does a state-transition diagram show? Select all · 所有 that apply. · 状态转换图显示了什么?选择所有适用的选项。
States, labelled transitions and the start marker. Timing is not part of the diagram. · 状态、带标签的转换和起始标记。时序不是该图的一部分。
Worked example: read the door-lock diagram
- The lock opens on the code 2, 5, 9. Start in Locked. Pressing 2 moves to One digit correct; pressing 5 from there moves to Two digits correct; pressing 9 from there moves to Unlocked.
- Any other key from any of the waiting states returns to Locked: the diagram shows those arrows too, and a diagram that leaves them out has a gap. What happens if 2 is pressed while Unlocked? If no arrow says, the design has not decided.
- That is what the diagram is for: every state must say what happens on every event, so the missing transitions are found on paper and not by a customer.
例题:读门锁的图
- 门锁在密码 2、5、9 下打开。从已锁开始。按 2 进入一位正确;从那里按 5 进入两位正确;从那里按 9 进入已解锁。
- 从任何等待状态按其他任何键都回到已锁:图上也画出了这些箭头,漏掉它们的图就有缺口。在已解锁时按 2 会怎样?如果没有箭头说明,设计就还没有决定。
- 这正是这种图的用途:每个状态都必须说明每个事件下发生什么,这样缺失的转换在纸上就被发现,而不是被客户发现。
A state-transition diagram makes missing or unhandled transitions easy to spot, because every state and the events between them are laid out. · 状态转换图使缺失或未处理的事件易于发现,因为每个状态及其之间的事件都已列出。
Seeing every state and event reveals cases you have not handled — e.g. an unexpected second coin in a vending machine. · 看到所有状态和事件会揭示你未处理的情况——例如自动售货机中的意外第二枚硬币。
Choosing the tool
- To show how a program is broken into modules and what passes between them: a structure chart.
- To show how a system behaves over time in response to events, especially a machine or an interface with modes: a state-transition diagram.
- To show the step-by-step logic of one algorithm: a flowchart or pseudocode. Say which and why.
选择工具
- 要显示程序怎样分成模块以及它们之间传递什么:结构图。
- 要显示系统随时间对事件的行为,尤其是有模式的机器或界面:状态转换图。
- 要显示单个算法的逐步逻辑:流程图或伪代码。说出哪一种以及为什么。
Match each design tool to what it shows. · 将每个设计工具与其显示的内容匹配。
Each tool views the design differently — structure (modules), behaviour (states), flow (flowchart) or steps (pseudocode). · 每种工具从不同角度查看设计——结构(模块)、行为(状态)、流(流程图)或步骤(伪代码)。
A traffic-light controller must respond to a timer and a pedestrian button. Which design tool documents its behaviour best? · 交通灯控制器必须响应计时器和行人按钮。哪个设计工具最能记录其行为?
Red, red-and-amber, green, amber are states; the timer and the button are events. A structure chart would show the modules, not the behaviour. · 红色、红黄、绿色、黄色是状态;计时器和按钮是事件。结构图将显示模块,而非行为。
Marks that slip away
- A structure chart is not a flowchart. It shows hierarchy and parameters, not the sequence of decisions inside a module.
- Label every arrow with the name of the parameter or result and point it the right way. A bare arrow says nothing.
- A state is a condition the system is in, waiting; an event is what happens to it. "Press 5" is an event, not a state.
- The pseudocode headers must match the chart: same parameters, same return values, same order of calls.
容易丢掉的分
- 结构图不是流程图。它显示层次和参数,不显示模块内部决策的顺序。
- 给每个箭头标上参数或结果的名字,并指向正确的方向。光秃秃的箭头什么也没说。
- 状态是系统所处的、等待中的状况;事件是发生在它身上的事。"按 5"是事件,不是状态。
- 伪代码头部必须与图一致:同样的参数、同样的返回值、同样的调用顺序。
You've got it
- a structure chart shows top-down decomposition into modules, with the parameters passed down and the results returned up on labelled arrows
- read the pseudocode headers off it: downward arrows are the parameter list, an upward arrow makes it a
FUNCTION … RETURNS, and the main module calls left to right - a state-transition diagram shows the states and the events that move between them, and exposes the transitions nobody has decided
- decomposition → structure chart; behaviour → state-transition diagram; one algorithm's logic → flowchart or pseudocode
你掌握了
- 结构图显示自顶向下向模块的分解,带标签的箭头上有向下传递的参数和向上返回的结果
- 从图上读出伪代码头部:向下的箭头是参数列表,向上的箭头使它成为
FUNCTION … RETURNS,主模块从左到右调用 - 状态转换图显示状态和在它们之间移动的事件,并暴露出没人决定过的转换
- 分解 → 结构图;行为 → 状态转换图;单个算法的逻辑 → 流程图或伪代码