Specification algorithm code (C ++,)

C ++ coding standards I follow is this "high-quality C ++ / C Programming Guide"

http://www.scholat.com/course/V_courseResourceDownload.html?courseId=1227&resourceId=12836

1. Structure of header files

In order to prevent the repeated header is referenced, should be produced by preprocessing block ifndef / define / endif structure.

With #include <filename.h> header file format to reference standard library (the compiler from the standard library directory to start the search).

With #include "filename.h" format to refer to a non-standard library header files (the compiler from the user's working directory to start the search).

Header files stored only "declaration" rather than store the "definition"

In the C ++ syntax, the class member functions can be defined at the same statement, and automatically inline functions. While this will bring the convenience of writing, but it resulted in inconsistent style, more harm than good. Suggested that the definition of the member function declarations separately, regardless of how small the function body.

Does not promote the use of global variables, as not a phenomenon in the header file extern int value of such statements.

2.1 blank line

Blank line functions as the partition of the program section. Blank lines decent (but more than nothing but less) layout will make the program more clearly. Blank lines do not waste memory, although the print program contains blank lines that will consume some paper, but it is worth. So do not be reluctant to use blank lines. 

After each class declaration, function definition after the end of each blank line will be added.

A function in the body, without a blank line between the closely related bowed logic statements, should be added to other parts of the partition blank line.

// Blank line

void Function1(…)

{

  …

}

// Blank line

void Function2(…)

{

  …

}

// Blank line

void Function3(…)

{

  …

}

 

// Blank line

while (condition)

{

statement1;

// Blank line

if (condition)

{

statement2;

}

else

{

statement3;

}

// Blank line

statement4;

}  

2.2 lines of code

A line of code do one thing, just as the definition of a variable, or write only one statement. This code is easy to read and easy to write a comment.

if, for, while, do so on account statement from his party, the statement is executed not followed. Regardless of how much will be added to execute the statement {}. This prevents writing errors.

2.3 spaces within the lines

After the keywords you want to leave a space. After at least as const, virtual, inline, case and other keywords you want to leave a space, or can not discriminate keywords. As if, for, while after keywords such talk should leave a space left parenthesis '(', to highlight keywords.

Do not leave a space after the function name, followed by the left parenthesis '(' to distinguish keywords.

'(' Followed by back, ')', ',', ';' forward followed by, followed at no spaces.

',' Then to leave a space, such as Function (X, Y, Z) . If ';' symbol is not the end of a line, thereafter to leave a space, such as for (initialization; condition; update) .

Assignment operators, comparison operators, arithmetic operators, logic operators, bit field operators, such as "=", "+ =", "> =", "<=", "+", "*", "% "," before and after && "," || "," << "," ^ " operator should dihydric spaces.

The unary operator "-", "+", "!", "-", "&" (address operator) without spaces before and after.

As " [] ", ".", "->" no space before and after such operator.

For expression longer for the statement and the if statement, for compactness reasons may suitably remove some spaces, such as for (i = 0; i <10; i ++), and if ((a <= b) && (c <= d ))

void Func1 (int x, int y, int z); // good style

void Func1 (int x, int y, int z); // bad style

if (year> = 2000) // good style

if (year> = 2000) // bad style

if ((a> = b) && (c <= d)) // good style

if (a> = b && c <= d) // bad style

for (i = 0; i <10; i ++) // good style

for (i = 0; i <10; i ++) // bad style

for (i = 0; I <10; i ++) // excessive space

x = a <b a: b; // good style?

x = a <b a:? b; // bad style

int * x = & y; // good style  

int * x = & y; // bad style  

array [5] = 0; // Do not write array [5] = 0;

a.Function (); // instead of writing a Function ().;

b->Function();                            // 不要写成 b -> Function();

 

2.4 对齐

程序的分界符‘{’和‘}’应独占一行并且位于同一列,同时与引用它们的语句左对齐。

{ }之内的代码块在‘{’右边数格处左对齐。

 

void Function(int x)

{

… // program code

}

 

void Function(int x){

… // program code

}

 

if (condition)

{

… // program code

}

else

{

… // program code

}

if (condition){

… // program code

}

else {

… // program code

}

for (initialization; condition; update)

{

… // program code

}

for (initialization; condition; update){

… // program code

}

While (condition)

{

… // program code

}

while (condition){

… // program code

}

如果出现嵌套的{},则使用缩进对齐,如:

{

   …

 {

   …

 }

  …

}

 

                                           风格良好的对齐                                                                    风格不良的对齐

这边书有详细全面的代码规范讲解,有助于我们养成良好的代码习惯,在这里不再一一举例;

 

《数学之美》读后感

统计语言模型
  “数学是解决信息检索和自然语言处理的最好工具。”从小到大,我们一直学习数学,在感到枯燥或者乏力之时,我们时常会思考,数学的作用。然而用数学工具解决语言问题,这便是一种数学的美;如何让机器理解人类语言这种最复杂最动态的信息是一大难题,但是人们在研究语法规则的方法上几乎毫无突破,后来随着计算机的发展,用数学方法处理自然语言的方法才得以实现。当时一批杰出的科学家利用大型计算机来处理人类语言问题。统计语言模型就是在那个时候提出的。书中举出了一个简单的统计模型来解决一个文字序列是否能构成一个大家能理解的句子的问题:
句子S出现的概率等于每一个词出现的概率相乘,于是 P(S) 可展开为:
                                                P(S) = P(w1)P(w2|w1)P(w3| w1 w2)…P(wn|w1 w2…wn-1)
假定任意一个词wi 的出现概率只同它前面的词 wi-1 有关(即马尔可夫假设),于是问题就变得很简单了。现在,S 出现的概率就变为:
                                                P(S) = P(w1)P(w2|w1)P(w3|w2)…P(wi|wi-1)… 

事实证明,统计语言模型比任何已知的借助某种规则的解决方法都有效。数学的美妙之处在于把复杂的问题变得简单化,解决实际问题来为人们服务。



 

 

Guess you like

Origin www.cnblogs.com/gwpsf/p/11484976.html