【问题标题】:Counting chars without comments计算没有评论的字符
【发布时间】:2018-06-14 02:05:21
【问题描述】:

我正在尝试制作一个可以计算字符串输入中所有字符的函数,但不计算任何 cmets(// 或 /*)

到目前为止,它与 // 之后的行完美配合。我将我的字符串按每个新行拆分,如果它不包含//

,我只计算行字符

这是目前为止的代码:

function findComments() {

    var string = document.getElementById("input").value;
    var splittedString = string.split("\n");

    var count = 0;

    for (var i = 0; i < splittedString.length; i++) {
        while (countStars) {
            if(splittedString[i].indexOf("*/") > -1) {
                countStars = false;
            }
            continue;
        }

        if(splittedString[i].indexOf("/*") > -1) {
            var countStars = true;
        }

        if(splittedString[i].indexOf("//") === -1) {

            var chars = splittedString[i].split("");
            for (var j = 0; j < chars.length; j++) {
                count++;
            }
        }

    }
    console.log(count);
}

正如我所提到的,它应该继续循环,直到找到注释的结尾 ( */ )。到目前为止,这不起作用

非常感谢任何帮助! :)

【问题讨论】:

  • 您可以忽略 // 之后的所有内容,但您需要为 /* */ 处理多行 - 您可以使用 &lt;&gt; sn-p 编辑器创建 minimal reproducible example 吗?
  • 是的,很难处理多行,因为我需要以某种方式继续循环,直到找到 */
  • 在分割成行之前删除它们。 blog.ostermiller.org/find-comment
  • 我想在没有正则表达式的情况下这样做,有没有办法在没有正则表达式的情况下删除它?

标签: javascript if-statement while-loop comments


【解决方案1】:
 var inComment = false, count = 0;
 for(const line of splittedStrings){
  var end = -1;
  if(inComment){
   end = line.indexOf("*/");
   if(end === -1){
     count += line.length;
     continue;
   } else {
     count += end;
     inComment = false;
   }
  }
  const single = line.indexOf("//", end);
  const multi = line.indexOf("/*", end);
  if(single + multi === -2) continue;
  if(multi !== -1 && multi < single){
    //got a multiline comment
    inComment = true;
    count += line.length - (multi + 2);
 } else{
   //got a single line comment
   count += line.length - (single + 2);
 }
}

如果您在多行注释中,只需保留一个标志

【讨论】:

  • 不错的解决方案!一个问题,当我在 /* .. */
  • 另外,你能详细说明一下 for 循环(splittedStrings 的 const 行)的作用吗?
  • @gustav 实际上它应该可以工作。 for 循环只是 for(var i = 0... 循环的美丽形式
【解决方案2】:

您应该在 for 循环之外声明 countStars,因为您甚至在声明之前就检查了该变量
您应该将 continue 语句从 while 循环中取出。 Continue 将再次命中 while 语句。

for(var i = 0; i<10; ++i)
{
    var j = 10;
    var k = true;
    while(j > 0 && k)
    {
       j--;
       if(j == 8)
       {k == false;}
    }
    if(!k)
       continue;
}

【讨论】:

  • 我无法将 continue 置于 while 循环之外,它必须从内部激活
  • 在while内添加一个布尔变量。将其设置为 true 。然后使用该变量调用 continue 语句。
  • 你有例子吗? :' D
  • 在答案中添加了一个示例。
  • 我不确定我是否理解,在这个例子中你在哪里使用 countStars?
【解决方案3】:

这可以进一步改进,但您可以将原始文本拆分为“/*”并从计数中减去块 cmets 的长度。这意味着不必要地计算块 cmets,但它可以完成工作。

像这样拆分文本,您知道数组中的每个第二项都是您的块注释。

var blockComments = string.split('/*');

function blockCommentsLength(array){
   length = 0;
   for (var i = 0; i < array.length; i = i +2){
      length += array[i].length
   }

   return length;
}

...

console.log(count - blockCommentsLength(blockComments));

【讨论】:

  • 这其实不是一个坏主意!非常感谢:)
  • 如何识别 cmets 的 */ 结尾并重新开始计数?
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2023-01-14
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多