如何使用C#程序计算C程序文件中的函数数量?我创建了一个简单的C#类来计算C文件中的LOC。
private bool IsInMultipleComment = false;
private int getNumberOFFuncions(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
if (IsFunction(tempStr))
count++;
tempStr = rdr.ReadLine();
}
return count;
}
支持方法:
private bool IsFunction(string line)
{
if (line.Contains("//"))
return false;
if (line.Contains("/*"))
IsInMultipleComment = true;
if (line.Contains("*/"))
IsInMultipleComment = false;
if (!IsInMultipleComment)
{
if (line.Contains("void") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
{
if (!line.Contains(";"))
{
return true;
}
}
}
return false;
}
这是我计算变量的方式:
private int getNumberOfVariables(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
count += getVariblesOfLine(tempStr);
tempStr = rdr.ReadLine();
}
return count;
}
支持方法:
private int getVariblesOfLine(string line)
{
line = line.Trim(); // trim the lines
if (line.Contains("#")) // remove preprocessive declarations
return 0;
if (line.Contains("//"))
return 0;
if (line.Contains("/*"))
IsInMultipleComment = true;
if (line.Contains("*/"))
IsInMultipleComment = false;
if (!IsInMultipleComment)
{
if (line.Contains("unsigned") || line.Contains("signed") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
{
if (!line.Contains("(")) // remove if this is function
{
Console.WriteLine(line);
if (line.Contains(",")) // count at multiple declarations
{
int y = line.Count(f => f == ',');
return y + 1;
}
return 1;
}
}
}
return 0;
}
答案 0 :(得分:0)
了解正则表达式。字符串模式是函数声明的样子。如果你以普遍接受的.net方式使用声明函数,你就不会捕获函数的每个可能的扭曲,你可以获得大部分函数。 Expresso正在学习良好的学习工具,帮助您获得正则表达式的模式。
这是一个识别功能的模式。它看起来很疯狂,但事实并非如此。 Expresso将为您解码。它没有完全发展,因为它不会捕捉私人功能,你不会在它面前把这个词放在私有状态,而且它不会受到内部保护。它可能还有很多东西不会被捕获。
Regex regex = new Regex("\s*(private|public|internal|protected)\s*\w+\s+([a-zA-Z_0-9.]+)\s*\(.*\)",RegexOptions.Compiled)
if (regex.IsMatch(lineOfCode)
{
//then it's a function
}
另一方面,请勿继续打开并重新阅读该文件。打开它一次,然后通过它就可以了。
我已经获得了一些代码(在javascript中)来进行行计数等等,在Csharp文件中你可以提取一些正则表达式模式。请注意正则表达式如何保存在对象中(.net中的字典)在javascript中,/ pattern /与.net" pattern"
相同module.exports = ( function() {
var classifiers = [] ;
classifiers.push(
{
ctype: "using",
regex: /^(\s*using\s*)([a-zA-Z_0-9.]+)/,
extractMethod: function(lineInfo) {
lineInfo.extractValue = lineInfo.line.split(this.regex)[2] ;
}
},
{
ctype: "namespace",
regex: /^(\s*namespace\s*)([a-zA-Z_0-9.]+)/,
extractMethod: function(lineInfo) {
lineInfo.extractValue = lineInfo.line.split(this.regex)[2] ;
}
},
{
ctype: "comment",
regex: /^\s*\/\/[/ A-Za-z,*]*/,
extractMethod: function(lineInfo) {
lineInfo.extractValue = null ;
}
},
{
ctype: "n/a",
regex: /^\s*$|^\s*[;{}]+?\s*$/,
extractMethod: function(lineInfo) {
lineInfo.extractValue = null ;
}
}
);
function classifyLine(line, lineNo) {
var lineInfo = {} ;
lineInfo.line = line ;
lineInfo.lineNo = lineNo;
for (var index = 0; index < classifiers.length; index++) {
var classifier = classifiers[index];
if (classifier.regex.test(line)) {
lineInfo.ctype = classifier.ctype;
lineInfo.line = line ;
classifier.extractMethod(lineInfo) ;
break ;
}
}
if (lineInfo.ctype == undefined){
lineInfo.ctype = "code" ;
}
return lineInfo ;
}
return {
classifyLine : classifyLine
};
} )();
答案 1 :(得分:-1)
我编写了这个类来计算变量,函数和loc。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LineOfCounter
{
class FileDialtion
{
private string FilePath; // file path
private string fileName; //file name
private long fileSize; // file size
private string extension; // extension
private bool readOnly; // is file raad only
private string creationTime;// file cration time
private string accessTime; // last access time
private long fileLOC; // line of code insert in the file
private int numOfFuncs; // number of functions in thefile
private int numOfVariables; // number of variables in the code
private int numOfLV; // number of local varibles
private int numOfGV; // number of globle variables
private int numOfComments; // number of Comments
private int NumOfPPR = 0;// number of pre proccesssive derectories
private bool IsInMultipleComment = false; // when starts multiple comment ignore Count
#region getters
public int getNumberOfPPR
{
get { return NumOfPPR; }
}
public int getNumOfVariables
{
get { return numOfVariables; }
}
public int getNumOfGlobleVariables
{
get { return numOfGV; }
}
public int getNumOfLocalVariables
{
get { return numOfLV; }
}
public int getNumberOfFunctions
{
get { return numOfFuncs; }
}
public int getNumberOfComment
{
get { return numOfComments; }
}
public string getFilePath
{
get
{
return FilePath;
}
}
public string getFileName
{
get
{
return fileName;
}
}
public string getFileSize
{
get
{
return fileSize.ToString();
}
}
public string getFileExtension
{
get
{
return extension;
}
}
public string getFileCreationtime
{
get
{
return creationTime;
}
}
public string getFileAccessTime
{
get
{
return accessTime;
}
}
public long getFileLOC
{
get
{
return fileLOC;
}
}
#endregion
// class Constructor
public FileDialtion(string filename) {
try
{
FilePath = filename;
FileInfo fi = new FileInfo(getFilePath);
fileName = fi.Name;
fileSize = fi.Length;
extension = fi.Extension;
readOnly = fi.IsReadOnly;
creationTime = fi.CreationTime.ToString();
accessTime = fi.LastAccessTime.ToString();
fileLOC = getLOC(fi); // read loc
numOfComments = getNumberOfComments(fi); // get comments
NumOfPPR = getNumberOfPPRs(fi); // get pre processive directories
numOfFuncs=getNumberOFFuncions(fi); // read functions
numOfVariables = getNumberOfVariables(fi); // get number of variables
numOfGV = getNumberOfGlobleVaariables(fi); // getnumber of LocalVariables
numOfLV = numOfVariables - numOfGV;
}
catch (Exception es)
{
Console.WriteLine(es.Message);
}
}
#region main get Functuens
private int getLOC(FileInfo fs) {
StreamReader rdr;
int count=0;
rdr = fs.OpenText();
while (rdr.ReadLine() != null)
{
count++;
}
return count;
}
private int getNumberOFFuncions(FileInfo fs) {
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
if(IsFunction(tempStr))
count++;
tempStr = rdr.ReadLine();
}
return count;
}
private int getNumberOfVariables(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
count += getVariblesOfLine(tempStr);
tempStr = rdr.ReadLine();
}
return count;
}
private int getNumberOfComments(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
if (IsComment(tempStr))
count++;
tempStr = rdr.ReadLine();
}
return count;
}
private int getNumberOfPPRs(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
if (isPPR(tempStr))
count++;
tempStr = rdr.ReadLine();
}
return count;
}
private int getNumberOfGlobleVaariables(FileInfo fs)
{
StreamReader rdr;
int count = 0;
string tempStr;
// initialize
rdr = fs.OpenText();
tempStr = rdr.ReadLine();
while (true)
{
if (tempStr == null)
break;
count += getVariblesOfLine(tempStr); // count variables
if (tempStr.Contains("main")) // main function founds
break;
tempStr = rdr.ReadLine();
}
return count;
}
#endregion
#region Supportive Functions
private bool IsFunction(string line)
{
if (line.Contains("//"))
return false;
if (line.Contains("/*"))
IsInMultipleComment = true;
if (line.Contains("*/"))
IsInMultipleComment = false ;
if (!IsInMultipleComment)
{
if (line.Contains("void") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
{
if (!line.Contains(";"))
{
return true;
}
}
}
return false;
}
private bool isPPR(string line)
{
if (line.Contains("//"))
return false;
if (line.Contains("/*"))
IsInMultipleComment = true;
if (line.Contains("*/"))
IsInMultipleComment = false;
if (!IsInMultipleComment)
{
if (line.Contains("#"))
{
return true;
}
}
return false;
}
private bool IsComment(string sp)
{
if (sp.Contains("//"))
{
return true;
}
if (sp.Contains("/*"))
{
return true;
}
return false;
}
private int getVariblesOfLine(string line)
{
line = line.Trim(); // trim the lines
if (line.Contains("#")) // remove preprocessive declarations
return 0;
if (line.Contains("//"))
return 0;
if (line.Contains("/*"))
IsInMultipleComment = true;
if (line.Contains("*/"))
IsInMultipleComment = false;
if (!IsInMultipleComment)
{
if (line.Contains("unsigned") || line.Contains("signed") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
{
if (!line.Contains("(")) // remove if this is function
{
Console.WriteLine(line);
if (line.Contains(",")) // count at multiple declarations
{
int y = line.Count(f => f == ',');
return y + 1;
}
return 1;
}
}
}
return 0;
}
#endregion
}
}