C ++ code comments and function count statistics

Source: Internet
Author: User

C ++ code comments and function count statistics

Problem Source: a small project in the summer of 14 years encountered such a problem, requiring statistics on the number of comments in the C ++ code, the number of valid code lines, and the number of comments in the code, and the number of functions.

The following is a brief explanation of the problem,

1) Number of comment lines: refers to the lines with comments, including public lines with code and comments (for example, 3, 4, 15, 22 ...)

2) valid code line: refers to the line with code, including the public lines with code and comments (such as:, 25 ....)

3) code comments public line: refers to the lines with code and comments (for example, 4, 15 ...)

4) number of functions: Needless to say, it is clear.

The following code shows the Annotations:

 1 #include 
 
   2  3 //follow is a common line 4 void swap(char *p/* = NULL */) 5 { 6     printf("this is a function /*is not comments*/"); 7 } 8  9 int main(void)10 {11     int a = 10;12     int b;13     char *p = NULL;14 15     swap(p);  //common line16     if (10 == a)17     {18         printf("is not function;//is not comments");19     }20     //pure comments/*no use*/21 22     /*a = 5;23     printf("not use\n");*/24 25     b = 3;/*i'm a comment*/ a = 5; /*comments line26     pure //comments line;"*/......"look there*/27     printf("test \" escape char");28     //and the follow,maybe affect the count of function29     if (*p == '{')30     {31         printf("the '{' is a question\n");32     }33     //34 35     return 0;36 }
 

The above code snippet shows the possible comments. Next, let's analyze the location of the comments:

I. "//" Double slash comments

1) The row header may appear, for example, row 3rd;

2) The row may end, for example, row 15th;

Note: There are 18th rows and 26th rows. In row 3, the comment is invalid; in Row 3, the comment is invalid; // In/**/the comment is invalid.

Ii. Slash star comments

1) it may appear in function parameters, for example, row 4th;

2) A paragraph may be commented out, for example, the 22,23 line;

3) it may also appear in the middle of the Code, such as 25 or 26 lines of complex comments;

Note: There are 20th rows and 26th rows. Row 3,/**/In // comment, invalid. Row 3, with one */located in "", */invalid.

Now, the location analysis is complete. The following describes how to design an algorithm:

Iii. Specific algorithm design ideas

1) Solve the Problem of File Reading

Here we use the c ++ file to read data. Each time a row is read, The read result is put into the string variable, so that the tail of the string variable is automatically '\ 0 '. We are used to judge the end of a row.

2) double quotation marks

Because the content in double quotation marks is invalid, We can filter the content in double quotation marks directly. (For specific implementation, see the code)

3) single quotation marks

As shown in the preceding code, the content in single quotes (for example, if (ch = '{') may affect the number of functions, so we need to filter single quotes.

4) Empty rows

This is the simplest. If the string is null, It is a blank line. Including (\ t \ n \ r'' and so on, invalid characters, are considered empty rows ).

5) "//" comment

This is relatively simple. When you encounter a //, you can perform statistics. At the same time, you do not need to traverse the row. Then, you can directly break and read the next row.

6) "/**/" comment

The code mark and comment mark are used here, and the number of rows is counted only when the row is traversed to the end of the row. This prevents the row from being counted multiple times. (For specific implementation, see the code)

7) number of functions

First, let's talk about the statistical idea. When we count a function, we only mark it with braces. It is represented by a stack. When it comes to left brackets, it goes to the stack. When it comes to right brackets, a left bracket is displayed. Knowing that the stack is empty, the number of functions is + 1,

In this way, only the outer-layer pair {} is retained, and all the parentheses in the middle layer are offset. We thought about it again and simplified it. Instead of using a stack, we directly use a variable to represent the number of parentheses. in the case of left parentheses, ++ top, and right parentheses -- top, until top = 0, it is equivalent to the empty stack, the number of functions + 1.

In fact, there are still problems with the number of functions in the following code:

Example: A function is counted as a class, struct, enumeration body, and global array. That is to say, the Code with braces ({}) in the external body of the function will be recognized as a function.

The following is the implementation code:

1 # include
 
  
2 # include
  
   
3 # include
   
    
4 using namespace std; 5 6 int main (void) 7 {8 string line = ""; 9 ifstream ifs; 10 ifs. open ("Test. cpp "); 11 if (! Ifs) 12 {13 cout <"file opening failed" <
    
     
0 & line [I-1]! = '\') | (I = 0) 48 {49 + I; 50 bSyh = 1; 51 continue; 52} 53 // "Double quotation marks are being shielded .... "54 if (bSyh) 55 {56 // "\" end "57 if (line [I] = '\"' & (I> 0 & line [I-1]! = '\') | (I = 0) 58 {59 bSyh = 0; 60} 61 else if (line [I] = '\ 0 ') // 62 {63 if (bZs) 64 + + nZs; 65 if (bCode) 66 + + nDm; 67 if (bZs & bCode) 68 + nGg; 69 break; 70} 71 + + I; 72 continue; 73} 74 // single quotation marks (avoid '{', '}') and non-escape characters \', skip 3 consecutive (second location) 75 if (line [I] = '\ ''& (I> 0 & line [I-1]! = '\') | (I = 0) 76 {77 I + = 3; 78 continue; 79} 80 // "// comment row" 81 if (! BXgx & line [I] = '/' & line [I + 1] = '/') 82 {83 if (bCode) // "there is code before, hybrid comment line "84 {85 + nZs; // comment 86 + nDm; // code 87 + nGg; // public 88} 89 else // pure comment row 90 {91 + + nZs; 92} 93 break; // jump out of the current row (that is, the internal while LOOP ), "//" after the code is not judged 94} 95 // "/* Comment start" 96 if (! BXgx & line [I] = '/' & line [I + 1] = '*') 97 {98 I + = 2; // skip/* symbol 99 bXgx = 1; // mark "/*" Start 100 bZs = 1; // "Notice comments" 101 continue; 102} 103 // "multi-line comments in progress .... "104 if (bXgx) 105 {106 // "*/end of comment" 107 if (line [I] = '*' & line [I + 1] = '/') 108 {109 ++ I; // "skip */" note that there is a ++ I; 110 bXgx = 0; 111} 112 else if (line [I] = '\ 0') // code before the end of the row 113 {114 if (bCode) // comments, that is, "hybrid row" 115 {116 + nDm; 117 + nZs; 118 + + nGg; 119} 120 else121 {122 + nZs; // "Pure annotation" 123} 124 break; 125} 126 + + I; 127 continue; 128} 129 if (line [I] = '\ 0 ') 130 {131 if (bZs) 132 + + nZs; 133 if (bCode) 134 + + nDm; 135 if (bZs & bCode) 136 + + nGg; 137 break; 138} 139 // "The following are all valid Code regions" 140 // "function count statistical regions: Start" 141 if (line [I] = '{') // record function 142 {143 + bHs; 144} 145 else if (line [I] = '}') // encounter a function right brace 146 {147 if (bHs = 0) // "discover a function" 148 + + AIS; 149 -- bHs; 150} 151 // "function statistics: End" 152 + I; 153 bCode = 1; // can be executed here, this row contains code 154} 155} 156 157 cout <"Note:" <
     
      

Address: http://www.cnblogs.com/nchar/p/3915889.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.