转载注明出处:http://blog.csdn.net/wang_zheng_kai
让学生用开发工具,练习学到的数据压缩编码
本文包括两部分:
linux上用c实现算术编码(一)--具体操作
/********************************* ***Arithmetic coding fixed mode*** *****Writed By Wang Zhengkai****** ***********2014.11.24************* **********************************/ #include<stdio.h> #include<math.h> #include<string.h> char c[20],s[100]; float p[20],f[20],len; int cord[100]; /************************************** * **************encord**************** * ***********************************/ void encord(int a,int h) { float Fr; float Ps=1; float Fs=0; float cp[100]; int i,j; for(i=0;i<h;i++) { for(j=0;j<a;j++) { if(s[i]==c[j]) { cp[i]=p[j]; Fr=f[j]; } } Fs=Fs+Ps*Fr; Ps*=cp[i]; } printf("The result of encord: %f\n",Fs); len=log(1/Ps)/log(2); if(len>(int)len) len=(int)len+1; else len=(int)len; for(i=0;len>i;i++) { Fs=2*Fs; if(Fs>1) { Fs=Fs-1; cord[i]=1; } else if(Fs<1) cord[i]=0; else { cord[i]=1; break; } } if(i>=len) { for(j=i;j>=1;j--) { cord[j-1]=(cord[j-1]+1)%2; if(cord[j-1]==1) break; } } printf("The resault of encord:\n"); for(j=0;j<i;j++) printf("%d ",cord[j]); printf("\n"); } /************************************** * **************decord**************** * ***********************************/ void decord(int a,int h) { int i,j; float Ft,Pt,w=0.5,result=0; float Fs=0,Ps=1; for(i=0;i<len;i++,w*=0.5) //two_to_ten result += w*cord[i]; printf("Select the number of decode: %f\n",result); printf("The result of decord:\n"); for(i=0;i<h;i++) { for(j=a;j>0;j--) { Ft=Fs; Pt=Ps; Ft+=Pt*f[j-1]; Pt*=p[j-1]; if(result>=Ft) { Fs=Ft; Ps=Pt; printf("%c ",c[j-1]); break; } } } printf("\n"); } int main(void) { int num,i,h=0; char rub; /************************************** * ****input num of symbols************ * ***********************************/ printf("Please input num of character!\n"); if(1==scanf("%d", &num)) { // printf("%d\n",num); rub=getchar(); } else { rewind(stdin); printf("num:input error!\n"); } /************************************** * ***input sybmol and values********** * ***********************************/ printf("Please input character symbols and their probability values of corresponding!\n"); for(i=0;i<num;i++) { printf("Please input character symbols!\n"); scanf("%c", &c[i]); printf("Please input probability values of corresponding!\n"); scanf("%f", &p[i]); rub=getchar(); } for(i=0;i<num;i++) { printf("<%d>----> %c ",i,c[i]); } printf("\n"); for(i=0;i<num;i++) { printf("<%d>----> %f ",i,p[i]); } printf("\n"); for(i=1;i<num;i++) { f[0]=0; f[i]=f[i-1]+p[i-1]; } /************************************** * ******input encord symbol*********** * ***********************************/ printf("Please input encord symbols,and input * to exit!\n"); while(1) { char ss; scanf("%c", &ss); rub=getchar(); if(ss=='*')break; s[h++]=ss; } printf("The numbers of input\n"); for(i=0;i<h;i++) { printf("%c ",s[i]); } printf("\n"); /************************************** * *******encord and dedord************ * ***********************************/ printf("............encord............\n "); encord(num,h); printf("............decord............\n "); decord(num,h); return 0; }
时间: 2024-10-04 21:02:15