前幾天看到一個(gè)面試題目,題目如下:
題目要求:請(qǐng)?jiān)O(shè)計(jì)一個(gè)命令行程序:使用多線程,統(tǒng)計(jì)C\C++程序語(yǔ)言程序源代碼行數(shù);源代碼是可以編譯通過(guò)的合法的代碼,統(tǒng)計(jì)其物理行數(shù)、其中的空行行數(shù)、其中含有的有效代碼行數(shù)、其中含有的注釋行數(shù)。
沖突處理?:在多行注釋內(nèi)的空行不算作注釋行;一行中同時(shí)有代碼和注釋,需要同時(shí)算作有效代碼行和注釋行。
提示?:注意換行符和續(xù)行符、注意字符和字符串內(nèi)部的處理和轉(zhuǎn)義符、不要使用正則表達(dá)式、要求自己獨(dú)立完成答題;如果代碼框架能更容易的擴(kuò)展到支持多種語(yǔ)言的源代碼行數(shù)統(tǒng)計(jì),將獲得更高的評(píng)價(jià)。
提交要求?:
- 包含程序所有的工程源代碼文件;
- 包含編譯出來(lái)的命令行程序,要求能夠直接運(yùn)行在windows、macosx或linux操作系統(tǒng)上(3種任選一個(gè)支持),包含必要的支撐文件和運(yùn)行環(huán)境說(shuō)明;
- 從命令行給程序輸入一個(gè)文件夾的路徑作為參數(shù);程序統(tǒng)計(jì)其中(包含子文件夾)源代碼文件的?行數(shù)信息;?(以參數(shù)方式傳遞路徑,如啟動(dòng)命令:counter.exe c:\src ,不要在程序執(zhí)行過(guò)程中手動(dòng)輸入任何內(nèi)容,程序完成要自動(dòng)退出)。
- 輸出結(jié)果到標(biāo)準(zhǔn)輸出,每個(gè)代碼文件輸出一行結(jié)果,格式為 file:src.cpp total:123 empty:123 effective:123 comment:123 依次為文件名file(帶工程目錄內(nèi)的相對(duì)路徑)、物理行數(shù)total、空行行數(shù)empty、有效代碼行數(shù)effective、注釋行數(shù)comment,每個(gè)數(shù)據(jù)以 key:value 的形式輸出,key為以上使用的單詞,請(qǐng)勿使用其他單詞命名,數(shù)據(jù)間以空格分隔。
有以下幾個(gè)點(diǎn)需要注意:
- 多行注釋:/* aaa* /,/* aaa/* aaa* /,多行注釋以第一個(gè)* /為結(jié)束。
- 字符串:在字符串內(nèi)遇到的 //,/* */ ,不能作為注釋行統(tǒng)計(jì),同時(shí),字符串內(nèi)遇到的 “ 也不能直接判定為字符串結(jié)束,應(yīng)當(dāng)判斷前一個(gè)字符是否 \。
題目沒(méi)有要求如何使用多線程,因此可以用C++的庫(kù)函數(shù)std::async來(lái)實(shí)現(xiàn)多線程。
以下是C++實(shí)現(xiàn)的全部代碼:
// 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; // 多行注釋 開(kāi)
status.bComment = true; // 單行注釋
}
else if (buf[0] == '*' && buf[1] == '/')
{
status.bCommentM == false; // 多行注釋 關(guān)
}
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;
}
執(zhí)行結(jié)果:

程序執(zhí)行結(jié)果