Self--Study Chapter One (Summary programme principle)

来源:互联网 发布:炫酷导航网站源码 编辑:程序博客网 时间:2024/06/08 18:57

                            Self--Study Chapter One

                          (Summary programme principle)

The Game of Life

解决方案:类,对象和方法

1类,对象(class  object:C++中,我们使用类(class)来组合数据和用于访问或修改这些数据的方法,数据和方法这样的组合称为属于给定类的对象。For the Life game,we shall call the class Life,so that configuration becomes a Life object.we shall then use three methods for this object:initialize()will set up the in initial configuration of living and dead cells;print()will print out the current configuration ;and update()will make all the changes that occur in moving from one generation to the next.

2 C++类,方法(C++classes methods

每个C++类由表示变量或函数的成员所组成。表示变量的成员称为数据成员(data members),这些成员用于存储数据值。表示某个类的函数的成员称为方法(members)或成员函数(member functions),

类的方法一般用于访问或改变数据成员。

3成员选择运算符(member selection operator

We can now apply methods to work with configuration ,using the C++operator .(the member selection operator ).For example ,we can print out the data in configuration by writing:

4私有和公用(private and public

当开始实现类life时 ,需要决定如何存储数据,并且需要变量和函数来操作这些数据,然而所有这些变量和函数是这个类私有的,客户仅需要这个类说明和定义的公用方法。

Convention Methods of a class are public .Functions in a class are private.

5使用程序包(utility package) 此程序已包含文件开始,使得它能够用类life和标准C++的输入和输出库。使用函数user_says_yes()utility.h中声明,对于Life程序,我们所需要的关于utility.h的仅有的其他信息是以如下的指令开始:

      #include<iostream>

      using name space std;

这条指令允许我们使用标准的C++ 输入和输出流,如cincout(在较老的编译器上,一条替代的指令#include<iostream.h>具有同样的效果)

Programming Precept:Include precise preconditions with every program,function,and method that you write.

6函数(function

程序说明的第三部分是它所使用的类和函数的列表,每个程序,函数或方法都应包含一个类似的列表。

7程序的动作(action of the program

Life 程序中我们仍然必须编写代码来实现:

The class life.

The method initialize ()to initialize a life configuration.

The method print()to output a Life configuration.

The method update ()to change a life object so that it stores the configuration at the next generation .

The function use_says_yes()to ask the user whether or not to go on to the next generation.

The function instructions() to print instructions for using the  program.

 

Programming Style

1名称

特别注意的是:使用的前缀和后缀来关联同一常规类型的名称,例如在一个程序中使用的文件可以被称为:

Input_file   transaction_file total_file out _file reject_file

避免采用故意的误拼和无意义的后缀来获得不同的名称

避免那些本身意义与问题毫无关系或只有很少关系的漂亮名程

避免选择拼写互相接近或者其他方面一语混淆的名称

应小心使用字母“l”(小写的ell),“o”(大写的oh)和“0”(零)

2文档和格式(Documentation and Format)

1)注意避免机械模仿代码的功能的注释,如:

cout++;   //counter1

或者使用无意义的行话,如

//Horse string length into correctitude

(2)格式 程序中的空格空白行和缩排是一种重要的文档形式

Programming Precept

The reading time for programs is much more than the writing time.Make reading easy to do.

3细化和模块化(Refinement and Modularity

参数:细化过程最重要的部分之一就是确切地确定每个函数的任务,精确地确定每个函数的任务,首先必须对函数中使用的参数进行精确的说明(1)输入参数使用单不被函数修改,在C++中,输入参数是通过值传递的。(2)输出参数包含函数的计算结果。

 

三 编码,测试和进一步细化CODINS,TEST,AND,FURTHER REFINEMENT

1占位函数(stubs

为正确的编译主程序,再用到的每个函数上应该有点东西,因此我们必须放上简短的哑函数,又称为占位函数。Note that in writing the stub functions we must at least pin down their associated parameters and return types

2更新网络,方法update

首先使用存储在配置中的数据来计算名为new_gride的矩形数组中的各个元素,new_gride记录了更新后的配置,然后逐个元素地将new_grid复制回Life对象的grid成员中,为创建new_grid,我们用一个在行row和列col上的嵌套的循环,在矩阵数组grid中的非栅栏元素上运行一遍,这些嵌套的循环体由多路选择语句switch组成,函数neighbor_count(row,col)的返回值是0~8中的某一个,对其中的每种情况可以采取一种不同的动作。

3输入和输出(1instructions

流出运算符“<<”是预定义的插入符,作用在流对象的cout上便可以实现最一般的屏幕输出。格式如下;

cout<<表达式1<<表达式2<<...

(2)initializations 

输入运算符 最一般的键盘输入是将提取符作用在流类对象cin上,格式如下cin>>表达式1>>表达式2>>......

用户响应 在C++中,函数get()实际上仅是类istream的一个方法,在我们的应用程序中,应有的方法cin.get()属于istream的对象cin