java计数函数count_读取代码中函数的个数java

package com.in;

import org.apache.commons.lang.StringUtils;

import java.io.*;

public class readCode {

public static final String CODE_GBK = "GBK";//解决乱码的

public static final String CODE_UTF_8 = "UTF-8";

public static void main(String[] ager){

// 文件路径

String filePath = "C:\\Users\\THINK\\Desktop\\源1.cpp";

System.out.println(readCpp(filePath));

}

// 读取文件并解决乱码

public static String readCpp(String filePath){

FileInputStream fis = null;

String line = "";//存放文本

String regexBlankLine = "\\s*"; // 空行的正则

String regexAnnotation = "\\s*/{2}.*"; // 单行注释的正则

String regexAnnotationStart = "\\s*/\\x2A.*";// 多行注释开始标记

String regexAnnotationEnd = "\\s*\\x2A/.*";// 多行注释结束

String regexFunction = "(\\w+)\\s+[\\*,&]*\\s*(\\w+)\\s*\\(";//函数匹配 void demo(

int countBlankLine = 0;//空行

int countAnnotation = 0;//注释行

int countOther = 0;//代码行

int function = 0;//函数多少个

int i = 0;// 计数器 记录函数是否缺 { } 括号问题

int j = 0;// 计数器 记录函数是否缺 ( ) 括号问题

int row = 0;//记录出现的第几行的

int begin = 0;//记录函数的开始行数

int end = 0;//记录函数的结束行数

float average = 0;//平均数函数的平均行数

int count = 0;// 总数 每个函数的总行数之和

try {

fis = new FileInputStream(filePath);

InputStreamReader reader = new InputStreamReader(fis,CODE_GBK);

BufferedReader br = new BufferedReader(reader);

System.out.println("源程序:");

while ((line = br.readLine()) != null) {

System.out.println(line);

++row;// 用来记录行数的

// 多行注释统计

if (line.matches(regexAnnotationStart)) {

do {

++countAnnotation;//记录注释行数

line = br.readLine();

} while (!line.matches(regexAnnotationEnd));

}

// 空行统计

if (line.matches(regexBlankLine)) {

++countBlankLine;

} else if (line.matches(regexAnnotation)) {

// 单行注释统计

++countAnnotation;

} else {

// 代码行

++countOther;

}

// 括号匹配规则

if (line.indexOf("(")!=-1){

++j; // 对计数器的累加 (

// 函数个数统计

if(StringUtils.substring(line,0,line.indexOf("(")+1).matches(regexFunction)){

++function;// 函数累加

begin = row+1;//记录每个函数的开始行数

}

}

if (line.indexOf(")")!=-1){

--j;

}

if (line.indexOf("{")!=-1) {

++i; // 对计数器的累加

}

if (line.indexOf("}")!=-1){

--i;

if(i==0){

end = row;

count += (end-begin+1);

}

}

}

// 函数平均行数统计

if (i != 0 && j != 0){

System.out.println("缺括号阿");

}else {

if (begin != 0 && end != 0 && i==0 && j==0 && function != 0){

average = (float) count/function;

}

}

br.close();

reader.close();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return "**************************"+"\n"+

"信息统计如下:"+"\n"+

"**************************"+"\n"+

"总行数:" + row+"\n"+

"空行:" + countBlankLine+"\n"+

"注释行:" + countAnnotation+"\n"+

"代码行:" + countOther+"\n"+

"函数个数:" +function+"\n"+

"函数的平均行数:"+ average+"\n"+

"**************************";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值