Code Specification & "mathematical beauty" Book

   A code specification

Reference from HTTP: // c.biancheng.net/view/158.html

1) blank line

Blank line functions as the partition of the program section. Blank lines decent layout will make the program more clearly. Blank lines will not waste memory, although the program contains a print blank lines will consume some paper, but it is worth.

Rule number one: After you define the variable to be a blank line. As defined in the variable initialization of variables at the same time, that is to follow the principle of proximity. If the variable references and definitions relatively far apart, then initialize variables can easily be forgotten. If uninitialized variable is referenced, it will cause an error.

Rule number two: after the end of each function definition must add a blank line.

General Rules: two independent blocks, must be added after the empty row variable declaration. Few lines of code such as the above is a complete functional, the following lines of code completion is another function, then you should add a blank line between them. So it looks more clear.

2) Space

Rule number one: to leave a space after keywords. After at least as const, case and other keywords you want to leave a space, or can not discriminate keywords. After like if, for, while keywords such talk should leave a space left parenthesis (, to highlight keywords.

Rule number two: Do not leave a space after the function name, should be followed by a left parenthesis (, to distinguish keywords.

Rule number three: (back followed; ), ,, ;the three forward followed; followed at no spaces.

Rule Four: ,After going to stay spaces. If ;not the end symbol of a line, followed by a space to stay.

Rule 5: assignment operators, relational operators, arithmetic operators, logic operators, bitwise operators, such as =, ==,! =, + =, - =, * =, / =,% =, >> =, << =, & =, ^ =, | =,>, <=,>,> =, +, -, *, /,%, &, | front, &&, ||, <<, >> , ^ and other binary operator should spaces.

Note that the operator "%" is the remainder operator, and in printf% d of "%" are different, so the% d "%" before and after no spaces.

Rule six:! Unary operator, -, +, -, - front, *, etc. & no space.

note:

  • Here the "-" and Rule 5 inside the "-" different. Here, "-" operator is negative, the inside of Rule 5 "-" is a subtraction operator.
  • Here the "*" and Rule 5 inside the "*" is also different. Here, "*" is the pointer operator, Rule 5 inside the "*" is the multiplication operator.
  • Here the "&" and Rule 5 inside the "&" is also different. Where "&" is an address operator, Rule 5 inside the "&" is a bitwise AND operator.


In conclusion, six of unary operators are the rule, and the rule is a binary operator Fifth, they are not the same.

Rule seven: image array notation [], structure members operator ., the operator points to the structure members ->before and after such operators without spaces.

Rule Eight: For an expression for a long statement and if statements, for the sake of compactness, can properly remove some space. However, for keeping the space and can not be deleted if the back, behind the statement can be appropriately removed based on the length statement a number of spaces. E.g:

for (i=0; i<10; i++)

and behind the space reserved for it semicolon, =and <before and after the space can be removed.

3) Writing in pairs

The pair of symbol pairs must be written as (), {}. Do not finish the opening parenthesis and then write the contents of the last fix right parenthesis, it is easy to miss right parenthesis, especially when writing nested procedures.

4) indent

Indent is by the Tab key on the keyboard to achieve, indentation can make the program more layered. Principle is: if equal status, you do not need indent; if one part of the internal code of the code you need to indent.

5) aligned

Align mainly for braces {}said:

Rule number one: {and }each must be on a separate line. They are a pair of {and }be in the same column, and aligned with reference to their statement left.

Rule number two: {}in the inside of the Code To indent a Tab, and aligned to the left in the same position, a different position continue to indent.

 

6) lines of code

Rule number one: one 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.

Rule number two: if, else, for, while , do so on account statement from his party, the statement is executed not followed. In addition, a very important point is that no matter how many rows the statement is executed, even if only one line should also increase {}, and follow the principles of alignment, which can prevent write errors.

7) Notes

C language commonly used in the comment line //…, multi-line comments must be used /*…*/. Comments are used to important lines or paragraphs prompt. In general, the source must be an effective amount comment more than 20%. Although comments help to understand the code, but be careful not to use too much comment.

