2016-11-18 85 views
-1

該項目計算書中字母的平均字母頻率 - 出於某種原因,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; 
} 
+8

哪個字母僅具有25個信嗎?你正在寫出你的數組的界限,所以任何事情都會發生。 –

+0

一個快速而髒的測試文件,只包含字母a到z的段落錯誤。你的代碼需要一些工作。 – David

+0

您使用字符常量進行範圍檢查,但爲什麼這樣:'ch - = 65;'?不要使用神奇數字!爲什麼不簡單地使用'isalpha'作爲支票? – Olaf

回答

0

最根本的問題是,你要擠計數爲26個字母到空間25.本保留不導致幸福。

這是您的代碼的小清理。除了將陣列大小更改爲NUM_LETTERS之外,它還會更好地報告使用情況,檢查是否打開輸出文件,使用isalpha()檢查字母,並通過減去'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):

Opening: fq97.c 
Opening: /dev/stdout 
A: Times used:   17 Frequency Used: 0.02956521739130434784 
B: Times used:   3 Frequency Used: 0.00521739130434782650 
C: Times used:   26 Frequency Used: 0.04521739130434782733 
D: Times used:   13 Frequency Used: 0.02260869565217391366 
E: Times used:   78 Frequency Used: 0.13565217391304348893 
F: Times used:   41 Frequency Used: 0.07130434782608695288 
G: Times used:   13 Frequency Used: 0.02260869565217391366 
H: Times used:   13 Frequency Used: 0.02260869565217391366 
I: Times used:   50 Frequency Used: 0.08695652173913043237 
J: Times used:   0 Frequency Used: 0.00000000000000000000 
K: Times used:   0 Frequency Used: 0.00000000000000000000 
L: Times used:   33 Frequency Used: 0.05739130434782608453 
M: Times used:   8 Frequency Used: 0.01391304347826087008 
N: Times used:   43 Frequency Used: 0.07478260869565217517 
O: Times used:   25 Frequency Used: 0.04347826086956521618 
P: Times used:   26 Frequency Used: 0.04521739130434782733 
Q: Times used:   5 Frequency Used: 0.00869565217391304358 
R: Times used:   52 Frequency Used: 0.09043478260869565466 
S: Times used:   25 Frequency Used: 0.04347826086956521618 
T: Times used:   61 Frequency Used: 0.10608695652173913415 
U: Times used:   26 Frequency Used: 0.04521739130434782733 
V: Times used:   6 Frequency Used: 0.01043478260869565299 
W: Times used:   2 Frequency Used: 0.00347826086956521752 
X: Times used:   3 Frequency Used: 0.00521739130434782650 
Y: Times used:   6 Frequency Used: 0.01043478260869565299 
Z: Times used:   0 Frequency Used: 0.00000000000000000000 
+0

它的工作原理!謝謝 – Submersed24