计算 c++ 代码中的特定内容
谁能帮助我使这个更通用、更专业?
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// open text file for input:
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// associate the input file stream with a text file
ifstream infile(file_name.c_str());
// error checking for a valid filename
if ( !infile )
{
cerr << "Unable to open file "
<< file_name << " -- quitting!\n";
return( -1 );
}
else cout << "\n";
// some data structures to perform the function
vector<string> lines_of_text;
string textline;
// read in text file, line by
while (getline( infile, textline, '\n' ))
{
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << lines_of_text.back() << "\n";
}
cout<<"OUTPUT BEGINS HERE: "<<endl<<endl;
cout<<"the total capacity of vector: lines_of_text is: "<<lines_of_text.capacity()<<endl;
int PLOC = (lines_of_text.size()+1);
int numbComments =0;
int numbClasses =0;
cout<<"\nThe total number of physical lines of code is: "<<PLOC<<endl;
for (int i=0; i<(PLOC-1); i++)
//reads through each part of the vector string line-by-line and triggers if the
//it registers the "//" which will output a number lower than 100 (since no line is 100 char long and if the function does not
//register that character within the string, it outputs a public status constant that is found in the class string and has a huge value
//alot more than 100.
{
string temp(lines_of_text [i]);
if (temp.find("//")<100)
numbComments +=1;
}
cout<<"The total number of comment lines is: "<<numbComments<<endl;
for (int j=0; j<(PLOC-1); j++)
{
string temp(lines_of_text [j]);
if (temp.find("};")<100)
numbClasses +=1;
}
cout<<"The total number of classes is: "<<numbClasses<<endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正确格式化代码,使用一致的风格和术语,并扔掉完全多余的注释和空行。生成的代码应该没问题。或“专业”。
在这里,我采取了努力(以及一些纯粹主观的风格上的东西):
请注意,输出实际上是错误的(只需在程序代码本身上运行它即可看到......)。
Format the code properly, use consistent style and nomenclature and throw out the utterly redundant comments and empty lines. The resulting code should be fine. Or “pro”.
Here, I’ve taken the efford (along with some stylistic things that are purely subjective):
Notice that the output is actually wrong (just run it on the program code itself to see that …).
我不太确定您在问什么,但我可以指出此代码中可以改进的一些内容。我将重点关注实际陈述,并将风格评论留给其他人。
cin>>文件名;
要处理带空格的文件名,最好这样写
getline(cin, file_name);
int PLOC = (lines_of_text.size()+1);
为什么你声称比实际多了一行?
if (temp.find("//")<100)
有一些复杂的评论解释了这一点。写得更好
if (temp.find("//")
在所有行长度上都能正常工作。
cout<<"注释行总数为:"<
实际上,您计算了行尾评论的数量。我不会将语句末尾的注释称为“注释行”。
您不计算
/* */
风格的注释。将类的数量计算为
};
?真的吗?struct
、enum
和简单的多余分号怎么样?只需计算class
关键字出现的次数即可。它的两侧不应有字母数字字符或下划线。I'm not really sure what you're asking, but I can point out some things that can be improved in this code. I'll focus on the actual statements and leave stylistic comments to others.
cin >> file_name;
To handle file names with spaces, better write
getline(cin, file_name);
int PLOC = (lines_of_text.size()+1);
Why do you claim that there's one more line than there actually is?
if (temp.find("//")<100)
with some complicated comment explaining this. Better write
if (temp.find("//")<temp.npos)
to work correctly on all line lengths.
cout<<"The total number of comment lines is: "<<numbComments<<endl;
Actually, you counted the number of end-of-line comments. I wouldn't call a comment at the end of a statement a "comment line".
You don't count
/* */
style comments.Counting the number of classes as
};
? Really? How aboutstruct
s,enum
s, and plain superfluous semicolons? Simply count the number of occurences of theclass
keyword. It should have no alphanumeric character or underscore on either side.++variable
而不是variable += 1
;++
运算符的存在是有原因的。cout
和<<
等内容之间留出空格,则函数参数和函数括号会这样做,否则不会,但要保持一致。为您的变量选择一种命名约定并坚持使用。您可以在 Google 上找到很多有关样式的信息,例如此处和此处。std
命名空间,仅使用您需要的部分。用户可以using std::cout;
或在所有cout
语句前加上std::
这些都是风格点。假设您的目标是计算评论和类,您的程序无法以其当前形式运行。这样做比你想象的要困难得多。如果我有一个“};”怎么办?例如作为字符串的一部分?如果我在字符串中有注释怎么办?
++variable
instead ofvariable += 1
when possible; the++
operator exists for a reason.cout
and<<
, function arguments and the function parantheses do it, otherwise don't, but be consistent. Pick one naming convention for your variables and stick to it. There is a lot about styles you can find on google, for example here and here.std
namespace, only what you need. User eitherusing std::cout;
or prefix all of yourcout
statements withstd::
ifstream infile(file_name.c_str());
does for example, what I don't know is what your program does as a whole, because I don't really care to understand what it does due to the indentation. It's a short program, so rather than explaning every statement on its own, why not explain what the program's purpose is, and how to use it?These are all stylistic points. Your program doesn't work in its current form, assuming your goal is to count comments and classes. Doing that is a lot more difficult than you are considering. What if I have a "};" as part of a string for example? What if I have comments in strings?
不要导入整个
std
命名空间,仅导入您需要的内容:使用一致的命名约定:决定您更喜欢
name_for_a_variable
还是nameforavariable
或nameForAVariable
。并使用有意义的名称:numbComments
让我联想到与numberOfComments
、numComments
或commentCount
截然不同的事物。如果您的原始代码如下所示,我强烈建议您选择单一一致的缩进样式:要么在同
一个源文件中,要么
在同一个源文件中不两者。
还要删除无用的注释,例如
This is “only” 关于代码的可读性,甚至没有触及其功能...(正如其他人已经指出的那样,这是不正确的)。请注意,任何一段代码都可能被读取的次数多于编辑的次数。我相当确定,如果您需要在几个月后阅读(并理解)您自己的这种形式的代码,您将遇到困难。
Don't import the whole
std
namespace, only things you need from it:Use a consistent naming convention: decide whether you prefer
name_for_a_variable
ornameforavariable
ornameForAVariable
. And use meaningful names:numbComments
makes me associate to very different things than wouldnumberOfComments
,numComments
orcommentCount
.If your original code looks like this, I strongly recommend to select a single consistent indentation style: either
or
bot not both in the same source file.
Also remove the useless comments like
This is "only" about the readability of your code, not even touching its functionality... (which, as others have already pointed out, is incorrect). Note that any piece of code is likely to be read many more times than edited. I am fairly sure that you will have trouble reading (and understanding) your own code in this shape, if you need to read it even a couple of months after.
“更专业”并不是根本不做。使用现有的 SLOC 计数器,这样您就不必重新发明轮子。
此讨论列出了一些:
http://discuss.techinterview.org/default.asp?joel.3.207012。 14
另一个提示:不要使用 "temp.find("};}) < 100)",使用 "temp.find("};") != temp.npos;"
编辑:s/end()/npos。呃。
"More professional" would be not doing it at all. Use an existing SLOC counter, so you don't reinvent the wheel.
This discussion lists a few:
http://discuss.techinterview.org/default.asp?joel.3.207012.14
Another tip: Don't use "temp.find("};}) < 100)", use "temp.find("};") != temp.npos;"
Edit: s/end()/npos. Ugh.