Since the mid-1960s,industry has used the concept of structured program development. Since that time the structured methodology has gained such acceptance that we introduce it immediately. Let’s summarize the process。
The first and most important step in programming is to define the problem. You may have a perfectly written program but it is useless if it doesn’t solve the correct problem. As part of the problem definition, write down exactly what you want the program to do, including a description of the input to the program and the desired output. Until you can do this, you can't begin to write the program.
After defining the problem, the next step is to break the program that is supposed to solve it ontological sections,or modules.We can represent these modules graphically using a structure chart. An or-generation chart has many similarities to a structure chart: the top boxes in each represent the modules or people that control the program flow or activity in the boxes under them, and the bottom boxes represent the level at which most of the detail work is done.
The upper modules or "manager modules” of a structure chart direct processing to the proper place at the proper time. The worker modules each perform one simple task.
A program is much easier to write, test and modify if we break each of the top modules into smaller and smaller modules, much as we would write an outline. The process is called top-down programming. For example, if we must calculate pay, we might create sub modules to compute regular pay, overtime pay and deductions.
As we break the problem into sub modules, we will get to a point where the module can no longer be decomposed. At this stage we will code the module using pseudopodia, a human- language like programmer's shorthand that can outline the steps that the module must perform. Pseudopodium is BASIC without commas or exact syntax; indeed, the actual translation to BASIC code is quite simple.
Pseudopodia is useful both for program design and for program documentation.Another aid to pro-gram documentation and design is the flowchart. Flowcharting provides a highly visual and easily understood way of representing the program's flow of logic.
Once the program is defined and designed, we encode (or code) it in a programming language again using structured techniques. We have described the logic of each worker module using pseudopodia and/oar flowchart. We have recognized that the manager module or modules direct the order in which the worker modules are called on to perform their tasks.At this point, the translation to BASIC is straight for-ward.
Each worker module becomes what BASIC callas subroutine. Within the subroutines are lines; of BASIC code that may be almost line by -line translations from pseudopodia。
The main manager module (at the top of the structure chart) becomes the driver module. In most programs that you write at this stage of your knowledge, the driver module calls subroutines (worker modules) directly, in proper sequence, to perform their tasks. Later we will introduce "middle manage-management",that is,subroutines called by the driver module that in turn call lower-level subroutines to perform the lowest level tasks.
As an example, see figure 4-1,in BASIC terms, the Payroll Process module becomes the driver module of the BASIC program. It calls the Read Input subroutine (a middle manager),which in turn calls two more subroutines(worker modules):Read Employee Master Record and Read Individual Timecards. As you can see from the Calculate Pay part of the structure chart, there can be more than one level of middle management!For example, once all this is translated to BASIC, the driver module (Payroll Process) would at the correct time call the subroutine Calculate Pay, which at the correct time would call the subroutine Calculate Gross Pay, which at the right time would (finally!) Call the subroutine Calculate Regular Pay.
At first, modular programming may seem cumbersome to you, especially for simpler programs.Why use a subroutine, some would ask, when you can just write fewer instructions”nine line"?The reasons become clear when the programs become lengthy and when each subroutine consists of many lines of code. The modular approach allows the programmer to write in a logical fashion. Coding the general outline first and then coding each section separately. In the past, programmers would be almost finished writing a program only to discover a major flaw in their logic, which often required rewriting nearly the entire pro-gram. With modular programming the basic logic is written and checked first. Because each set of steps is written as a separate module, changes and corrections made to that section don't require changes to other modules. For these reasons, most companies have standardized on structured program development for all their computer tasks.
翻译:
程序结构
60年代中期,在计算机行业中 提出了结构化程序设计的概念。自那以后,结构化程序设计技术得到 如此广泛的承认以致我国也立即引 进了这种技术。现在就让我们来看 一下结构化程序设计的过程。
结构化程序设计中,第一步也是最关键的一步就是确定所要解决 的问题。你也许可以编写出一个很完美的程序,但如果它不能正确地解决问题,是没有任何用处的。在明确问题的过程中,先写下想要程序完成哪些功能,包括对程序的输入和理想的输出的描述,然后才能开始编程。
问题确定之后,下一步就是把 解决问题的程序分解成逻辑上合理 的几个部分,即模块。我们可以压 结构图来表示这些模块。结构图与 组织机构图有很多相似之处:图中 顶层的盒子所代表的模块或领导者,对其下的盒子中程序流起控制作用,最底层的盒子代表是所有必须完成的最具体的工作。
结构图中较上层的模块或称“管理模块”指明了“适时适地干 适事”的程序。每个工作模块只执行一项任务。
如果将每个顶层模块都分解为很多越来越小的模块,并画出详细的框图,则程序的编写、调试和修改工作就变得简单多了。这个过程就叫做自顶向下程序设计。比如,我们在计算工资时,可设子模块分别计算出固定工资、加班费和扣除额等项内容。
在把问题分解为子模块时,一直要分解到模块不能被再分解为止。这时的模块可用伪代码来编写,即一种类似人类语言的程序员速记,能将模块必须执行的步骤列示出来。伪代码是没有标点或语法 的BASIC语言。事实上,真正要翻译成BASIC语言代码是很简单的。
伪代码可用于程序设计和程序 注释。另外.流程图也有助于程序 注释和程序设计。用流程图来表示 程序执行的逻辑顺序,是一种既高 度可视化,又易于理解的方式。
程序一旦确定并设计好执行顺序之后,便可运用结构化技术,通过编程语言将其代码化。前面我们已描述过用伪代码或流程图表示出各个工作模块的逻辑执行顺序。也己知道管理模块指示的顺序正是其 调用工作模块执行任务的顺序。这时,直接将流程图转化为BASIC语言即可。
每个工作模块就是BASIC语言中称作的子程序。每个子程序由很多行BASIC语句组成,这些语句几乎全是由伪代码逐行转换而来。
主模块(结构图的顶层模块) 即是驱动模块。在大多数程序中,凭你所知即可写下主模块。驱动模 块可在适当的时候直接调用子程序 (工作模块)执行相应的任务。接下来我们将介绍“中层管理”模块,即被驱动模块所调用,同时又可顺次调用较低层子程序去执行最底层任务的那些子程序。
如图4-1,在用BASIC语言编写 的程序中,工资单处理模块是驱动 模块。该模块调用读输入子程序(工作模块):读雇员和雇主记录 子程序以及读个人工作时间卡片子 程序。从结构图中的计算工资子程序可看出,中层管理模块可不止一 层!比如,当结构图全部转换为BASIC语言程序时,驱动模块(工资单处理)将适时调用计一算工资子 程序,计算工资子程序适时地调用 计算总工资子程序,而计算总工资 子程序又将(最后!)适时地调压 计算固定工资子程序。
最初,模块化程序设计看起来可能很麻烦,尤其是相对于较简单的程序而百。有人或许会问,当你可以直接地逐行编写较少的指令时, 为什么要采用子程序设计的方式呢?在程序变得足够长且每个子程序都由很多行语句组成时,采用子程序的原因就很明显了模块化字设计方法使编译者可按一定的顺序,完成总框图的代码表示私 各个部分的独立编程。过去,程序员在快要完成整个编程工作时,可以发现程序在逻辑顺序上存在一个主要错误,这经常导致几乎整个程导的重新编写。而采用模块化技术编程时.首先编写和检查的就是程子的基本逻辑关系。同时各个子程序都是作为单独的一个模块编写的.修改时不需要改变其他模块。由于这些原因,许多公司都将结构化工序设计工作标准化以完成各项计算计编程任务。
责任编辑:cyth