簡體   English   中英

計算 C 文件中注釋字符和單詞的程序

[英]program to count commented characters and words in a C file

我必須計算 C 文件評論中的字符和單詞,包括單行評論和阻止評論。 這是我所擁有的:

#include <stdio.h>

#define IN = 1
#define OUT = 0

main() {
    int c, nc;
    nc = 0;
    while ((c = getchar()) != EOF) {
        if (c == '/') {
            if (getchar() == '/')
                while (getchar() != '\n')
                    ++nc;
        }  
    }
    
    if (c == '/') {
        if (getchar() == '*')
            while (getchar() != '/')
                ++nc;
    }  
    
    printf("Character Counts: %d\n", nc);
}

它適用於每一行注釋 ( // ),但它會跳過被阻止的注釋 ( /*...*/ )。 我覺得它永遠不會為被阻止的評論輸入 if 塊。 非常感謝!

您的代碼中存在多個問題:

  • 您必須將int指定為main function 的返回類型。問題中的語法已過時。

  • INOUT的定義不正確。 你應該使用

     #define IN 1 #define OUT 0

    要么

     enum { IN = 1, OUT = 0 };
  • 第一個循環消耗了標准輸入中的所有字節,你在文件末尾,所以/*...*/評論的測試永遠不會產生任何東西。

  • 如果在文件末尾之前未找到測試的字節,則諸如while (getchar() != '\n')類的循環可以永遠運行。

  • 您不能單獨測試///*...*/注釋,因為一個可以隱藏另一個:

     //* this is a line comment that does not start a C style one /* this comment contains a // but stops here */ return 0;
  • 另請注意,您應該解析 C 字符串和字符常量,因為它們可能包含不開始注釋的//和或/*序列。

  • 對於完整的解決方案,您還應該處理轉義的換行符。 以下是一些病態的例子:

     // this is a line comment that extends \ on multiple \ lines (and tricks the colorizer) /\ * this is a C comment, but the colorizer missed it *\ /

在一般情況下,這個問題很難解決,但您可以從簡單的情況開始。

這是修改后的版本:

#include <stdio.h>

int main() {
    int c, cc, nc = 0;

    while ((c = getchar()) != EOF) {
        if (c == '/') {
            if ((cc = getchar()) == '/') {
                while ((c = getchar()) != '\n')
                    nc++;
            } else
            if (cc == '*') {
                while ((cc = getchar()) != EOF) {
                    if (cc == '*') {
                        if ((cc = getchar()) == '/')
                            break;
                        ungetc(cc, stdin);
                    }
                    nc++;
                }
            }
        }
    }
    printf("Character Counts: %d\n", nc);
    return 0;
}

我添加了代碼來計算單詞數。 它在少數情況下有效,但當我在斜線后有空間時它表現得很奇怪。 例如,//評論... 大多數時候,字數減 1。

#include<stdio.h>
#define IN 1
#define OUT 0

int main() {
    int c, cc, nc = 0;
    int state;
    int nw = 0;
    state = OUT;
    
    while ((c = getchar()) != EOF) {
        if (c == '/') {
            if ((cc = getchar()) == '/') {
                while ((c = getchar()) != '\n'){
                  nc++;
                  if (c == ' ' || c == '\t')
                      state = OUT;
                   else if (state == OUT){
                      state = IN;
                      nw++;
                    }        
                }
            }      
       
            else if (cc == '*') {
                while ((cc = getchar()) != EOF) {
                if (cc == ' ' || cc == '\t')
                      state = OUT;
                   else if (state == OUT){
                      state = IN;
                      nw++;
                    }
                
                    if (cc == '*') {
                        if ((cc = getchar()) == '/')
                            break;
                        ungetc(cc, stdin);
                    }

                    nc++;
                }
            }
        }
    }
     
    printf("Character Counts: %d\n", nc);
    printf("Word Counts: %d\n", nw);
    return 0;
}

計算 C 文件中注釋字符和單詞的程序

它會跳過被屏蔽的評論 (/ ... /)

我建議至少解析代碼並查找 5 種狀態:正常、在 // 注釋中、在 /* 注釋中、在 "" 字符串文字中、在 '' 字符常量中。

// pseudo code
while ((ch = getchar()) != EOF) {
   if ch == '/' and next == '/', process `//` comment until end-of-line
   elseif ch '/' and next == '*', process `/*` comment until `*/`
   elseif ch '"', process string until  " (but not \")
   elseif ch ''', process constant until  ' (but not \')
   else process normally
}

要查看下一個字符,請調用getchar() ,然后調用ungetc()如果不符合預期)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM