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



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

#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 .txtn");
    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 :nn", 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 = %dn",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 = %dn",commNum2);                 //just to test if my calculations are correct
                k++;
            }
        }
    }
    double commAVG = (commNum1 + commNum2) / fsize * 100;
    double avgTAS = (tabs + spaces) / 2;
    printf("nnOccurence 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。如果它遇到另一个特定字符或一组字符(/* 或 ),它将停止添加,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 = %dn",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%的问题:

如果注释的形成方式类似于//* comment text/*// comment text */,则可能会为commNum1commNum2添加相同的注释

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

我不打算进一步解决的事情:

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

最新更新