




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
#include<stdio.h>#include<stdlib.h>#inelude<math.h>#inelude<string.h>#defineN1000charkind[N]={'\0'};intnum[N]={0};intcount=0;ints1,s2;typedefstruct{intweight;/*字符權重*/intparent,lchild,rchild;}HTNode,*HuffmanTree;/*定義哈夫曼樹結點*/typedefchar**HuffmanCode;structcharcode{chardata;charcode[20];}Code[N],d[N],e[N];voidCreat_message()/*輸入字符并存入文件*/{FILE*fp;charc;if((fp=fopen(”文本文件.txt","wb"))==NULL){printf("cannotopenthisfile!\n");return;}printf("請輸入原代碼,以#表示結束\n”);do{scanf("%c",&c);fputc(c,fp);}while(c!='#');fclose(fp);}voidCalculate() /*計算文件中字符的權值 */{charc;inti=1,k;FILE*fp;if((fp=fopen(”文本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}c=fgetc(fp);while(c!='#'){for(k=O;k<i;k++)if(c==kind[k]){num[k]++;break;}if(k>=i&&kind[k-1]!=c){kind[i]=c;num[i]++;i++;}c=fgetc(fp);}count=i-1;}voidselect(HuffmanTreeHu,intj) /*尋找輸入的字符數據中權值最小的兩個結點 */{intk=1,t;while(Hu[k].parent!=O)k++;for(t=k;t<=j;t++)if(Hu[t].parent==O&&Hu[t].weight<Hu[k].weight)k=t;s仁k;k=1;while(Hu[k].parent!=O||k==s1)k++;for(t=k;t<=j;t++)if(Hu[t].weight<Hu[k].weight&&Hu[t].parent==O&&t!=s1)k=t;s2=k;}voidHuffmanCoding() /*構建哈夫曼樹并編碼*/{voidselect(HuffmanTreeHu,intj);HuffmanTreeHT;HuffmanCodeHC;intm,i,start,f,c;char*cd;FILE*fp;if(count<=1)return;m=2*count-1;HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));for(i=1;i<=count;i++)HT[i].weight=num[i];for(i=1;i<=m;i++){HT[i].parent=O;HT[i].lchild=0;HT[i].rchild=O;}for(i=count+1;i<=m;i++){select(HT,i-1);HT[s1].parent=i;HT[s2].parent=i;HT[i].lchild=s1;HT[i].rchild=s2;HT[i].weight=HT[s1].weight+HT[s2].weight;}HC=(HuffmanCode)malloc((count+1)*sizeof(char*));cd=(char*)malloc(count*sizeof(char));cd[count-1]='\0:for(i=1;i<=count;i++){start=count-1;for(c=i,f=HT[i].parent;f!=O;c=f,f=HT[f].parent)if(HT[f].lchild==c)cd[--start]='0';elsecd[--start]='1';HC[i]=(char*)malloc((count-start)*sizeof(char));strcpy(HC[i],&cd[start]);}if((fp=fopen(”字符代碼.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=1;i<=count;i++){Code[i].data=kind[i];strcpy(Code[i].code,HC[i]);fwrite(&Code[i],sizeof(structcharcode),1,fp);}fclose(fp);}voidTransform() /*將字符文件翻譯成代碼文件 */{FILE*fp1,*fp2,*fp3;inti;charc;if((fp仁fopen(”字符代碼.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp2=fopen(”文本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp3=fopen(”碼本文件.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=0;i<count;i++)fread(&d[i],sizeof(structcharcode),1,fp1);while(!feof(fp2)){c=fgetc(fp2);for(i=0;i<count;i++)if(d[i].data==c){fputs(d[i].code,fp3);break;}}fputc(#,fp3);fclose(fp3);}voidTranslate() /*將代碼文件翻譯成字符文件 */{FILE*fp1,*fp2,*fp3;inti,j,k;charc[20]={'\0'};if((fp仁fopen(”碼本文件.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp2=fopen(”字符代碼.txt","rb"))==NULL){printf("cannotopenthisfile\n");return;}if((fp3=fopen(”破譯文件.txt","wb"))==NULL){printf("cannotopenthisfile\n");return;}for(i=1;i<=count;i++)fread(&d[i],sizeof(structcharcode),1,fp2);i=0;c[i]=fgetc(fp1);while(c[i]!='#'){for(k=1;k<=count;k++)if(!strcmp(c,d[k].code)){fputc(d[k].data,fp3);for(j=0;j<=i;j++)c[j]='\0';i=-1;break;}c[i]=fgetc(fp1);}fputc('#',fp3);fclose(fpl);fclose(fp2);fclose(fp3);}voidprint1(char*name) /*打印文件函數*/{FILE*fp;charc;if((fp=fopen(name,"rb"))==NULL){printf("cannotopenthisfile\n");return;}c=fgetc(fp);while(c!='#'){printf("%c",c);c=fgetc(fp);}}voidprint2(char*name) /*打印文件函數*/{FILE*fp;inti=1;if((fp=fopen(name,"rb"))==NULL){printf("cannotopenthisfile\n");return;}while(!feof(fp)){fread(&e[i],sizeof(structcharcode),1,fp);printf("%c--%s\n",e[i].data,e[i].code);i++;}}voidmain(){Creat_message();printf("您輸入的文本文件是:\n");print1("文本文件.txt");printf("\n");Calculate();HuffmanCoding();
printf(”每個字符相應的哈夫曼編碼是:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 3590-2019食用菌害蟲綠色防控技術規程
- DB31/T 962-2015混凝土模卡砌塊技術要求
- DB31/T 836-2021制冷劑使用技術通則
- DB31/T 1326-2021研學旅行服務規范
- DB31/T 1164-2019快遞末端綜合服務站通用規范
- DB31/T 1129-2019母豬早期妊娠診斷B型超聲波法
- 2025年Web開發職業規劃試題及答案
- 2025年中國北京寫字樓行業市場投資可行性調研報告
- 留學歸國人員產學研一體化勞務合作協議
- 建筑工程項目合同終止與安全生產監管服務協議
- 2025保密法宣傳專題培訓課件
- 班組安全教育試題及答案
- 虎符銅砭刮痧課件
- 《醫療機構工作人員廉潔從業九項準則》解讀
- 水產養殖網箱租賃與飼料供應合作協議
- 2025年全國保密教育線上培訓考試試題庫含答案(新)附答案詳解
- 鋼結構施工 課件項目3 鋼結構工程安裝
- 《神經網絡模型》課件
- 四川省成都外國語2025年高三聯考數學試題科試題含解析
- 后現代思潮與教育
- 四川省樹德中學2025年高三第一次模擬考試(物理試題含解析)
評論
0/150
提交評論