万事万物都不是凭空产生的,都有一个从无到有的过程,包括结构化编程思想及其应用。
我们知道,计算机诞生后,最初的编程是“硬”编程,通过插拔连线的硬件连接方式来实现不同硬件模块的组合来完成不同的任务。随后,冯诺依曼提出“存储程序控制”概念,硬件方面,由内存和控制器实现这一概念,程序和数据加载到内存,由控制器读取程序,解释程序,控制其它部件协同工作。最初的“软”编程是用机器语言(指令集和程序都有二进制序列表示),然后是符号化的汇编语言,然后是英文词汇化、命名化的高级语言编程。
我们知道,汇编语言可以通过不同的跳转指令来实现选择和重复操作,到了高级语言,可以通过关系、逻辑表达式和goto语句来实现选择和重复操作。
各种跳转让程序的流程控制变得杂乱无序,随后,有人提出并证明了可用只有单入口和单出口的顺序、选择、循环(也称为重复或迭代)三种控制结构也可以同等实现程序的相同功能。
三种控制只涉及到由几个关键字(if、else、switch、while、for等)描述的简单的几种选择结构和循环结构。
使用三种控制结构来构建代码块、函数,由一组相关函数来完成一个较大的功能,这组相关函数放到一个文件中构成一个编译单元,一个或多个文件通过编译、链接可以构建一个应用程序,这便是基本的结构化编程思维。
0 代码块和结构体编程思维
(code block and structured programing thinking)
Some (mostly ancient) programming languages let you jump directly into the middle of a block of code using the notorious GOTO statement. This is particularly deadly and strongly discouraged in modern programming. In assembly language it is easily possible to send control directly to nearly any point in a program. Writing assembly programs that work takes careful discipline.
一些(大部分是古老的)编程语言允许您使用臭名昭著的GOTO语句直接跳转到代码块的中间。这在现代编程中尤其致命,并且受到强烈的反对。在汇编语言中,很容易将控制直接发送到程序中的几乎任何一点。编写工作正常的汇编程序需要严格的纪律。
Bugs happen when control jumps out of (or into) a block of code. The block can no longer be regarded as a module that performs a particular function, but must be examined statement by statement.
当控制跳出(或跳入)代码块时,就会发生错误。块不再被视为执行特定功能的模块,而必须逐个语句进行检查。
You would like to think of a code block as a “black box” that performs some particular operation. Just before control enters the block everything is ready for the operation, and when control leaves the block the operation has been done correctly.
您可以将代码块视为执行某些特定操作的“黑盒”。就在控制进入块之前,所有操作都已准备就绪,当控制离开块时,操作已正确完成。
This is a prime idea of structured programming. A block of code is a module that has one entry point (where execution enters the block) and one exit point (where execution leaves the block). The entry point has well defined entry conditions. The exit point has well defined exit conditions.
这是结构化编程的基本思想。代码块是具有一个入口点(执行进入块)和一个出口点(执行离开块)的模块。入口点具有定义良好的入口条件。退出点具有定义良好的退出条件。
For the block to execute correctly, execution must start at the entry point, and the entry conditions must be met. When execution leaves the block, the exit conditions are true (if the block itself is bug free).
为了正确执行块,必须从入口点开始执行,并且必须满足入口条件。当执行离开块时,退出条件为true(如果块本身没有bug)。
1 代码块是结构化的
(A code block is structured)
In flow charting terms, a box with a single entry point and single exit point is structured.
在流程图术语中,构建了一个具有单个入口点和单个出口点的框。
A block should have one set of entry conditions and one set of exit conditions. The block should be a module that can be used for building larger structures. In C, blocks are made by putting braces around a group of statements. The code inside a block often has its own internal logic, but once the block is written and debugged it should be a black box.
一个代码块应该有一组进入条件和一组退出条件。该块应该是可用于建造更大结构的模块。在C语言中,块是通过将一组语句放入大括号来构成的。块中的代码通常有自己的内部逻辑,但一旦编写和调试了块,它就应该是一个黑盒子。
Each block can be thought of as a module that is part of the solution to a problem.
每个模块都可以看作是问题解决方案的一部分。
With the above two blocks, the exit conditions of the first block match the entry conditions to the second block.
对于上述两个区块,第一个区块的出口条件与第二个区块的进入条件相匹配。
If the exit conditions of block A are the correct entry conditions for block B, then the two blocks can follow in sequence.
如果A代码块的退出条件是B代码块的正确进入条件,则两个代码块可以依次进行。
The set of two blocks can be regarded as one big block. If the entry conditions for block A are correct, then the exit conditions for block A are correct, then the entry conditions for block B are correct, then (finally) the exit conditions for block B are correct.
两个块的集合可视为一个大块。如果A代码块的进入条件正确,则A代码块的退出条件正确,代码块区的进入条件正确,然后(最后)B代码块的退出条件正确。
In terms of the big block, if the entry conditions for the big block are correct, then the exit conditions for the big block are also correct.
就大代码块而言,如果大代码块的进入条件正确,那么大区块的退出条件也正确。
The blocks should work like jigsaw puzzle pieces where the shape of one puzzle exactly matches the shape of the following piece. Once the two pieces are fit together they can be regarded as a bigger piece that fits somewhere in the puzzle.
这些代码块应该像拼图一样工作,其中一个拼图的形状与下一个拼图的形状完全匹配。一旦这两块拼合在一起,它们就可以被视为一块更大的拼图,适合拼图中的某个地方。
A sequence of blocks is correct if the exit conditions of each block matches the entry conditions of the following block. Control enters each block at the block’s entry point, and leaves through the block’s exit point. The whole sequence can be regarded as a single block, with an entry point and an exit point.
如果每个代码块的退出条件与下一个代码块的进入条件相匹配,则代码块序列是正确的。控制语句在块的入口点进入每个块,然后通过块的出口点离开。整个序列可以看作是一个单独的区块,有一个入口点和一个出口点。
2 按顺序构造两个或多个代码块
(Two or more code blocks in sequence are structured)
Ultimately, if the program is a sequence of small blocks that fit together properly, the entire program could be thought of as one big block. When the program’s entry conditions are met, then the program’s exit conditions will be met.
归根结底,如果程序是一系列适当组合在一起的小程序块,那么整个程序可以被视为一个大程序块。当程序的进入条件得到满足时,程序的退出条件也将得到满足。
With a program, the entry conditions consist of the problem the program was written to solve, and the exit conditions are the solution to the problem. If each of the blocks of the program are correct, then the entire program is correct.
对于一个程序,进入条件包括编写程序要解决的问题,而退出条件是问题的解决方案。如果程序的每个块都正确,则整个程序都正确。
3 两个代码块的选择是结构化的
(The alternation of two code blocks is structured)
If-then-else is sometimes called alternation (because there are alternative choices). In structured programming, each choice is a code block. If alternation is arranged as in the flowchart below, then there is one entry point (at the top) and one exit point (at the bottom). The structure should be coded so that if the entry conditions are satisfied, then the exit conditions are fulfilled (just like a code block).
If-then-else有时被称为选择(因为有其他选择)。在结构化编程中,每个选项都是一个代码块。如果按照下面流程图安排交替,则有一个入口点(顶部)和一个出口点(底部)。应对结构进行编码,以便在满足进入条件的情况下,满足退出条件(就像代码块一样)。
An example of an entry condition for an alternation structure is: the discriminant contains a value that is zero or positive. The exit condition might be: the correct number of roots have been computed.. The branching structure is used to fulfil the exit condition.
选择结构的输入条件的一个示例是:判别式包含零或正的值。退出条件可能是:已计算正确的根……分支结构用于满足退出条件。
In C and in most languages, alternation is implemented with an if-statement:
在C语言和大多数语言中,选择使用if语句实现:
if(condition)truebranchelsefalsebranch
With C, the condition evaluates to 0 or to non-zero. Zero is regarded as false and anything non-zero is regarded as true. Often, the branches are code blocks:
对于C,条件的计算结果为0或非零。零被视为假,任何非零的东西都被视为真。分支通常是代码块:
if(condition){trueblock}else{falseblock}
Here is a small example:
/* disc contains the discriminant of a quadratic equation */if( disc >= 0.0) { compute real roots } else{ compute imaginaryroots }
4 代码块的迭代是结构化的
(The iteration of a code block is structured)
Iteration (while-loop) also has one entry point and one exit point. The entry point has conditions that must be satisfied and the exit point has conditions that will be fulfilled. There are no jumps into or out of the structure.
迭代(while循环)也有一个入口点和一个出口点。入口点具有必须满足的条件,出口点具有将要满足的条件。没有跳进或跳出结构。
In C, iteration may be implemented with a while statement.
在C中,迭代可以用while语句实现。
while( condition ) statement
condition is an expression that evaluates to zero (regarded as false) or non-zero (regarded as true). statement is executed when condition is true, then control returns to the while. Almost always, the statement is a code block:
条件是计算结果为零(视为false)或非零(视为true)的表达式。当条件为true时执行语句,然后控制返回到while。语句几乎总是是一个代码块:
while( condition ) { block}
The statement or block that follows the while should ensure that condition will eventually become zero (false) and end the loop.
while后面的语句或块应确保条件最终变为零(false)并结束循环。
Here is the syntax of the for statement:
下面是for语句的语法:
for( initialize ; condition; bottom ) body
Almost always the body is a code block:
循环体总是一个代码块:
for( initialize ; condition; bottom ) { body}
5 具有单个入口点和单个出口点的结构(任何大小)相当于一个代码块
In all of the structures so far there has been an entry point at the top and an exit point at the bottom. There are no jumps into or out of the structure. The entry point has entry conditions that are expected to be satisfied, and exit conditions that are fulfilled if they are. Each of the structures can be considered to be a code block.
到目前为止,在所有结构中,顶部有一个入口点,底部有一个出口点。没有跳进或跳出结构。入口点具有预期要满足的进入条件,如果满足,则具有要满足的退出条件。每个结构都可以被视为一个代码块。
It is OK that there are several paths within a structure, but from the outside, each structure looks like a code block with one entry and one exit.
一个结构中有多条路径是可以的,但从外部看,每个结构就像一个代码块,有一个入口和一个出口。
In flowcharting terms, any code block can be replaced by any of the three structures sequence, alternation, or iteration. Or, going the other direction, if there is a portion of the flowchart that has a single entry point and a single exit point, it can be summarized as a single code block.
在流程图术语中,任何代码块都可以被序列、选择或迭代这三种结构中的任何一种替换。或者,如果流程图的一部分有一个入口点和一个出口点,则可以将其概括为一个代码块。
6 具有单个入口点和单个出口点的结构(任何大小)相当于代码块
(A structure (of any size) that has a single entry point and a single exit point is equivalent to a code block)
For example, say that you are designing a program to go through a list of signed integers calculating the absolute value of each one. You might (1) first regard the program as one block, then (2) sketch in the iteration required, and finally (3) put in the details of the loop body.
例如,假设您正在设计一个程序来遍历一个有符号整数列表,计算每个整数的绝对值。您可以(1)首先将程序视为一个块,然后(2)在所需的迭代中绘制草图,最后(3)输入循环体的细节。
Or, you might go the other way. Once the absolute value code is working, you can regard it as a single code block to be used as a component of a larger program.
或者,你可以走另一条路。一旦绝对值代码正常工作,就可以将其视为单个代码块,用作更大程序的组件。
7 代码块可以构建为函数(function)
A flowchart can become arbitrarily complex and still be structured. But if there are many boxes, it is hard to visualize what is going on. Some of the boxes should be grouped into a function that is separately flowcharted.
流程图可以变得任意复杂,并且仍然是结构化的。但如果有很多盒子,很难想象发生了什么。一些盒子应分组为单独绘制流程图的函数。
In flowcharts, a function call is shown as a box with double lines on its sides. The picture shows an if-then-else structure that chooses between two functions.
在流程图中,函数调用显示为一个框,框的两侧有双线。图中显示了在两个函数之间进行选择的if-then-else结构。
Functions can be complex. If the logic of a function becomes too complex, some of that logic can be made into other functions. Functions can call functions, of course. Ideally, the logic of each function corresponds to an easily understood structured flowchart.
功能可能很复杂。如果一个函数的逻辑变得过于复杂,则可以将其中一些逻辑转换为其他函数。当然,函数可以调用函数。理想情况下,每个功能的逻辑对应于易于理解的结构化流程图。
A programming team consists of several individuals all working on a project. If the design of a project is well-structured, the flowcharts correspond to programs which can be assigned to individual programmers. Usually the programs consist of individual source files which will be separately compiled and them linked together to form an executable.
编程团队由几个个人组成,他们都在一个项目上工作。如果项目的设计结构良好,流程图对应的程序可以分配给各个程序员。通常,程序由单独的多个源文件组成,这些源文件将被单独编译,并将它们链接在一起以形成一个可执行文件。
8 自顶向下(top-down)结构化设计
The absolute value problem is an example of Top-down structured design. When using this design method you start with just one program block, and then replace that block with one of the structures sequence, alternation, or iteration. You then refine the new flowchart by replacing one of its blocks with one of the same structures. Continue replacing blocks with structures until the flowchart has enough detail to work as a design for a program.
绝对值问题是自顶向下结构化设计的一个例子。当使用这种设计方法时,您只需从一个程序块开始,然后将该块替换为序列、选择或迭代结构之一。然后,通过将新流程图的一个块替换为相同的结构之一来优化新流程图。继续用结构替换块,直到流程图有足够的细节作为程序的设计。
For example, say that you want to design a program that adds up integers entered by the user. The initial design might look like the picture as below.
例如,假设您想设计一个程序,将用户输入的整数相加。最初的设计可能如下图所示。
(Actually, you would probably do this step in your mind and not actually draw the flowchart.)
(实际上,您可能会在脑海中完成这一步,而不是实际绘制流程图。)
Usually programs need an initialization step, a middle step that does most of the work, and a final step to finish the work.
通常,程序需要一个初始化步骤、一个完成大部分工作的中间步骤和一个完成工作的最后步骤。
The initialization step for our problem initializes the sum to zero.
问题的初始化步骤将总和初始化为零。
The chart is still not detailed enough for programming. It needs some detail about how all those integers are to be read in. A loop is needed.
该图表对于编程来说仍然不够详细。它需要一些关于如何读入所有这些整数的细节。需要一个循环。
After initialization, the program loops as long as there is data. When it reaches the end of the data, it writes out the answer and exits.
初始化后,只要有数据,程序就会循环。当到达数据末尾时,它写出答案并退出。
There are still some details left to specify, such as how the end of data is determined. This might be done with a special sentinel value, or by prompting the user for a “yes” or “no” response. But the over-all design for the program will be the same.
还有一些细节需要指定,例如如何确定数据的结尾。这可以通过一个特殊的sentinel值来完成,或者通过提示用户“是”或“否”响应来完成。但该计划的总体设计将是相同的。
In almost all problems (in life or in programming) it helps to have a high-level plan of what you are trying to achieve. A high level plan keeps you focused on the task and directs your efforts. If you don’t have a high-level plan, you may think only in terms of what to do next from moment to moment, and you may never reach your goal. You might flounder forever, making small moves that don’t add up to progress. This is like wandering aimlessly through a murky woods without a map. Or like trying to write a novel by picking one word at a time. Not likely to succeed.
在几乎所有的问题(生活中或编程中)中,有一个高层次的计划来帮助你实现目标。一个高水平的计划可以让你专注于任务并指导你的努力。如果你没有一个高水平的计划,你可能只考虑下一步要做什么,而你可能永远也达不到你的目标。你可能永远都在挣扎,做出一些小动作,但这些小动作加起来并不能带来进步。这就像在没有地图的阴暗森林中漫无目的地徘徊。或者喜欢一次只选一个词来写小说。不太可能成功。
In programming, a high-level design is given by a top-level structured flowchart (very often the Universal Flow Chart, modified to fit your task). Structuring forces the chart to be a genuine design and not merely a poorly-conceived collection of boxes and lines. Any program can be described by a structured flow chart. If you design your program in a chart using a small number of boxes, it will be a top-level design.
在编程中,高层设计由顶层结构化流程图(通常是通用流程图,经过修改以适合您的任务)给出。结构化迫使图表成为真正的设计,而不仅仅是构思拙劣的方框和线条集合。任何程序都可以用结构化流程图来描述。如果使用少量方框在图表中设计程序,则它将是顶级设计。
Now you can more sensibly focus on smaller tasks, and then yet smaller tasks, using the same method. This method of divide and conquer is a very powerful technique.
现在,使用相同的方法,您可以更明智地专注于较小的任务,然后再专注于更小的任务。这种分而治之的方法是一种非常强大的技术。
9 结构化编程的思想适用于任何编程语言
(The ideas of structured programming work for any programming language)
Structuring principles can be used with any programming language, even with unstructured programming languages like assembly language or the early versions of BASIC and FORTRAN. With unstructured languages it is up to you (the programmer) to implement structures without the support of the syntax of the language.
结构化原则可以用于任何编程语言,即使是汇编语言或BASIC和FORTRAN的早期版本等非结构化编程语言。对于非结构化语言,您(程序员)可以在不支持该语言语法的情况下实现结构。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至87172970@qq.com举报,一经查实,本站将立刻删除。