From: William Park on
Dion <a_dionysia(a)yahoo.co.uk> wrote:
> It can be even the simple "Hello World" program. Let's say I have
> something like that:
>
> // This is my program
> // I want to calculate its comments' proportion vs. the whole C++
> program
> // Let'try
>
> int main()
> {
> cout << "Hello Wolrd!" << endl;
>
> return 0;
> }
>
> >From this program I would like to output that the 3 commented lines are
> the 33.33% of the whole program's lines.

First, extract only the comment lines (not as easy as you'd think).
Then,
man wc
man awk
http://home.eol.ca/~parkw/index.html#rpn

--
William Park <opengeometry(a)yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
From: Janis Papanagnou on
Dion wrote:
> It can be even the simple "Hello World" program. Let's say I have
> something like that:
>
> // This is my program
> // I want to calculate its comments' proportion vs. the whole C++
> program
> // Let'try
>
> int main()
> {
> cout << "Hello Wolrd!" << endl;
>
> return 0;
> }
>
>>From this program I would like to output that the 3 commented lines are
> the 33.33% of the whole program's lines.
>
>
> Thanks a lot for your help!
>

Something like this maybe...

awk '
/\/\// {c++}
/^[ \t]*$/{e++}
END { printf("Lines:%d Comments:%d Empty:%d C/L:%.3g%\n",NR,c,e,100*c/NR) }
'

If you don't want to the count empty lines as part of the program change the
expression 100*c/NR to 100*c/(NR-e).

(Though it does not check for a "//" sequence within a string literal.)

Janis
From: Ian Wilson on
Dion wrote:
> I am a beginner in UNIX and I want to write a shell script calculating
> the proportion of one-line comments in any C++ program. Could anyone
> help me please? Thanks!
>
Please don't multi-post, either pick one newsgroup or use cross-posting
carefully:
http://www.uwasa.fi/~ts/http/crospost.html

Also
http://www.catb.org/~esr/faqs/smart-questions.html#homework
From: Ed Morton on


Dion wrote:
> It can be even the simple "Hello World" program. Let's say I have
> something like that:
>
> // This is my program
> // I want to calculate its comments' proportion vs. the whole C++
> program
> // Let'try
>
> int main()
> {
> cout << "Hello Wolrd!" << endl;
>
> return 0;
> }
>
>>From this program I would like to output that the 3 commented lines are
> the 33.33% of the whole program's lines.
>

What would you want the output to be for this:

// This is my program with a multi-line continued comment \
I want to calculate its comments' proportion vs. the whole C++ \
program Let'try

int main()
{
cout << "Hello Wolrd!" << endl; // comment at end of line
cout << "Slashes look like this: //" << endl;

return 0;
}

i.e.:

What do you mean by a "line" - stops at a newline, or ignores escaped
newlines?
How should a line with comments plus text be counted?
How should you treat lines with the comment start indicator embedded in
a string?

Regards,

Ed.