📄 信息熵统计.cpp
字号:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
using namespace std;
void main()
{
char ch,aa[26],bb[25],cc[2];
float a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,sum,storet,storer,twostr;
float dd[26][26];
int ii,jj;
a=0;
b=0;
c=0;
d=0;
e=0;
f=0;
g=0;
h=0;
i=0;
j=0;
k=0;
l=0;
m=0;
n=0;
o=0;
p=0;
q=0;
r=0;
s=0;
t=0;
u=0;
v=0;
w=0;
x=0;
y=0;
z=0;
sum=0;
storet=0;
storer=0;
twostr=0;
for(ii=0;ii<=25;ii++)
for(jj=0;jj<=25;jj++)
{
dd[ii][jj]=0;
}
for(ii=0;ii<=25;ii++)
{
aa[ii]=97+ii;
bb[ii]=97+ii;
}
cc[0]='$';
cc[1]='$';
ifstream infile;
ofstream outfile;
infile.open("sample.txt");
outfile.open("result.txt");
while(infile>>ch)
{
switch(ch)
{
case 'a':
{
a=a+1;
sum=sum+1;
}
break;
case 'b':
{
b=b+1;
sum=sum+1;
}
break;
case 'c':
{
c=c+1;
sum=sum+1;
}
break;
case 'd':
{
d=d+1;
sum=sum+1;
}
break;
case 'e':
{
e=e+1;
sum=sum+1;
}
break;
case 'f':
{
f=f+1;
sum=sum+1;
}
break;
case 'g':
{
g=g+1;
sum=sum+1;
}
break;
case 'h':
{
h=h+1;
sum=sum+1;
}
break;
case 'i':
{
i=i+1;
sum=sum+1;
}
break;
case 'j':
{
j=j+1;
sum=sum+1;
}
break;
case 'k':
{
k=k+1;
sum=sum+1;
}
break;
case 'l':
{
l=l+1;
sum=sum+1;
}
break;
case 'm':
{
m=m+1;
sum=sum+1;
}
break;
case 'n':
{
n=n+1;
sum=sum+1;
}
break;
case 'o':
{
o=o+1;
sum=sum+1;
}
break;
case 'p':
{
p=p+1;
sum=sum+1;
}
break;
case 'q':
{
q=q+1;
sum=sum+1;
}
break;
case 'r':
{
r=r+1;
sum=sum+1;
}
break;
case 's':
{
s=s+1;
sum=sum+1;
}
break;
case 't':
{
t=t+1;
sum=sum+1;
}
break;
case 'u':
{
u=u+1;
sum=sum+1;
}
break;
case 'v':
{
v=v+1;
sum=sum+1;
}
break;
case 'w':
{
w=w+1;
sum=sum+1;
}
break;
case 'x':
{
x=x+1;
sum=sum+1;
}
break;
case 'y':
{
y=y+1;
sum=sum+1;
}
break;
case 'z':
{
z=z+1;
sum=sum+1;
}
break;
default: break;
}
cc[0]=cc[1];
cc[1]=ch;
for(ii=0;ii<=25;ii++)
for(jj=0;jj<=25;jj++)
{
if(cc[0]==aa[ii]&&cc[1]==bb[jj])
dd[ii][jj]=dd[ii][jj]+1;
}
}
storet=(log(26)/log(2))*sum;
if(a!=0)
storer=-(a/sum)*((log(a/sum))/(log(2)));
if(b!=0)
storer=storer-(b/sum)*((log(b/sum))/(log(2)));
if(c!=0)
storer=storer-(c/sum)*((log(c/sum))/(log(2)));
if(d!=0)
storer=storer-(d/sum)*((log(d/sum))/(log(2)));
if(e!=0)
storer=storer-(e/sum)*((log(e/sum))/(log(2)));
if(f!=0)
storer=storer-(f/sum)*((log(f/sum))/(log(2)));
if(g!=0)
storer=storer-(g/sum)*((log(g/sum))/(log(2)));
if(h!=0)
storer=storer-(h/sum)*((log(h/sum))/(log(2)));
if(i!=0)
storer=storer-(i/sum)*((log(i/sum))/(log(2)));
if(j!=0)
storer=storer-(j/sum)*((log(j/sum))/(log(2)));
if(k!=0)
storer=storer-(k/sum)*((log(k/sum))/(log(2)));
if(l!=0)
storer=storer-(l/sum)*((log(l/sum))/(log(2)));
if(m!=0)
storer=storer-(m/sum)*((log(m/sum))/(log(2)));
if(n!=0)
storer=storer-(n/sum)*((log(n/sum))/(log(2)));
if(o!=0)
storer=storer-(o/sum)*((log(o/sum))/(log(2)));
if(p!=0)
storer=storer-(p/sum)*((log(p/sum))/(log(2)));
if(q!=0)
storer=storer-(q/sum)*((log(q/sum))/(log(2)));
if(r!=0)
storer=storer-(r/sum)*((log(r/sum))/(log(2)));
if(s!=0)
storer=storer-(s/sum)*((log(s/sum))/(log(2)));
if(t!=0)
storer=storer-(t/sum)*((log(t/sum))/(log(2)));
if(u!=0)
storer=storer-(u/sum)*((log(u/sum))/(log(2)));
if(v!=0)
storer=storer-(v/sum)*((log(v/sum))/(log(2)));
if(w!=0)
storer=storer-(w/sum)*((log(w/sum))/(log(2)));
if(x!=0)
storer=storer-(x/sum)*((log(x/sum))/(log(2)));
if(y!=0)
storer=storer-(y/sum)*((log(y/sum))/(log(2)));
if(z!=0)
storer=storer-(z/sum)*((log(z/sum))/(log(2)));
storer=storer*sum;
outfile<<"a="<<setw(6)<<a<<" P(a)="<<a/sum<<endl;
outfile<<"b="<<setw(6)<<b<<" P(b)="<<b/sum<<endl;
outfile<<"c="<<setw(6)<<c<<" P(c)="<<c/sum<<endl;
outfile<<"d="<<setw(6)<<d<<" P(d)="<<d/sum<<endl;
outfile<<"e="<<setw(6)<<e<<" P(e)="<<e/sum<<endl;
outfile<<"f="<<setw(6)<<f<<" P(f)="<<f/sum<<endl;
outfile<<"g="<<setw(6)<<g<<" P(g)="<<g/sum<<endl;
outfile<<"h="<<setw(6)<<h<<" P(h)="<<h/sum<<endl;
outfile<<"i="<<setw(6)<<i<<" P(i)="<<i/sum<<endl;
outfile<<"j="<<setw(6)<<j<<" P(j)="<<j/sum<<endl;
outfile<<"k="<<setw(6)<<k<<" P(k)="<<k/sum<<endl;
outfile<<"l="<<setw(6)<<l<<" P(l)="<<l/sum<<endl;
outfile<<"m="<<setw(6)<<m<<" P(m)="<<m/sum<<endl;
outfile<<"n="<<setw(6)<<n<<" P(n)="<<n/sum<<endl;
outfile<<"o="<<setw(6)<<o<<" P(o)="<<o/sum<<endl;
outfile<<"p="<<setw(6)<<p<<" P(p)="<<p/sum<<endl;
outfile<<"q="<<setw(6)<<q<<" P(q)="<<q/sum<<endl;
outfile<<"r="<<setw(6)<<r<<" P(r)="<<r/sum<<endl;
outfile<<"s="<<setw(6)<<s<<" P(s)="<<s/sum<<endl;
outfile<<"t="<<setw(6)<<t<<" P(t)="<<t/sum<<endl;
outfile<<"u="<<setw(6)<<u<<" P(u)="<<u/sum<<endl;
outfile<<"v="<<setw(6)<<v<<" P(v)="<<v/sum<<endl;
outfile<<"w="<<setw(6)<<w<<" P(w)="<<w/sum<<endl;
outfile<<"x="<<setw(6)<<x<<" P(x)="<<x/sum<<endl;
outfile<<"y="<<setw(6)<<y<<" P(y)="<<y/sum<<endl;
outfile<<"z="<<setw(6)<<z<<" P(z)="<<z/sum<<endl;
outfile<<"sum="<<sum<<endl;
outfile<<"the theoretical total space of the words is: "<<storet<<" bits"<<endl;
outfile<<"the practical total space of the words is: "<<storer<<" bits"<<endl;
for(ii=0;ii<=25;ii++)
for(jj=0;jj<=25;jj++)
{
if(dd[ii][jj]!=0)
twostr=twostr-(dd[ii][jj]/(sum-1))*((log(dd[ii][jj]/(sum-1)))/(log(2)));
}
outfile<<"the two-stair modle of the total space is:"<<twostr*(sum-1)/2<<" bits"<<endl;
infile.close();
outfile.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -