Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记一

中文翻译为:数据结构——C++版(第四版)Michael Main,  Walter Savitch 著

Chapter 1   The Phases of Software Development(软件开发的几个阶段)

Preface One     This chapter illustrates(阐明) the phases of software development.

    These phases occur in all software, including the small programs that you'll see in this first chapter. In subsequent(随后的) chapters, you'll go beyond these small programs, applying the phases of software development to organized collections of data.

    These organized collections of data are called data structures.(数据结构)

    Here is a list of the phases of software development:

  • Specification of the task
  • Design of a solution 
  • Implementation (coding) of the solution
  • Analysis of the solution
  • Testing and debugging
  • Maintenance and evolution of the system
  • Obsolescence(退化)
    Note 1:Do not memorize this list. Because, your practice of these phases will achieve far better familiarity than mere memorization.
    Note 2: In practice, the phases blur into each other;
The language we use in this book is C++.

Preface two   What You Should Know About C++ Before Starting This Text
    1 The C++ language was designed by Bjarne Stroustrup. is an extension of the C language with the purpose of supporting  object-oriented programming (OOP) .
    OOP ----a technique that encourages important strategies of information hiding and component(组件) reuse.
        you should already know how to write, compile, and run short C++ programs. You should know how to use the built-in types, and you should be able to use arrays.the C++ compiler should support the latest features of the ANSI/ISO C++ Standard, which we have incorporated into the text.
    3 Throughout the text, we will introduce the important roles of the C++ Standard Library, though you do not need any previous knowledge of the library. Studying the data structures of the Standard Library can help you understand trade-offs between different approaches, and can guide the design and implementation of your own data structures. 
    when you are designing your own data structures, an approach that is compliant with the STL has twofold benefits:
  • Other programmers will understand your work more easily
  • Your own work will readily benefit from other pieces of the STL, such as the standard searching and sorting algorithms.
Section 1.1 focuses on a technique for specifying program behavior, and you'll also see some hints about design and implementation.
Section 1.2 illustrates a particular kind of analysis:the running time analysis of a program.
Section 1.3 provides some techniques for testing and debugging programs.
Now, we study Section 1.1

1.1 SPECIFICATION, DESIGN, IMPLEMENTATION(问题详述,设计,编码)
    This section we study with a example. 
    The specification is a precise description of the problem;
    The design phase consists of formulating(制定…的配方) the steps to solve the problem;
    The implementation is the actual C++ code that carries out the design.
    This example is converting Celsius temperatures to Fahrenheit. Such a sample is a good specification because it is precise, leaving no doubt about what the program must accomplish. The next step is to design a solution.
   Algorithm is a set of instructions(指令) for solving a problem. During the design of the algorithm, the details of a particular programming language can be distracting, and can obscure the simplicity of a solution.
     We use a rather corrupted kind of English that mixes in C++ when it's convenient. This mixture of English and a programming language is called pseudocode.(伪代码)

Design Concept: Decomposing the Problem (分解问题)
    A good technique for designing an algorithm is to break down the problem at hand into a few subtasks, then decompose each subtask into smaller subtasks, then replace the smaller subtasks with even smaller subtasks, and so forth.
    Eventually the subtasks become so small that they are trivial to implement in C++ or whatever language you are using. When the algorithm is translated into C++, each subtask is implemented as a separate C++ function. In other programming languages, functions are called "methods" or "procedures", but it all boils down to the same thing:The large problem is decomposed into subtasks, and subtasks are implemented as separate pieces of your program.
    For example, the temperature problem has at least two good subtasks: (1) converting a temperature form Celsius degrees to Fahrenheit, and (2) printing a line of the conversion(转换) table in the specified format(指定的格式). Using these subproblems, the first draft of our pseudocode might look like this:
  1. Do preliminary(准备的)  work to open and set up the output device properly.
  2. Display the labels at the top of the table.
  3. For each line in the table (using variables celsius and fahrenheit):
  •     Setcelsius equal to the next Celsius temperature of the table.
  •     fahrenheit = the celsius temperature converted to Fahrenheit.
  •     Print the Celsius and Fahrenheit values with labels on an output line.
But what are the aspects of a good decomposition?
One primary guideline is that the subtasks should help you produce short pseudocode----no mare than a page of succinct(简洁的) description to solve the entire problem and ideally much less than a page.
In your designs, you can also keep in mind two considerations for selecting good subtasks:
              the potential for code reuse.
              the possibility of future changes to the program.
Let's see how our subtasks embody(体现) these considerations.
Code reuse
    Step 1 opens an output device, making it ready for output in a particular form. This is a common operation that many programs must carry out. If we write a function for Step 1 with sufficient flexibility(富有弹性), we can probably reuse the function in other programs. This is an example of code reuse, in which a function is written with sufficient generality(富有一般性) that it can be reused elsewhere.
    In fact, programmers often produce collections of related C++ functions that are made available in packages to be reused over and over with many different application programs.
    Later we will use the C++ Standard Library as this sort of package, and we will also write our own packages of this kind.
Easily modified code
    Decomposing problems also produces a good final program in the sense that the program is easy to understand, and subsequent(随后的) maintenance and modifications are relatively easy.
Information hiding
    In order for a problem decomposition to produce easily modified(修改) code, the functions that you write need to be genuinely separated from one another.
    An analogy(类似)can help explain the notion of "genuinely separated", Suppose you are moving a bag of gold coins to a safe hiding place. If the bag is too heavy to carry, you might divide the coins into three smaller bags and carry the bags one by one. Unless you are a character in a comedy(喜剧), you would not try to carry all three bags at once.That would defeat(误解) the purpose of dividing the coins into three groups.This strategy works only if you carry the bags one at a time.
    Something similar happens in problem decomposition. If you divide your programming task into three subtasks and solve these subtasks by writing three functions, then you have traded one hard problem for three easier problems.
    Your total job has become easier--provided that you design the functions separately.
    When you are working on one function, you should not worry about how the other functions perform their jobs.But the functions do interact. So when you are designing one function, you need to know something about what the other functions do. The trick is to know only as much as you need, but no more. This is called information hiding.

One technique for incorporating(合并) information hiding involves specifying(指定) your functions' behavior using preconditions(前置条件) and postconditions(后置条件).



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值