计算代码行数的 C 程序

发布于 2024-10-21 07:51:01 字数 889 浏览 10 评论 0原文

我正在用 C 编写一个简单的 LOC 计数器来计算我的 C 源文件中有多少行代码。它应该从命令行运行,将目标文件重定向为输入,并查看打印到标准输出的总行数。例如:

  counter.exe < counter.c
  15

到目前为止,我使用的唯一规则是:

  1. 仅计算包含 3 个以上字符的行(没有空白行或仅包含右大括号和分号等的行)。

  2. 不要将空格算作字符。

这是我的计划:

#include <stdio.h>

int main() {

    int input;
    int linecount = 0;
    int charcount = 0;

    while ((input = getchar()) != EOF) {

        if (input == ' ') {
        }
        else if (input == '\n') {
            if (charcount > 3) {
               linecount++;
            }
            charcount = 0;
        }
        else {
            charcount++;
        }
    }

    printf("%d\n", linecount);

    return 0;
}

我的问题是,您能否对规则进行一些改进,使其成为更有效的措施?人们经常将注释视为有效的代码行吗?空格或空行怎么样?

我不想就 LOC 计数的有效性展开争论,这是我在几次采访中被问到的问题,我认为从一般意义上讲,我自己的项目有多少行代码是值得知道的。谢谢!

I'm writing a simple LOC counter in C to count how many lines of code are in my C source files. It's meant to be run from the command line, redirecting the target file as input and seeing a total line count printed to standard out. For example:

  counter.exe < counter.c
  15

So far the only rules I'm using are:

  1. Only count lines that have more than 3 characters (no blank lines or lines that only have a closing brace and semi-colon, etc).

  2. Don't count spaces as characters.

Here's my program:

#include <stdio.h>

int main() {

    int input;
    int linecount = 0;
    int charcount = 0;

    while ((input = getchar()) != EOF) {

        if (input == ' ') {
        }
        else if (input == '\n') {
            if (charcount > 3) {
               linecount++;
            }
            charcount = 0;
        }
        else {
            charcount++;
        }
    }

    printf("%d\n", linecount);

    return 0;
}

My question is, can you offer some improvements to the rules to make this a more valid measure? Do people often count comments as valid lines of code? How about spaces or blank lines?

I don't want to start a debate over the validity of LOC counts in general, it's something I've been asked in several interviews and I think is worth knowing, in a general sense, how many lines of code my own projects are. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偷得浮生 2024-10-28 07:51:01

一般来说,人们会:

  • 将注释计为 LOC
  • 将空行计为 LOC

然而,人们也假设/实践:

  • 包含的注释是必要的/有用的(不是无缘无故的)
  • 空行并不过分,并且存在是为了提供清晰度

代码行计数器,因此,通常会在计算中考虑所有换行符。

Generally, people do:

  • Count comments as LOC
  • Count blank lines as LOC

However, people also assume/practice:

  • Comments included are necessary/useful (not gratituitous)
  • Blank lines are not excessive, and exist to provide clarity

Code line counters, as a result, generally take into account all line breaks in their computation.

梦里°也失望 2024-10-28 07:51:01

不要写程序。使用 wc --lines

Don't write a program. Use wc --lines

七秒鱼° 2024-10-28 07:51:01

我意识到这是一个编程示例,但如果你想使用 Windows 命令行,我发现 这个

这导致:

findstr /R /N "^" *.h *.c | find /C ":"

计算 .h 和 .c 文件中的所有行。

I realise this was a programming example, but if you want to use a windows command line I found this.

Which lead to:

findstr /R /N "^" *.h *.c | find /C ":"

Counts all lines in .h and .c files.

昔日梦未散 2024-10-28 07:51:01

我想我的开发风格与 kvista 不同,但我们分别计算了代码行和注释行。我们预计评论将占总行数(评论+代码)的一定百分比。我们根本没有计算空行。

如果您正在寻找一些编程挑战,您可以测量圈复杂度(或C 程序的条件复杂度)。

I guess I came from a different development style than kvista, but we counted lines of code and comments separately. We expected that the comments would be a certain percentage of the total lines (comments + code). We didn't count blank lines at all.

If you're looking for a bit of a programming challenge, you can measure the cyclomatic complexity (or conditional complexity) of your C programs.

美煞众生 2024-10-28 07:51:01

制定一个时间表,确定编写和调试应用程序需要多长时间。请注意结束日期。

花一些时间在网上搜索工具,例如:
http://www.campwoodsw.com/sourcemonitor.html

将剩余时间安排在日程表中,去度假吧。 :-)

否则任务就会变得艰巨。比如解析评论。定义一行(以及一行上有多少个语句或标记)。空行算不算?

为其他项目节省宝贵的时间。使用现有工具。

Make a schedule of how long it will take you to write and debug the application. Note the ending date.

Spend some time searching the web for tools such as:
http://www.campwoodsw.com/sourcemonitor.html

Take the remaining time in your schedule and go on vacation. :-)

Otherwise the task becomes monstrous. Such as parsing comments. Defining a line (and how many statements or tokens are on a line). Are blank lines counted?

Save your precious time for other projects. Use existing tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文