C++实现的代码行数统计器

代码在GitHubMaolinYe/CodeCounter: C++20实现的代码统计器,代码量小于100行,可以统计目录下所有代码文件的行数 (github.com)

前段时间到处面试找实习,有技术负责人的负责人问我C++写过多少行,5万还是10万,用来评估熟练度,有点难顶,于是写个代码统计器吧,输入文件夹目录或者代码文件,可以统计所有代码的行数

可以直接编译代码运行程序,在控制台输入目录的路径按下回车即可,例如输入

复制代码
C:\Users\Yezi\Desktop\C++\CodeCounter

也可以在终端命令行直接运行编译好的程序,带上参数运行,例如输入

复制代码
.\CodeCounter.exe C:\Users\Yezi\Desktop\C++\CodeCounter

思路比较简单,主要是用到了C++17的filesystem库用来解析目录和提取文件后缀,如果路径是个目录就提取子目录项逐个分析,如果子目录项是目录就递归调用本身继续解析目录,如果是代码文件就开始计数行数

复制代码
//
// Created by YEZI on 2024/5/20.
//

#ifndef CODECOUNTER_H
#define CODECOUNTER_H
#include<vector>
#include<string>
#include<filesystem>
#include <fstream>
#include <iostream>

class CodeCounter {
    int lines = 0;
    // 检查是否是代码文件
    static bool isCodeFile(const std::filesystem::path &path) {
        // 常见代码文件后缀
        static const std::vector<std::string> extensions = {
            ".cpp", ".h", ".java", ".py", ".cs", ".js", ".go", ".c", ".cc", ".hh"
        };
        // 检查路径是否存在
        if (std::filesystem::exists(path) == false) {
            std::cerr << "There is no file " << path << std::endl;
            return false;
        }
        // 检查是否是文件
        if (is_regular_file(path) == false) {
            std::cerr << path << " is no a file." << std::endl;
            return false;
        }
        std::string extension = path.extension().string();
        for (const auto &e: extensions) {
            if (e == extension) {
                return true;
            }
        }
        return false;
    }

    void countCodeFile(const std::filesystem::path &filePath) {
        // 检查是否是代码文件
        if (isCodeFile(filePath) == false)
            return;
        std::ifstream file(filePath);
        // 检查文件是否可以打开
        if (file.is_open() == false) {
            std::cerr << "Error opening file: " << filePath << std::endl;
            return;
        }
        std::string trash;
        int count=0;
        while (std::getline(file, trash)) {
            ++count;
        }
        lines+=count;
        std::cout<<filePath<<" Lines: "<<count<<std::endl;
    }

    void countDirectory(const std::filesystem::path &path) {
        // 检查是否是目录
        if (is_directory(path) == false)
            return;
        for (const auto &entry: std::filesystem::directory_iterator(path)) {
            if (entry.is_directory())
                countDirectory(entry.path());
            else
                countCodeFile(entry.path());
        }
    }

public:
    void countThis(const std::filesystem::path &path) {
        if (is_directory(path))
            countDirectory(path);
        else
            countCodeFile(path);
        std::cout << "Code Lines: " << lines;
    }
};
#endif //CODECOUNTER_H

从命令行参数读取目录或者从控制台输入读取目录

复制代码
#include <iostream>
#include"CodeCounter.h"

int main(int argc, char *argv[]) {
    CodeCounter code_counter;
    std::string path;
    if (argc == 2)
        path.assign(argv[1]);
    else
        std::getline(std::cin, path);
    code_counter.countThis(path);
    return 0;
}
相关推荐
草莓熊Lotso22 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM28 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
海的诗篇_38 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
feiyangqingyun40 分钟前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181
CV点灯大师43 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
cccc来财1 小时前
Go中的协程并发和并发panic处理
开发语言·后端·golang
狐凄1 小时前
Python实例题:Python计算线性代数
开发语言·python·线性代数
惊鸿一博2 小时前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
Bruce_Liuxiaowei2 小时前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
成工小白2 小时前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针