计算文件中 8 位符号的频率
Count frequency of 8-bit symbols from file
我有下一个挑战,需要一点帮助 ;)
Program to write: The file is treated as a row of 8-bit symbols.
- Count the frequency (incidence) of these symbols
- 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).
所以我写了一点代码;这里是:
主要内容:
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;
编辑:现在好了吗?
Now i need a help, what will be the best way to write a 2* point?
与您目前所做的几乎相同,只是您需要使用二维数组并跟踪您看到的前一个字符,例如
++charArray[b][c];
b = c;
当然,打印数组时您需要处理两个维度。
我有下一个挑战,需要一点帮助 ;)
Program to write: The file is treated as a row of 8-bit symbols.
- Count the frequency (incidence) of these symbols
- 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).
所以我写了一点代码;这里是:
主要内容:
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;
编辑:现在好了吗?
Now i need a help, what will be the best way to write a 2* point?
与您目前所做的几乎相同,只是您需要使用二维数组并跟踪您看到的前一个字符,例如
++charArray[b][c];
b = c;
当然,打印数组时您需要处理两个维度。