说说我的代码格式习惯之C/C++篇

来源:互联网 发布:完美解码 知乎 编辑:程序博客网 时间:2024/05/17 00:54

 代码格式习惯是写程序中一个非常重要的问题。除非你是一个人单干,否则如果你没有良好的代码格式规范,别人读你的代码将会很头大。

 个人认为自己的代码格式习惯还不错,有一个良好的习惯,别人看着也不至于太累。

先说第一个:

Code:
  1. #include <iostream>  

在include之后空一格。

接下去是两种大括号习惯:

Code:
  1. int main() {  
  2. }  
  3.   
  4. int main()  
  5. {  
  6. }  

第一种风格有点趋向于java,不过这只是我个人理解。但是在用于struct的时候,我的大括号是第一种情况,但是小括号之后有一个空格。

而第二种就没什么说的,C/C++中的大括号我都是用这种的,除了上面说的struct。

然后是运算符。

Code:
  1. for(int i = 0; i < 5; i++)  
  2. {  
  3. }  
  4.   
  5. //================  
  6.   
  7. a = 1 + 2 + b * 5 + (7 + 2);  

不管在哪里,在运算符的两边都要空一格,当然自加自减之类的不算在内。而在分隔符(如逗号和封号)之后空一格,之前不空格。分隔符的这种风格是英文风格,在英语中,标点符号前不空格,后面空格。不信的同学们可以在word中试试。如果前后都不空,那么word会划出下划线说有语法错误,如果在后面空了就没有语法错误了。

然后是数组大括号:

Code:
  1. int a[4] = { 1, 2, 3, 4 };    
  2. int b[2][2] = { { 1, 2 }, { 3, 4 } };  

大括号里面前后各空一格。

接着是TAB键,在C/C++中,缩进是很重要的。不过我通常在IDE或者编辑器中使用空格替换TAB。因为在不同的环境中TAB的大小是不一样的,通常我都是用四个空格代替。

Code:
  1. for(int i = 0; i < 5; i++)  
  2. {  
  3.     cin >> a[i];  
  4. }  

下面提供一段我的风格的DFS迷宫:

Code:
  1. #include <iostream>  
  2. #include <cstring>  
  3. #include <list>  
  4. using namespace std;  
  5.   
  6. struct coor {  
  7.     int x, y;  
  8.     bool operator == (coor a)  
  9.     {  
  10.         if(a.x == x && a.y == y) return true;  
  11.         else return false;  
  12.     }  
  13.   
  14.     coor operator += (coor a)  
  15.     {  
  16.         x += a.x, y += a.y;  
  17.         return *this;  
  18.     }  
  19.   
  20.     coor operator -= (coor a)  
  21.     {  
  22.         x -= a.x, y -= a.y;  
  23.         return *this;  
  24.     }  
  25.   
  26.     bool pend(int n, int m)  
  27.     {  
  28.         if(x >= 0 && y >= 0 && x < m && y < n) return true;  
  29.         else return false;  
  30.     }  
  31. };  
  32.   
  33. int n, m;  
  34. int mat[20][20];  
  35. bool vis[20][20], ok = false;  
  36. coor op, ed;  
  37. list<coor> l;  
  38. const coor dir[4] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };  
  39. int all;  
  40.   
  41. void init()  
  42. {  
  43.     cout << "Input Row and Col: ";  
  44.     cin >> n >> m;  
  45.     cout << "Input the matrix:" << endl;  
  46.     for(int i = 0; i < n; i++)  
  47.         for(int j = 0; j < m; j++) cin >> mat[i][j];  
  48.   
  49.     cout << "Input the entrance coor: ";  
  50.     cin >> op.x >> op.y;  
  51.     cout << "Input the exit coor: ";  
  52.     cin >> ed.x >> ed.y;  
  53.   
  54.     cout << "Input if you wanna see all the solutions(0 / 1): ";  
  55.     cin >> all;  
  56.     l.push_back(op);  
  57.   
  58.     memset(vis, 0, sizeof(vis));  
  59. }  
  60.   
  61. void success()  
  62. {  
  63.     cout << endl;  
  64.     cout << "There'are total " << l.size() << " steps: " << endl;  
  65.   
  66.     for(list<coor>::iterator i = l.begin(); i != l.end(); i++)  
  67.         cout << " --> (" << i->x << ", " << i->y << ") <--" << endl;  
  68. }  
  69.   
  70. void dfs(coor now, coor ed)  
  71. {  
  72.     if(now == ed)  
  73.     {  
  74.         if(!all) ok = true;  
  75.         success();  
  76.     }  
  77.   
  78.     vis[now.y][now.x] = true;  
  79.     for(int i = 0; i < 4; i++)  
  80.     {  
  81.         now += dir[i];  
  82.         if(now.pend(n, m))  
  83.         {  
  84.             if(!mat[now.y][now.x] && !vis[now.y][now.x])  
  85.             {  
  86.                 l.push_back(now);  
  87.                 dfs(now, ed);  
  88.                 l.pop_back();  
  89.             }  
  90.   
  91.             if(ok) return;  
  92.         }  
  93.         now -= dir[i];  
  94.     }  
  95.     if(all) vis[now.y][now.x] = false;  
  96. }  
  97.   
  98. int main()  
  99. {  
  100.     init();  
  101.     dfs(op, ed);  
  102.   
  103.     return 0;  
  104. }  

还有就是注释。我的注释习惯是从Doxygen中学的。主要讲几种:

Code:
  1. /** 这是在一个语句上方的注释 */  
  2. ///< 这是一个在语句后方的注释  
  3.   
  4. /** 
  5.  * @brief 函数名 
  6.  * 这是一个函数的注释 
  7.  * 
  8.  * @param 参数名 参数解释 
  9.  * @param 参数名... 参数解释... 
  10.  * @return 返回说明 
  11.  */  

上面的只是一部分的注释,因为我都养成习惯了,所以一下子想不起自己的一些风格了 = = 不好意思哈。

 

不难看出,我本身就是习惯性地遵循着这样一个规则。

不敢说我的风格习惯非常好,但我也自认为算是良好吧。上面的一些看法仅仅是抛砖引玉,欢迎有相同“洁癖”的童鞋们在下面拍砖。