计算文件

时间:2017-01-10 09:38:10

标签: 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循环接管并完成整个数组的搜索。问题是,它正在计算其他东西,我无法弄清楚我的方法中的缺陷。谢谢!

1 个答案:

答案 0 :(得分:1)

让我们稍微玩一下......(你应该用调试器做的事情)

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 /*