计算文件中注释文本的百分比

标签 c

我正在尝试计算文件中评论文本的百分比,但我无法弄清楚我的计算方法有什么问题。

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int k, commNum1 = 0, commNum2 = 0, Nbrackets1 = 0, Nbrackets2 = 0, Cbrackets1 = 0, Cbrackets2 = 0, tabs = 0, spaces = 0;
    char str[10000];
    char ch, file_name[75];
    FILE *fp;
    char writtenText[2000];

    printf("Enter the name of file you wish to see with extension .c or .txt\n");
    gets(file_name);

    fp = fopen(file_name, "a");  // reads the file

    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        _getche();
        exit(EXIT_FAILURE);
    }
    printf("Enter a sentence:\n");
    gets(writtenText);
    fprintf(fp, "%s", writtenText);
    fclose(fp);
    fp = fopen(file_name, "r");
    printf("The contents of %s file are :\n\n", file_name);
    int i = 0;
    while ((ch = fgetc(fp)) != EOF) {
        //      printf("%c", ch);
        str[i] = ch;                                        //printing and storing process
        i++;
    }
    int fsize = i;

    for (k = 0; k < fsize; k++) {
        if (str[k] == '(')
            Nbrackets1++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == ')')
            Nbrackets2++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == '{')
            Cbrackets1++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == '}')
            Cbrackets2++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == '\t')
            tabs++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == ' ')
            spaces++;
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == '/' && str[k + 1] == '*') {
            while (str[k] != '*' && str[k + 1] != '/') {
                commNum1++;
                if (str[k] == ' ') {
                    commNum1--;
                }
                //              printf("commNum1 = %d\n",commNum1);                 //just to test if my calculations are correct
                k++;
            }
        }
    }
    for (k = 0; k < fsize; k++) {
        if (str[k] == '/' && str[k + 1] == '/') {
            while (str[k] != '\n') {
                commNum2++;
                if (str[k] == ' ') {
                    commNum2--;
                }
                //              printf("commNum2 = %d\n",commNum2);                 //just to test if my calculations are correct
                k++;
            }
        }
    }
    double commAVG = (commNum1 + commNum2) / fsize * 100;
    double avgTAS = (tabs + spaces) / 2;
    printf("\n\nOccurence of character ( : %d", Nbrackets1);
    printf("\nOccurence of character ) : %d", Nbrackets2);
    printf("\nOccurence of character {  : %d ", Cbrackets1);
    printf("\nOccurence of character } : %d ", Cbrackets2);
    printf("\nAverage number of spaces and tabulations: %2.f", avgTAS);
    printf("\nPercentage of comment text in the file: %2.f%%", commAVG);
    fclose(fp);
    return 0;
}

我的观点是 for 循环遍历存储文本的整个数组。如果它遇到一组特定的字符(/* 或//),它将开始向 int 加 1。在添加时,如果发现中间存在空格,则会减去 1。如果遇到另一个特定字符或字符集(/* 或\n),则会停止添加,并且 for 循环接管并完成整个数组的搜索。问题是它正在计算其他东西,而我无法找出我的方法中的缺陷。谢谢!

最佳答案

让我们玩一下...(您应该使用调试器做的事情)

for (k = 0; k < fsize; k++) {
    if (str[k] == '/' && str[k + 1] == '*') {
        while (str[k] != '*' && str[k + 1] != '/') {
            commNum1++;
            if (str[k] == ' ') {
                commNum1--;
            }
            //              printf("commNum1 = %d\n",commNum1);                 //just to test if my calculations are correct
            k++;
        }
    }
}

考虑文本"/* abc */"

if (str[0] == '/' && str[1] == '*') // true
while (str[0] != '*' && str[1] != '/') // true
commNum1++;
k++;
while (str[1] != '*' && str[2] != '/') // false, cause str[1] == '*'

故事结束。

您应该尝试首先在注释开始上方增加 k,然后更改 while情况

while (str[k] != '*' || str[k + 1] != '/') // instead of &&

此外,在使用前瞻的循环中,调整边界

for (k = 0; k < (fsize - 1); k++) // instead of k < fsize

也许您还有更多错误,但这是明显的错误。

编辑:

既然你提到了 400% 的问题:

您可以为两者添加相同的评论,commNum1commNum2 ,如果评论的形式类似于 //* comment text/*// comment text */

此外,您的内部 while 循环不会检查 k < fsize ,这意味着检查将超出文件最后一行的数组末尾。在那里你会得到未定义的行为,可能会计算文件结束后的注释,直到达到 400%。

我不会进一步讨论的事情:

/\
* comment starts here, cause \ is preprocessor line removal which merges the two lines into a /*

关于计算文件中注释文本的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41565554/

相关文章:

c - 使一个函数只能从另一个函数访问

c - makefile 包含其他目录中的 *.h 文件

c - 为什么 64 位 Windows 上原生 long 原语的大小只有 4 个字节?

c - C 中两个服务器的套接字客户端

c - C语言编程解密md5

c - 如何在 Windows 上的 Eclipse 中对 C 项目运行 .pl 测试?

c - C中的翻译单元到底是什么

c++ - 如何转换 RGB -> YUV -> RGB(双向)

c - 为什么我无法获取字符串输入?

c - 多线程 random_r 比单线程版本慢