读取代码中函数的个数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"+

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

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,889评论 0 38
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,048评论 0 2
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,409评论 0 4
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,402评论 0 2
  • Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和...
    Java小辰阅读 995评论 0 5