Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7c65dbb

Browse files
committed
add some c style guide
1 parent 5915e19 commit 7c65dbb

File tree

4 files changed

+253
-4
lines changed

4 files changed

+253
-4
lines changed

_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
* [简介](/)
22
* [关于我自己](myself/intro.md)
33
* [LinkedList](linkedlist/)
4+
* [C Style Guide](myself/c_style_guide.md)

linkedlist/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ public class Linkedlist {
2626
我们首先来看如何往单链表里面添加元素。
2727
#### 添加
2828
1. 使用给定的值去初始化新的节点cur;
29-
2. 插入链条当中。
3029

31-
[LeetCode876 题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)
30+
![before](/home/xuetao/.config/Typora/typora-user-images/1578974211336.png)
3231

33-
[LeetCode19 题解](linkedlist/leetcode19.md)
32+
2. 将cur的next字段连接到原来的下一个节点next当中:![linking](/home/xuetao/.config/Typora/typora-user-images/1578974292368.png)
33+
3. 完成。![finish](/home/xuetao/.config/Typora/typora-user-images/1578974316048.png)
3434

35-
[LeetCode 61题解](linkedlist/leetcode61.md)
35+
插入操作是非常高效的,可以在O(1)的时间复杂度中把新节点插入进去,这是非常高效的。
36+
37+
#### 删除
38+
39+
如果想要删除单链表上的节点, 也非常简单,就是删除过程的逆过程。这里不再赘述。
40+
41+
[LeetCode876 题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)

linkedlist/leetcode206.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
leetcode206.md

myself/c_style_guide.md

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
## C 语言风格指导
2+
3+
这份C语言风格指导来自于新南威尔士大学CSE学院的本科课程COMP1511的C语言风格指导。
4+
5+
[COMP1511 style guide](https://cgi.cse.unsw.edu.au/~cs1511/19T1/resources/style_guide.html#code-structure)
6+
7+
8+
9+
本文将会介绍一些推荐的和不推荐的C语言风格。
10+
11+
### 推荐的代码布局
12+
13+
#### 头注释
14+
15+
所有的程序都应该有头注释。
16+
17+
哪怕是非常小的功能点的代码,你的代码至少应该包含:
18+
19+
- 作者的名字
20+
- 日期
21+
- 一个简短的描述,描述你的程序的功能
22+
23+
代码例子:
24+
25+
```c
26+
//
27+
// COMP1511 Lab 10 Exercise - Turing's test
28+
//
29+
// Pretend to be a human in text-based conversation.
30+
// https://en.wikipedia.org/wiki/Turing_test
31+
//
32+
// Authors:
33+
// Grace Hopper (z1234567@unsw.edu.au)
34+
// Ada Lovelace (z5000000@unsw.edu.au)
35+
//
36+
//
37+
//
38+
// Written: 19/03/2019
39+
//
40+
41+
#include <stdio.h>
42+
#include <string.h>
43+
#include <math.h>
44+
45+
#define MEANING_OF_LIFE 42
46+
47+
48+
// function prototypes would go here
49+
50+
51+
int main(void) {
52+
printf("Hello!\n");
53+
return 0;
54+
}
55+
56+
57+
// function definitions would go here
58+
```
59+
60+
#### 括号和缩进
61+
62+
函数体,循环,if-else应该使用_完整的括号_。
63+
64+
代码实例:
65+
66+
```c
67+
int someFunction(int a, int b) { // 推荐这里有个空格
68+
if (a > b) {
69+
// do something
70+
} else if (b < a) {
71+
// do something
72+
} else {
73+
// do something
74+
}
75+
76+
int i = 0;
77+
while (i < 0) {
78+
// do something
79+
i++;
80+
}
81+
}
82+
```
83+
84+
下面是糟糕的代码风格:
85+
86+
```c
87+
// BAD - 在{}之间的代码应该被缩进
88+
89+
if (i % 2 == 0) {
90+
printf("even");
91+
} else {
92+
printf("odd");
93+
}
94+
95+
96+
// BAD - {}应该有换行。
97+
98+
while (i < 0) { i++; }
99+
100+
101+
// BAD - if 和while要有{}
102+
103+
while (i < 0)
104+
i++;
105+
106+
// BAD - 应该有缩进和对齐。
107+
108+
if (a > b) {
109+
// do something
110+
} else if (b < a) {
111+
// do something
112+
} else {
113+
// do something
114+
}
115+
```
116+
117+
### 缩进
118+
119+
在任何的括号之间,应该有**4个空格**的缩进。
120+
121+
在以下关键词之后需要有**一个空格**:
122+
123+
`if `, `while`,`for`, `return`
124+
125+
在下列二元操作符有空格:
126+
127+
`=`, `+`,`-`,`*`,`<`,`>`,`/`,`%`,`<=`,`>=`,`==`,`!=`
128+
129+
不需要在前置单元操作符上使用空格:
130+
131+
`& `,`*`,`!`
132+
133+
同样的,下面也不需要空格:
134+
135+
`++`,`--`
136+
137+
`.`,`->`
138+
139+
代码实例
140+
141+
```c
142+
143+
// 下面是正确的
144+
if (a + b > c) {
145+
i++;
146+
curr = curr->next;
147+
}
148+
149+
150+
// 下面是不正确的:
151+
if(a+b>c) {
152+
i ++;
153+
curr = curr -> next;
154+
}
155+
```
156+
157+
**一行最多有80个字符(包括空格)**
158+
159+
### 常量
160+
161+
使用 `#define`来定义常量
162+
163+
所有使用`#define`的常量,需要用大写字母和下划线来表示。
164+
165+
代码实例:
166+
167+
```c
168+
#define PI 3.14
169+
#define DAYS_OF_WEEK 7
170+
171+
// ...
172+
int array[DAYS_OF_WEEK];
173+
int i = 0;
174+
while (i < DAYS_OF_WEEK) {
175+
array[i] = i;
176+
i++;
177+
}
178+
```
179+
180+
### 变量
181+
182+
变量名应该尽可能的描述性。
183+
184+
应该遵从`snake_case`或者`camelCase`的方式来命名。
185+
186+
不推荐使用全局变量,不允许使用静态变量
187+
188+
全局变量是定义在函数外部的变量,这会使得代码难以阅读和维护。
189+
190+
使用`static`关键字声明的变量是不允许的。但是可以使用它来声明函数,来实现隐藏和封装。
191+
192+
### 函数
193+
194+
把大量重复,相似的代码重构成函数,这样可以提高你的代码能力和代码质量。
195+
196+
不允许函数定义和函数实现写在一起。
197+
198+
函数注释应该使用多行注释, 指定参数类型,返回值。
199+
200+
代码实例:
201+
202+
```c
203+
/**
204+
* author: Alex
205+
* date: 2020/01/01
206+
* args: none
207+
* return: none
208+
* just demostrate how to define a function.
209+
*/
210+
void func(void);
211+
/**
212+
* author: Alex
213+
* date: 2020/01/01
214+
* args: int a, int b
215+
* return: int
216+
* give 2 integer's sum.
217+
*/
218+
int sum(int, int);
219+
220+
// ...
221+
void func(void) {
222+
printf("hello world!\n");
223+
}
224+
225+
int sum(int a, int b) {
226+
return a + b;
227+
}
228+
```
229+
230+
231+
232+
### 推荐
233+
234+
- 避免使用switch, 使用if - else if - else if
235+
- 不要使用goto
236+
- 不要使用 `:?`这样的三元运算符
237+
- 避免使用 `do {} while(condition)`, 使用while循环来替代
238+
- 不要使用联合体。(union)
239+
- 避免指针的算数操作
240+
- 避免类型转换。
241+

0 commit comments

Comments
 (0)