Rule number one: the code of comment is "prompt" rather than a document. Program notes can not overwhelming and too much will comment dazzling.

Rule 2: If the code has always been clear, it is not necessary to add a comment.

Rule number three: while writing the code side comment, modify the code at the same time to modify the notes, in order to ensure consistency with the annotated code, no longer useful comment to be deleted.

The fourth rule: When the code is relatively long, especially when there are multiple nested, comments should be added at the end of the paragraph, it is easy to read.

Rule 5: the right of each macro definition must have a note about its role.

The following functions of the program reference code is a quadratic equation to any AX 2 + BX + C = 0 Solutions to find out.

The include # <stdio.h> 
# the include <math.h> / * since the use squaring function sqrt (), so to include the header file math.h * / 
int main ( void ) 
{ 
    // the three coefficients stored the computer 
    int a = . 1 ;   // "=" represents not equal, but rather the assignment 
    int B = 2 ;
     int C = . 1 ;
     Double Delta;    // Delta is stored in b * b - 4 * a * c of value 
    Double X1, X2;   // are used to store the two solutions of a quadratic equation 
    Delta B * B = - . 4 * a * C;
     IF (Delta> 0 )
    { 
        X1 = (-b + sqrt (Delta)) / ( 2 * A); 
        X2 = (-b - sqrt (Delta)) / ( 2 * A); 
        the printf ( " the quadratic equation has two solutions, % F = X1, X2 = F% \ n- " , X1, X2); 
    } 
    the else  IF ( 0 == Delta) 
    { 
        X1 = (-b) / ( 2 * A); 
        X2 = X1;   // left value assigned to the right of 
        the printf ( " the quadratic equation has a unique solution, X1 = X2 = F% \ n- " , X1); 
    } 
    the else 
    { 
        the printf ( " no solution \ n");
    }
    return 0;
}

 

                                                                                          

Second, the beauty of mathematics book review

         Beauty of mathematics is a very good book.

         For computer science, the mathematics is something we always want to reach. Before I did not like math this subject, always I felt that this subject really learned nothing useful, but probably calculate price to buy things, buy buy stocks. But with the study, I feel more and more mathematical beauty. And Wu Jun "mathematical beauty" of this book, as if combing again my application of mathematical knowledge in computer terms, some vague when I learned of this year, want to know a lot of knowledge about the same time I have to popularize a lot of knowledge. If I see this book as soon as possible, you may be a little more like math.

      For example, "information entropy", binary arithmetic, Boolean algebra widely used in life, he said, "to create a search engine generally need to do a few things: automatically download as many pages; establish a rapid and effective index; according to their relevance web pages fairly accurate sorting. ", and he called the Internet compared to a large map, each page is a node, as the arc of those hyperlinks web page. Such a description, the Internet feels like a big spider web. The "Building a search engine generally need to do a few things: automatically download as many pages; establish a rapid and effective index; pages according to their relevance to a fair and accurate sorting.", You need to look for the basic operation of the Boolean operations , read author about the content, will marvel at how mathematics so easy to use. Boolean operators contacted mathematical and logical, easy to implement and fast, so we can quickly find what they're looking for on the Internet vast amounts really should thank Boolean operations, thanks to the great predecessors. Web crawlers also traversal algorithm graph theory (based on BFS, the DFS), also plays an important role, he said above, we use the Internet as a drawing, each page as a node, beginning from Home A portal , first download the page, then by analyzing this page, you can find all hyperlinks hidden in it, also the same as knowing all this portal home page as a direct connection. Next visit, download and analyze e-mail this and other web portals, and can be found attached to other pages. We let the computer keep doing it, you can download the entire Internet. Of course, we have to record which pages downloaded, and to avoid duplication. In web crawlers, we use called "hash table" list (Hash Table) rather than a notepad record information whether a web page to download before.   

    Author really powerful, esoteric mathematical concepts, he can use fun-to-life examples about. The book is really good book, I hope more people can see.

Third, this term plans to achieve using a software algorithm: Maps navigation software

 

Guess you like

Origin www.cnblogs.com/xyishere/p/11484785.html