计算代码行数的 C 程序
我正在用 C 编写一个简单的 LOC 计数器来计算我的 C 源文件中有多少行代码。它应该从命令行运行,将目标文件重定向为输入,并查看打印到标准输出的总行数。例如:
counter.exe < counter.c
15
到目前为止,我使用的唯一规则是:
仅计算包含 3 个以上字符的行(没有空白行或仅包含右大括号和分号等的行)。
不要将空格算作字符。
这是我的计划:
#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:
Only count lines that have more than 3 characters (no blank lines or lines that only have a closing brace and semi-colon, etc).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说,人们会:
然而,人们也假设/实践:
代码行计数器,因此,通常会在计算中考虑所有换行符。
Generally, people do:
However, people also assume/practice:
Code line counters, as a result, generally take into account all line breaks in their computation.
不要写程序。使用 wc --lines
Don't write a program. Use
wc --lines
我意识到这是一个编程示例,但如果你想使用 Windows 命令行,我发现 这个。
这导致:
计算 .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:
Counts all lines in .h and .c files.
我想我的开发风格与 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.
制定一个时间表,确定编写和调试应用程序需要多长时间。请注意结束日期。
花一些时间在网上搜索工具,例如:
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.