#include"stdio.h"
#include<cmath>
#include<string.h>
#define N 256
//This is the called function.This function is used to compute the entropy of the whole text.
float log2(float index)
{
return log(index)/log(2.0f);
}
//build a function of log2()
float Entropy(float Sa)
{
float H;
H=-Sa*log2(Sa);
return(H);
}
//compute the entropy
int main()
{
//open the file
FILE *fp;
char ch;
int i;
int index;
int sum=0;
float Hs=0.0;
fp=fopen("C:\\669HOMEWORK\\material\\text.dat","rt");
if(fp==NULL)
{
printf("Can not open the file!\n");
return 1;
}
/***********************************************************************
read the text
************************************************************************/
struct TextPoint
{
float prob;
char lr;
}sym[N];
//any ASCII which is exsited in the text has to computer its probability and
//show the letter's name in the screen.lr=letter.
ch=fgetc(fp);
printf("The Answer of Problem 1.1.(1) is:\n");
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
sum++;
index=(int)(ch-'NUL');
sym[index].prob++;
sym[index].lr=ch;
for(i=0;i<N;i++)
{
Hs+=Entropy((sym[i].prob)/sum);//compute the entropy.
printf("The probability of %s is %f\n",sym[i].lr,(sym[i].prob)/sum);
}
printf("The entropy is %f",Hs);
}
fclose(fp);
return 0;
}
生成解决方案成功,但运行总说我“try3.exe 中的 0x004116b5 处最可能的异常: 0xC0000005: 读取位置 0xfda04d0c 时发生访问冲突”这是什么意思啊?请大家帮帮忙啊。。。泪奔
1.在#define N 256语句下面添上:
#define NUL '\0'
2.在while(ch!=EOF)语句前添上:
memset(sym,0x0,sizeof(sym));
3.index=(int)(ch-NUL);改为:
index=(int)(ch-NUL);
4.printf("The probability of %s is %f\n",sym[i].lr,(sym[i].prob)/sum);改为:
printf("The probability of %c is %f\n",sym[i].lr,(sym[i].prob)/sum);
---------------------------------------------------------
#include"stdio.h"
#include<cmath>
#include<string.h>
#define N 256
#define NUL '\0'/////////////////////////////////////////
//This is the called function.This function is used to compute the entropy of the whole text.
float log2(float index)
{
return log(index)/log(2.0f);
}
//build a function of log2()
float Entropy(float Sa)
{
float H;
H=-Sa*log2(Sa);
return(H);
}
//compute the entropy
int main()
{
//open the file
FILE *fp;
char ch;
int i;
int index;
int sum=0;
float Hs=0.0;
fp=fopen("C:\\text.dat","rt");
if(fp==NULL)
{
printf("Can not open the file!\n");
getchar();
return 1;
}
/***********************************************************************
read the text
************************************************************************/
struct TextPoint
{
float prob;
char lr;
}sym[N];
//any ASCII which is exsited in the text has to computer its probability and
//show the letter's name in the screen.lr=letter.
ch=fgetc(fp);
printf("The Answer of Problem 1.1.(1) is:\n");
memset(sym,0x0,sizeof(sym));///////////////////////////////////
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
sum++;
index=(int)(ch-NUL);///////////////////
sym[index].prob++;
sym[index].lr=ch;
for(i=0;i<N;i++)
{
Hs+=Entropy((sym[i].prob)/sum);//compute the entropy.
printf("The probability of %c is %f\n",sym[i].lr,(sym[i].prob)/sum);/////////////////////
}
printf("The entropy is %f",Hs);
}
fclose(fp);
getchar();
return 0;
}本回答被提问者采纳