Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用c+;计算多行注释之间的所有行数+;_C++_File_Parsing - Fatal编程技术网

C++ 使用c+;计算多行注释之间的所有行数+;

C++ 使用c+;计算多行注释之间的所有行数+;,c++,file,parsing,C++,File,Parsing,我正在制作一个程序,我可以用任何语言计算每个编程项目的注释,但我在计算多行注释时遇到了一个行计数问题,如下所示: /* this is a multiline comment */ 或者用Ruby的方式: =begin this is multiline comment =end 我想不出一种方法来计算多行注释中的每一行 这是我的一段代码: while(getline(file_f,line)) { lines_count++; if((line.empty(

我正在制作一个程序,我可以用任何语言计算每个编程项目的注释,但我在计算多行注释时遇到了一个行计数问题,如下所示:

/* this is 
  a multiline
  comment */
或者用Ruby的方式:

=begin 
this is multiline
comment
=end
我想不出一种方法来计算多行注释中的每一行 这是我的一段代码:

while(getline(file_f,line)) {

    lines_count++;

    if((line.empty()))
        blanklines_count++;

    if(((line.find("//")) != string::npos) || (line.find("/*") != string::npos)) 
        comments++;

我在其他帖子中没有找到关于我的getline实现问题的有用信息,有什么解决方案吗?

您需要设置一些
bool
标志变量,当您输入注释时,该变量将设置为
true
,当您留下注释时,该变量将设置为
false

// it's better to short-circuit the std::string::find
// (don't look for the start of the multi-line comment if you're already in one)
if(!inComment && line.find("//*") != std::string::npos)
    inComment = true; // don't increment here, just set the flag

if(inComment || line.find("//")) != std::string::npos)
    comments++;

if(inComment && line.find("*//") != std::string::npos)
    inComment = false;

您需要设置一些
bool
标志变量,当您输入注释时,该变量将设置为
true
,当您留下注释时,该变量将设置为
false

// it's better to short-circuit the std::string::find
// (don't look for the start of the multi-line comment if you're already in one)
if(!inComment && line.find("//*") != std::string::npos)
    inComment = true; // don't increment here, just set the flag

if(inComment || line.find("//")) != std::string::npos)
    comments++;

if(inComment && line.find("*//") != std::string::npos)
    inComment = false;

当您在多行注释中时,需要标记,例如:

bool in_multi_line_comment = false;

// ...

while//...

    // ...
    if (!in_multi_line_comment && line.find("/*") != string::npos)
        in_multi_line_comment = true;

    if (in_multi_line_comment || line.find("//")) != std::string::npos)
        comments++;

    if (in_multi_line_comment && line.find("*/") != string::npos)
        in_multi_line_comment = false;

当您在多行注释中时,需要标记,例如:

bool in_multi_line_comment = false;

// ...

while//...

    // ...
    if (!in_multi_line_comment && line.find("/*") != string::npos)
        in_multi_line_comment = true;

    if (in_multi_line_comment || line.find("//")) != std::string::npos)
        comments++;

    if (in_multi_line_comment && line.find("*/") != string::npos)
        in_multi_line_comment = false;

您需要一个布尔变量来告诉您是否在多行注释中。让我们称之为
is\u inside\u mcomment

  • 当您找到多行注释开始标记(例如,`/*')时

  • 找到多行注释结束标记时(例如“*/”)

每行

  • if在注释+++

根据您希望程序的复杂程度和准确程度,您必须考虑特殊情况:(例如,同一行上有多个多行注释,字符串内有标记等)

您需要一个布尔变量来告诉您是否在多行注释内。让我们称之为
is\u inside\u mcomment

  • 当您找到多行注释开始标记(例如,`/*')时

  • 找到多行注释结束标记时(例如“*/”)

每行

  • if在注释+++

根据您希望程序的复杂程度和准确程度,您必须考虑特殊情况:(例如,同一行上有多个多行注释,字符串内有标记等)

您需要一个真正有状态并能感知输入的解析器

<>强>例如您不想解析“代码>/*<代码>”作为C++中的块注释的开始,如果在这里出现:

std::cout << "Hello /*interesting*/ world!\n";
您将获得以下输出:

3 lines in cpp comments
2 lines in ruby comments
Couldn't parse java input

您需要一个实际上是有状态的、能够感知输入的解析器

<>强>例如您不想解析“代码>/*<代码>”作为C++中的块注释的开始,如果在这里出现:

std::cout << "Hello /*interesting*/ world!\n";
您将获得以下输出:

3 lines in cpp comments
2 lines in ruby comments
Couldn't parse java input

(注意,我之前的评论!)您打算如何处理
/
行?你不能轻易地用这些检查退出条件。对不起,我把它忘在条件里面了。也许你可以在
if()
中使用
incommment
,我很高兴。如果您想知道为什么在检查多行注释的结尾之前要进行递增,那么
/*comment*/
会产生
1
,如果一个多行注释在同一行结束,而另一个多行注释在同一行开始,那么这实际上是不正确的。(注意,我以前的注释!)您打算如何处理
/
行?你不能轻易地用这些检查退出条件。对不起,我把它忘在条件里面了。也许你可以在
if()
中使用
incommment
,我很高兴。如果您想知道为什么在检查多行注释的结尾之前要进行递增,那么
/*comment*/
会产生
1
,不
0
就像它在之后完成一样。@Ro Stel如果一个多行注释结束,然后另一个多行注释在同一行上开始,这实际上是不正确的。您需要能够跳过文本字符串中的注释序列吗?这需要很多额外的解析。您需要能够跳过文本字符串中的注释序列吗?这需要很多额外的解析。它可以正常工作,而且完全符合我的要求,但是编译时间很慢(我认为这没什么大问题),而且我还需要学习boost库,如何将它用于单独的项目文件,不过还是要谢谢你。我现在正在寻找修改这个代码在我的需要!它可以正常工作,而且完全符合我的要求,但是编译时间很慢(我认为这没什么大问题),而且我还需要学习boost库,如何将它用于单独的项目文件,不过还是要谢谢你。我现在正在寻找修改这个代码在我的需要!