美文网首页
解题——统计C++源码行数

解题——统计C++源码行数

作者: 落单的毛毛虫 | 来源:发表于2018-03-04 03:41 被阅读0次

前几天看到一个面试题目,题目如下:

题目要求:请设计一个命令行程序:使用多线程,统计C\C++程序语言程序源代码行数;源代码是可以编译通过的合法的代码,统计其物理行数、其中的空行行数、其中含有的有效代码行数、其中含有的注释行数。

冲突处理​:在多行注释内的空行不算作注释行;一行中同时有代码和注释,需要同时算作有效代码行和注释行。

提示​:注意换行符和续行符、注意字符和字符串内部的处理和转义符、不要使用正则表达式、要求自己独立完成答题;如果代码框架能更容易的扩展到支持多种语言的源代码行数统计,将获得更高的评价。

提交要求​:

  • 包含程序所有的工程源代码文件;
  • 包含编译出来的命令行程序,要求能够直接运行在windows、macosx或linux操作系统上(3种任选一个支持),包含必要的支撑文件和运行环境说明;
  • 从命令行给程序输入一个文件夹的路径作为参数;程序统计其中(包含子文件夹)源代码文件的​行数信息;​(以参数方式传递路径,如启动命令:counter.exe c:\src ,不要在程序执行过程中手动输入任何内容,程序完成要自动退出)。
  • 输出结果到标准输出,每个代码文件输出一行结果,格式为 file:src.cpp total:123 empty:123 effective:123 comment:123 依次为文件名file(带工程目录内的相对路径)、物理行数total、空行行数empty、有效代码行数effective、注释行数comment,每个数据以 key:value 的形式输出,key为以上使用的单词,请勿使用其他单词命名,数据间以空格分隔。

有以下几个点需要注意:

  • 多行注释:/* aaa* /,/* aaa/* aaa* /,多行注释以第一个* /为结束。
  • 字符串:在字符串内遇到的 //,/* */ ,不能作为注释行统计,同时,字符串内遇到的 “ 也不能直接判定为字符串结束,应当判断前一个字符是否 \。

题目没有要求如何使用多线程,因此可以用C++的库函数std::async来实现多线程。

以下是C++实现的全部代码:

//  main.cpp
#include <tuple>
#include <string>
#include <future>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <filesystem>

namespace filesys = std::experimental::filesystem;

struct Result {
    std::int32_t total;         //  总行
    std::int32_t empty;         //  空行
    std::int32_t comment;       //  注释行
    std::int32_t effective;     //  有效行
    Result(): total(0), empty(0), comment(0), effective(0)
    { }
};

struct Status {
    bool bEffective;
    bool bCommentM;
    bool bComment;
    bool bEmpty;
    Status(): bEffective(false), bCommentM(false), bComment(false), bEmpty(true)
    { }
};

using Print_t = std::tuple<std::string, Result>;

inline bool IsPrintChar(char ch)
{
    return ch > 32;
}

inline const char * SkipString(const char * buf)
{
    for (auto trans = *buf == '\\';
         *buf != '\"' && !trans;
         trans = *buf == '\\', ++buf);
    return buf;
}

inline size_t CheckFileLength(std::ifstream & ifile)
{
    ifile.seekg(0, std::ios::end);
    auto length = ifile.tellg();
    ifile.seekg(0, std::ios::beg);
    return (size_t)length;
}

inline void EndOfLine(Status & status, Result & result)
{
    ++result.total;
    status.bEmpty && !status.bComment && ++result.empty;
    status.bComment && ++result.comment;
    status.bEffective && ++result.effective;
    //  reset status
    if (0 == status.bCommentM)
    {
        status.bComment = false;
    }
    status.bEffective = false;
    status.bEmpty = true;
}

void Parse(const char * buf, Result & result)
{
    Status status;
    for (; *buf != '\0'; ++buf)
    {
        if (buf[0] == '\n')         //  新行
        {
            EndOfLine(status, result);
        }
        else if (buf[0] == '/' && buf[1] == '/')
        {
            status.bComment = true; //  单行注释
        }
        else if (buf[0] == '/' && buf[1] == '*')
        {
            status.bCommentM = true;    //  多行注释 开
            status.bComment = true;     //  单行注释
        }
        else if (buf[0] == '*' && buf[1] == '/')
        {
            status.bCommentM == false;      //  多行注释 关
        }

        if (IsPrintChar(*buf))
        {
            if (!status.bComment)
            {
                status.bEffective = true;   //  有效行
                if (buf[0] == '\\' && buf[1] == '\"' && ++buf);
                else if (buf[0] == '\"') buf = SkipString(++buf);
            }
            status.bEmpty = false;      //  非空
        }
    }
    EndOfLine(status, result);
}

inline Result Parse(std::ifstream & ifile)
{
    Result result;
    std::string bytes;
    ifile >> std::noskipws;
    std::copy(
        std::istream_iterator<char>(ifile),
        std::istream_iterator<char>(),
        std::back_inserter(bytes));
    Parse(bytes.c_str(), result);
    return result;
}

inline Print_t ParseResult(const std::string & fname)
{
    std::ifstream ifile(fname);
    return { fname, Parse(ifile) };
}

inline std::future<Print_t> OnParseResult(const filesys::directory_entry & entry)
{
    return std::async(std::launch::async, &ParseResult, entry.path().string());
}

inline void OnPrintResult(std::future<Print_t> & print)
{
    auto value = print.get();
    std::cout
        << "src: " << std::get<0>(value) << " "
        << "empty: " << std::get<1>(value).empty << " "
        << "total: " << std::get<1>(value).total << " "
        << "comment: " << std::get<1>(value).comment << " "
        << "effective: " << std::get<1>(value).effective << std::endl;
}

int main()
{
    std::string path;
    std::getline(std::cin, path);
    std::vector<std::future<Print_t>> prints;
    std::transform(filesys::directory_iterator(path),
                   filesys::directory_iterator(),
                   std::back_inserter(prints),
                   OnParseResult);
    std::for_each(prints.begin(), prints.end(), OnPrintResult);
    std::cin.get();
    return 0;
}

执行结果:

程序执行结果

相关文章

网友评论

      本文标题:解题——统计C++源码行数

      本文链接:https://www.haomeiwen.com/subject/mmfffftx.html