⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffmantree.txt

📁 利用哈夫曼编码进行信息通信可以大大提高信道利用率
💻 TXT
📖 第 1 页 / 共 2 页
字号:

    if((fp1=fopen("test.dat", "r"))==NULL){
                  printf("Cannot open file strike any key exit!00000");
                  getch();
            }
           fscanf(fp1, "%d", &n);
          fclose(fp1);
     if((fp=fopen("hfmtree","rb"))==NULL){
                  printf("Cannot open file strike any key exit!1111");
                  getch();
            }
     for(i=1;i<=2*n-1;i++){
         fread(&ht[i],sizeof(node),1,fp);
     }
     fclose(fp);
    if((fp=fopen("codefile.dat","r"))==NULL){
                         getch();
                         exit(1);
                         }

      cn=0;
    while(fread(&in,sizeof(int),1,fp)!=NULL){
        code[cn]=in;
       cn++; 
}
    fclose(fp);
     printf("\n");
    if((fp=fopen("textfile.txt","w"))==NULL){
                         getch();
                         exit(1);
                         }
    root=2*n-1;
    for(i=0;i<cn;i++){
      /* if(ht[i-1].rch==0)printf("%c",ht[i-1].data); */
       if(i==0) check=root;
       if (code[i]==1){
      check=ht[check].rch;
      if((ht[check].rch==0)&&(ht[check].lch==0)){
               fwrite(&ht[check].data,sizeof(char),1,fp);
               check=root;
               }
       }
       if (code[i]==0){
      check=ht[check].lch;
      if((ht[check].rch==0)&&(ht[check].lch==0)) {
         fwrite(&ht[check].data,sizeof(char),1,fp);
         check=root;
                  }
       }
    }
fclose(fp);
 gotoxy(15,6);
printf("!** DECODING SUCCESSSFULLY  **!");
}
     void preorder(int n ,node p[maxsize]){
           yy[begin].value=p[n].data;
           yy[begin].ad=n;
           begin++;
          if ((p[n].lch!=0)&&(p[n].rch!=0)){
                preorder(p[n].lch,p);
                preorder(p[n].rch,p) ;
          }

     }
void printshu(){
 FILE *fp1,*fp,*fp2;
 int i,n,m,add,length,leng,j;
 struct nodetype ht[maxsize],p[maxsize];
 struct codetype hcd[maxsize];
if((fp1=fopen("test.dat", "r"))==NULL){
                  printf("Cannot open file strike any key exit!00000");
                  getch();
            }
           fscanf(fp1, "%d", &n);
          fclose(fp1);
     if((fp=fopen("hfmtree","rb"))==NULL){
                  printf("Cannot open file strike any key exit!1111");
                  getch();
            }
     for(i=1;i<=2*n-1;i++){
         fread(&ht[i],sizeof(node),1,fp);
     }
     fclose(fp);
     if((fp2=fopen("code","rt"))==NULL){
                  printf("Cannot open file strike any key exit!2222");
                  getch();
            }
     for(i=1;i<=2*n-1;i++){
         fread(&hcd[i],sizeof(code),1,fp2);
     }
    fclose(fp2);
    /*先顺序遍历树*/
    m=2*n-1;
    preorder(m,ht);
    for(i=1;i<=m;i++) {
                    add=yy[i].ad ;
                   yy[i].len=hcd[add].start;
    }
   clrscr();
   leng=yy[1].len;
   for(i=1;i<=m;i++) {
        length=leng-yy[i].len;
        gotoxy(10+length+3,0+i);printf("  %c",yy[i].value);
        while(wherex()<=30) printf("-");
   }
}
void printcode(){
   int t,cn=1,j,i,n;
   char c;
   code hcd[maxsize];
   FILE *fp;
   if((fp=fopen("codefile.dat","r"))==NULL){
                         getch();
                         exit(1);
                         }
    gotoxy(13,2);printf("The code is ......");
    gotoxy(13,4);
     while(fread(&n,sizeof(int),1,fp)!=NULL){
        if (cn%50==0)gotoxy(13,wherey()+1);
        printf("%d",n);
        cn++;
         }
 fclose(fp);
}
void menu(){  /*--------------主菜单------begin-------------------*/

         int i;
         textmode(C80);
        textbackground(2);
        window(6,1,70,5);
        clrscr();
        textbackground(3);
        textcolor(LIGHTBLUE);
        window(8,2,67,4);
        clrscr();
        for(i=0;i<=5;i++){

        gotoxy(i*10+3,2);cprintf("%s",m1[i]);

        }

       gotoxy(3,2);
}

