linux上用c實現算術編碼(二)--具體代碼

來源:互聯網
上載者:User

標籤:c語言   資料壓縮   linux   算術編碼   

轉載註明出處:http://blog.csdn.net/wang_zheng_kai

讓學生用開發工具,練習學到的資料壓縮編碼

本文包括兩部分:

linux上用c實現算術編碼(一)--具體操作

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_tenresult += 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;}


linux上用c實現算術編碼(二)--具體代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.