-1

これをより一般化してよりプロにするのを手伝ってくれる人はいますか?

#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;
4

5 に答える 5

4

コードを適切にフォーマットし、一貫したスタイルと命名法を使用し、まったく冗長なコメントと空行を捨てます。結果のコードは問題ないはずです。または「プロ」。

ここでは、(純粋に主観的ないくつかの文体的​​なものと一緒に)努力をしました:

出力が実際には間違っていることに注意してください (それを確認するには、プログラム コード自体で実行してください…)。

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string file_name;

    cout << "please enter file name: ";
    cin  >> file_name;

    ifstream infile(file_name.c_str());

    if (not infile) {
        cerr << "Unable to open file " << file_name << " -- quitting!" << endl;
        return -1;
    }
    else cout << endl;

    vector<string> lines_of_text;
    string textline;

    while (getline(infile, textline)) {
        lines_of_text.push_back(textline);
        cout << lines_of_text.back() << endl;
    }

    cout << "OUTPUT BEGINS HERE:  " << endl << endl;
    cout << "the total capacity of vector: lines_of_text is: "
         << lines_of_text.capacity() << endl << endl;

    int ploc = lines_of_text.size() + 1;

    cout << "The total number of physical lines of code is: " << ploc << endl;

    // Look for comments `//` and count them.
    int num_comments = 0;
    for (vector<string>::iterator i = lines_of_text.begin();
            i != lines_of_text.end();
            ++i) {
        if (i->find("//") != string::npos)
            ++num_comments;
    }

    cout << "The total number of comment lines is: " << num_comments << endl;

    // Same for number of classes ...
}
于 2010-04-04T15:14:30.477 に答える
3

あなたが何を求めているのかよくわかりませんが、このコードで改善できる点をいくつか指摘できます。私は実際のステートメントに焦点を当て、文体に関するコメントは他の人に任せます。

  • cin >> file_name;
    スペースを含むファイル名を処理するには、次のように記述します
    getline(cin, file_name);

  • int PLOC = (lines_of_text.size()+1);
    実際よりも 1 行多いと主張するのはなぜですか?

  • if (temp.find("//")<100)
    これを説明するいくつかの複雑なコメントがあります。
    if (temp.find("//")<temp.npos)
    すべての行の長さで正しく動作するように書き込みます。

  • cout<<"The total number of comment lines is: "<<numbComments<<endl;
    実際、行末コメントの数を数えました。ステートメントの最後のコメントを「コメント行」とは呼びません。

  • /* */スタイル コメントはカウントしません。

  • クラスの数を};? 本当に?structs、enums、および単純な余分なセミコロンはどうですか? classキーワードの出現回数を数えるだけです。どちらの側にも英数字またはアンダースコアを含めないでください。

于 2010-04-04T15:13:24.343 に答える
2
  1. 適切なインデントを使用してください。コードは現在の形式では非常に読みにくくなっています。スタイル一覧はこちら。
  2. 可能な場合++variableは代わりに優先します。オペレーターは理由があって存在しますvariable += 1++
  3. コーディング スタイルに一貫性を持たせます。coutとのようなものの間にスペースを残す場合は<<、関数の引数と関数の括弧がそれを行います。それ以外の場合は行いませんが、一貫性を保ちます。変数の命名規則を 1 つ選び、それに従います。Google で見つけることができるスタイルについては、ここここなど、たくさんあります。
  4. 名前空間全体を使用しないでくださいstd。必要なものだけを使用してください。ユーザーのいずれか、using std::cout;またはすべてのcoutステートメントの前にstd::
  5. 不要なコメントは避けてください。たとえば、何が何をするかは誰もが知ってifstream infile(file_name.c_str());いますが、インデントのためにプログラムが何をするかを本当に理解しようとしないので、私が知らないのはあなたのプログラムが全体として何をするかです。これは短いプログラムなので、すべてのステートメントを単独で説明するのではなく、プログラムの目的と使用方法を説明してみませんか?

これらはすべて文体上のポイントです。あなたのプログラムは、コメントとクラスをカウントすることが目標であると仮定すると、現在の形式では機能しません。それを行うことは、あなたが考えているよりもはるかに困難です。「};」があるとどうなりますか たとえば、文字列の一部として?文字列にコメントがある場合はどうなりますか?

于 2010-04-04T15:14:25.377 に答える
0

「より専門的に」ということは、まったくしていないでしょう。既存の SLOC カウンターを使用して、一からやり直さないようにします。

このディスカッションにはいくつかのリストがあります: http://discuss.techinterview.org/default.asp?joel.3.207012.14

別のヒント: 「temp.find("};}) < 100)」を使用しないでください。「temp.find("};") != temp.npos;」を使用してください。

編集: s/end()/npos. うーん。

于 2010-04-04T15:11:58.750 に答える
0

名前空間全体をインポートしないstdでください。必要なものだけをインポートしてください。

using std::string;

一貫した命名規則を使用してname_for_a_variableください。そして、意味のある名前を使用してください。nameforavariablenameForAVariablenumbCommentsnumberOfCommentsnumCommentscommentCount

元のコードが次のようになっている場合は、単一の一貫したインデント スタイルを選択することを強くお勧めします。

if ( ... )
    {
    ...
    }

また

if ( ... )
{
    ...
}

botが同じソース ファイル内にあるわけではありません。

などの無用なコメントも削除してください。

// add the new element to the vector

これは、コードの可読性に関する「のみ」であり、その機能に触れていません...(他の人がすでに指摘しているように、これは正しくありません)。コードのどの部分も、編集されるよりも何度も読み取られる可能性が高いことに注意してください。数か月後にコードを読む必要が生じたとしても、このような形で自分のコードを読む (そして理解する) のに苦労することは間違いありません。

于 2010-04-04T15:04:55.253 に答える