0

I have next challenge and need a little help ;)

Program to write: The file is treated as a row of 8-bit symbols.

  1. Count the frequency (incidence) of these symbols
  2. Count the frequency of these symbols after symbols (if the previous character is given, in front of first charakter is sign with code 0).

The program should work for doc, pdf, mp4, jpg (take a min 1MB file).

So I wrote a little bit of code; here it is:

main:

int array[256] = {0};
double charArray[256][256] = {{0}};

FILE *fp = fopen("test.txt", "rb");

int c;
int b = 0;

printf("File content: \n");
while((c = fgetc(fp)) != EOF)
{
    printf("%2x", c);
    ++charArray[b][c];
    ++array[c];
    b=c;
}

int k;

printf("\nSymbols frequency counter: \n");
for(k=0;k<256;k++) {
    if(array[k] > 0 ) {
        printf("char %2x: %d times \n", k, array[k]);
    }
}


int y,z;
printf("Symbols after symbols frequency counter: \n");

for(y=0;y<256;y++){

    for(z=0;z<256;z++){

        if(charArray[y][z] > 0) {
        printf("char %2x after char %2x: %.0f times\n", z, y, charArray[y][z]);
        }
    }
}

fclose(fp);

return 0;

EDIT: It's now good?

4

1 回答 1

1

现在我需要帮助,写 2* 点的最佳方法是什么?

与您当前所做的几乎相同,除了您需要使用 2D 数组并跟踪您看到的前一个字符,例如

++charArray[b][c];
b = c;

而且,当然,您需要在打印数组时处理两个维度。

于 2015-03-25T21:22:24.780 回答