字母的频率 - 不成比例的高比率

时间:2016-11-18 03:19:42

标签: c

这个项目计算了一本书中字母的平均字母频率 - 由于某种原因,A,B和Z的数字大幅增加 - 数十亿。我在这里错过了什么吗?我觉得没有错误,但结果代码是错误的。 argv[1]是书籍文件,argv[2]是新文件。

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_LETTERS 26

int main( int argc, char *argv[] )
{
    FILE *fp,*fp2;
    int ch, totalcounter = 0, i;
    int letters[25];
    double letterfrequency[25];
    if(argc < 2)
    {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        exit(1);
    }
    for(i=0; i<NUM_LETTERS; i++)
    {
        letters[i] = 0;
        letterfrequency[i] = 0.0;
    }
    printf("Opening: %s", argv[1]);
    fp = fopen(argv[1], "r");
    if(!fp)
    {
        perror("fopen");
        exit(1);
    }
    while((ch=fgetc(fp)) != EOF)
    {
        ch = toupper(ch);
        if('A' <= ch && ch <= 'Z')
        {
            ch -= 65;
            letters[ch]++;
            totalcounter++;
        }
    }

    fp2 = fopen(argv[2], "w");
    for(i=0; i<NUM_LETTERS; i++)
    {
        letterfrequency[i] = (double)letters[i]/totalcounter;
    }
    for(i=0; i<NUM_LETTERS; i++)
    {
        fprintf(fp2, "\n%c: Times used: %10d\tFrequency Used: %20.20lf", i+65, letters[i], letterfrequency[i]);
    }
    fclose(fp);
    fclose(fp2);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

根本问题在于你试图将26个字母的数量压缩到为25个保留的空间。这不会带来快乐。

这是对代码的次要清理。除了将数组大小更改为NUM_LETTERS之外,它还会更好地报告使用情况,检查输出文件是否已打开,使用isalpha()检查字母,并通过减去{{从大写转换为偏移1}}而不是'A'

65

示例输出(在上面的源代码 - #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define NUM_LETTERS 26 int main(int argc, char *argv[]) { FILE *fp, *fp2; int ch, totalcounter = 0, i; int letters[NUM_LETTERS]; double letterfrequency[NUM_LETTERS]; if (argc != 3) { fprintf(stderr, "Usage: %s infile outfile\n", argv[0]); exit(1); } for (i = 0; i < NUM_LETTERS; i++) { letters[i] = 0; letterfrequency[i] = 0.0; } printf("Opening: %s\n", argv[1]); fp = fopen(argv[1], "r"); if (!fp) { perror("fopen"); exit(1); } while ((ch = fgetc(fp)) != EOF) { if (isalpha(ch)) { ch = toupper(ch) - 'A'; letters[ch]++; totalcounter++; } } fclose(fp); for (i = 0; i < NUM_LETTERS; i++) { letterfrequency[i] = (double)letters[i] / totalcounter; } printf("Opening: %s\n", argv[2]); fp2 = fopen(argv[2], "w"); if (!fp) { perror("fopen"); exit(1); } for (i = 0; i < NUM_LETTERS; i++) { fprintf(fp2, "%c: Times used: %10d\tFrequency Used: %20.20lf\n", i + 65, letters[i], letterfrequency[i]); } fclose(fp2); return 0; } 上):

fq97.c