• 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库用来解析目录和提取文件后缀,如果路径是个目录就提取子目录项逐个分析,如果子目录项是目录就递归调用本身继续解析目录,如果是代码文件就开始计数行数

    1. //
    2. // Created by YEZI on 2024/5/20.
    3. //
    4. #ifndef CODECOUNTER_H
    5. #define CODECOUNTER_H
    6. #include<vector>
    7. #include<string>
    8. #include<filesystem>
    9. #include <fstream>
    10. #include <iostream>
    11. class CodeCounter {
    12. int lines = 0;
    13. // 检查是否是代码文件
    14. static bool isCodeFile(const std::filesystem::path &path) {
    15. // 常见代码文件后缀
    16. static const std::vector<std::string> extensions = {
    17. ".cpp", ".h", ".java", ".py", ".cs", ".js", ".go", ".c", ".cc", ".hh"
    18. };
    19. // 检查路径是否存在
    20. if (std::filesystem::exists(path) == false) {
    21. std::cerr << "There is no file " << path << std::endl;
    22. return false;
    23. }
    24. // 检查是否是文件
    25. if (is_regular_file(path) == false) {
    26. std::cerr << path << " is no a file." << std::endl;
    27. return false;
    28. }
    29. std::string extension = path.extension().string();
    30. for (const auto &e: extensions) {
    31. if (e == extension) {
    32. return true;
    33. }
    34. }
    35. return false;
    36. }
    37. void countCodeFile(const std::filesystem::path &filePath) {
    38. // 检查是否是代码文件
    39. if (isCodeFile(filePath) == false)
    40. return;
    41. std::ifstream file(filePath);
    42. // 检查文件是否可以打开
    43. if (file.is_open() == false) {
    44. std::cerr << "Error opening file: " << filePath << std::endl;
    45. return;
    46. }
    47. std::string trash;
    48. int count=0;
    49. while (std::getline(file, trash)) {
    50. ++count;
    51. }
    52. lines+=count;
    53. std::cout<<filePath<<" Lines: "<<count<<std::endl;
    54. }
    55. void countDirectory(const std::filesystem::path &path) {
    56. // 检查是否是目录
    57. if (is_directory(path) == false)
    58. return;
    59. for (const auto &entry: std::filesystem::directory_iterator(path)) {
    60. if (entry.is_directory())
    61. countDirectory(entry.path());
    62. else
    63. countCodeFile(entry.path());
    64. }
    65. }
    66. public:
    67. void countThis(const std::filesystem::path &path) {
    68. if (is_directory(path))
    69. countDirectory(path);
    70. else
    71. countCodeFile(path);
    72. std::cout << "Code Lines: " << lines;
    73. }
    74. };
    75. #endif //CODECOUNTER_H

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

    1. #include <iostream>
    2. #include"CodeCounter.h"
    3. int main(int argc, char *argv[]) {
    4. CodeCounter code_counter;
    5. std::string path;
    6. if (argc == 2)
    7. path.assign(argv[1]);
    8. else
    9. std::getline(std::cin, path);
    10. code_counter.countThis(path);
    11. return 0;
    12. }
  • 相关阅读:
    SQL金融行业项目:你每月消费多少?(累计额,链表,case when)
    【PTA-训练day3】L2-014 列车调度 + L1-009 N个数求和
    长假回归,回顾一下所有的电商API接口
    多态(基本介绍、快速入门、注意事项以及细节、动态绑定机制)
    Python - 文件操作(1)
    海外原生广告平台——Taboola投放指南
    【IC刷卡数据专题】IC刷卡数据有哪些应用?
    在Spring Boot项目中使用全局异常处理
    程序分区:全局区、常量区、栈区、堆区、代码区
    NETPLIER : 一款基于概率的网络协议逆向工具(一)理论
  • 原文地址:https://blog.csdn.net/weixin_62264287/article/details/139204587