void outwindow(){
        textbackground(2);
        window(6,4,70,25);
        clrscr();
        textbackground(0);
        window(7,5,69,24);
        textcolor(WHITE);
        clrscr();
}
char *m2[]={"From Text","Input Text","Return"};
void choos(int this,int last)
{
lowvideo();
textcolor(LIGHTBLUE);
gotoxy(1,(last-1)+1); cprintf("%s",m2[last-1]);

highvideo();
textcolor(YELLOW);
gotoxy(1,(this-1)+1); cprintf("%s",m2[this-1]);
}
void cmenu(){
    int i,key0,current=1,past=0,key1;

    textbackground(2);
    window(19,4,30,8);
    clrscr();
    textbackground(3);
    textcolor(LIGHTBLUE);
    window(20,5,29,7);
    clrscr();
    gotoxy(1,1);printf("From Text");
    gotoxy(1,2);printf("Input Text");
    gotoxy(1,3);printf("Return");

     while(1)          {

            key0=0;
            key0=getkey();
            while(key0!=45&&key0!=28){
               key1=getkey();
               if (key1==80||key1==72)
                  {
                     past=current;
                    if(key1==72) current=(current==1)?3:current-1;
                    if(key1==80) current=(current==3)?1:current+1;
                    if(key1==45) exit(0);
                    choos(current,past);
                   }
              else if(key1==45) exit(0);
              else if ((current==3)&&key1==28) break;
                                        else if ((current==1)&&key1==28) {
                            outwindow();coding();
                            gotoxy(1,20);
                            printf("please input any key to continue.....");
                                          getch();
                                          clrscr();
                                          menu();
                                           break; }
 else if ((current==2)&&key1==28) {
                             outwindow();coding2();
                             gotoxy(1,20);
                             printf("please input any key to continue.....");
                             getch();
                             clrscr();
                             menu();
                             break;
}
            }
   break;
}
 }
void main(){
        char ch;
        int i,key0,current=1,past=0,key1;
        outwindow();
        menu();

       while(1)
{

key0=0;
key0=getkey();

while(key0!=45&&key0!=28)
{
key1=getkey();
if (key1==77||key1==75)
{
past=current;
if(key1==75) current=(current==1)?6:current-1;
if(key1==77) current=(current==6)?1:current+1;
if(key1==45) exit(0);
choosing(current,past);
}
else if(key1==45) exit(0);else if ((current==6)&&key1==28) exit(0);
 else  if ((current==1)&&key1==28) {
outwindow();
buildtree();
            gotoxy(1,20);
         cprintf("please input any key to continue.....");
          getch();
          clrscr();
          menu(); }
                
 else  if ((current==3)&&key1==28) {
outwindow();decoding();
                  gotoxy(1,20);
              printf("please input any key to continue.....");
                                getch();
                                clrscr();
                                menu();}
     else if ((current==4)&&key1==28) {outwindow();printcode();

                 gotoxy(1,20);printf("please input any key to continue.....");
                                getch();
                                clrscr();
                                menu();}
             else if ((current==5)&&key1==28) {outwindow();printshu();

                 gotoxy(1,20);printf("please input any key to continue.....");
                                getch();
                                clrscr();
                                menu();}
           else if ((current==2)&&key1==28) { cmenu();outwindow();menu();
                                /*gotoxy(1,20);
                printf("please input any key to continue.....");
                                getch();
                                clrscr();*/
                                }
}
}
getch();
exit(0);
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -