#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "ctype.h"
/********************全局变量定义部分***************************/
int n;
FILE *fp;
char tmpcode[50];
int t=0;
/*************************结点定义***************************/
struct node{
int w;
int flag;
char c;
struct node *plink,*llink,*rlink;
char code[50];
}*num[100],*root;
/*************************函数声明********************/
void Initialization(void); /*建立树*/
void Encoding(void); /*对文件编码*/
void Decoding(void); /*译码*/
void Print(void); /*印制代码文件*/
char DrawScreen(); /*菜单显示*/
void WriteHfmTree(struct node *);/*将编码写入HfmTree文件*/
void GetCode(struct node *); /*建立每一个字符的编码*/
/********************主函数从这里开始*****************/
void main()
{
char c;
root=(struct node*)malloc(sizeof(struct node)); /*初始化根节点*/
do
{
system("cls");
clrscr();
c=DrawScreen();
switch(c)
{
case 'i':;
case 'I':Initialization();;break;
case 'e':;
case 'E':Encoding();break;
case 'd':;
case 'D':Decoding();break;
case 'p':;
case 'P':Print();break;
case 't':;
case 'T':Print();break;
case 'q':;
case 'Q':exit(0);break;
default :printf("Input Error!/n");
}
}while(c!='q'&&c!='Q');
}
/*************************DrawScreen***************************/
char DrawScreen()
{
int i;
char *f[]={"
Initialization.", "[E] Encoding.", "[D] Decoding.","[P] Print.", "[T] Tree printing.", "[Q] Quit."}; clrscr(); textcolor(LIGHTRED); textbackground(DARKGRAY); window(0, 0, 80, 45); clrscr(); for(i=0; i<6; i++) { gotoxy(30, i+10); cputs(f[i]); } printf("/n"); gotoxy(20, i+10); cputs("Please input your choice:"); return (getchar());}/*************************Initialization***************************/void Initialization(void){ int i,j,k; struct node *p1,*p2,*tmp,*p; puts("Input the number of the code:"); scanf("%d",&n); while(getchar()!='/n') continue; for(i=0;i<n;i++) { p=(struct node *)malloc(sizeof(struct node)); puts("Please input a charactor:"); scanf("%c",&p->c); while(getchar()!='/n') continue; puts("Please input it/'s size:"); scanf("%d",&p->w); while(getchar()!='/n') continue; p->plink=NULL; p->rlink=NULL; p->llink=NULL; num[i]=p; } for(i=0;i<n-1;i++) /*按照权重从小到大选择排序*/ { for(j=i+1;j<n;j++) { if(num[i]->w>num[j]->w) { tmp=num[i]; num[i]=num[j]; num[j]=tmp; } } } /*******************************开始建立树***********************/ num[n]=NULL; /*结束标志 */ k=n; while(num[1]!=NULL) { p=(struct node *)malloc(sizeof(struct node)); p1=num[0]; p2=num[1]; p->llink=p1; p->rlink=p2; p->plink=NULL; p1->plink=p; p2->plink=p; p->w=p1->w+p2->w; for(i=1;i<k;i++) { num[i]=num[i+1]; } k--; num[0]=p; for(i=0;i<k-1;i++) /* 排序 */ { for(j=i+1;j<k;j++) { if(num[i]->w>num[j]->w) { tmp=num[i]; num[i]=num[j]; num[j]=tmp; } } } } root=num[0]; if((fp=fopen("hfmTree.hfm","wb"))==NULL) /*写入文件*/ { puts("File open error!"); getchar(); exit(0); } GetCode(root); WriteHfmTree(root); fclose(fp); }/*************************GetCode***************************/ void GetCode(struct node *p) { if(p->llink==NULL&&p->rlink==NULL) { tmpcode[t]='/0'; strcpy(p->code,tmpcode); } else { tmpcode[t++]='0'; /*递归调用*/ GetCode(p->llink); t--; tmpcode[t++]='1'; GetCode(p->rlink); t--; } }/*************************WriteHfmTree***************************/ void WriteHfmTree(struct node *p) { if(p->llink==NULL&&p->rlink==NULL) { fputc('(',fp); fputc(p->c,fp); fputs(p->code,fp); fputc(')',fp); } else { WriteHfmTree(p->llink); WriteHfmTree(p->rlink); } }/*************************Encoding***************************/ void Encoding(void) { FILE *fp1,*fp2,*fp3; char ch1,ch2,c; if((fp1=fopen("hfmTree.hfm","rb"))==NULL) { puts("fp1 File open error!"); getchar(); exit(0); } if((fp2=fopen("tobetrans.txt","rb"))==NULL) { puts("fp2 File open error!"); getchar(); exit(0); } if((fp3=fopen("CodeFile.hfm","wb"))==NULL) { puts("fp3 File open error!"); getchar(); exit(0); } printf("/nCode From File is:/n"); while((ch1=fgetc(fp2))!=EOF) { t=0; printf("%c->",ch1); while((ch2=fgetc(fp1))!=EOF) { if(ch1==ch2) { while((c=fgetc(fp1))!=')') { tmpcode[t++]=c; } tmpcode[t]='/0'; fputs(tmpcode,fp3); printf("%s./n",tmpcode); fputc('-',fp3); rewind(fp1); break; } } } getchar(); printf("/n/nPress any key to continue..."); getchar(); fclose(fp1); fclose(fp2); fclose(fp3); } /************************Decoding*************************/ void Decoding(void) { FILE *fp1,*fp2,*fp3; char ch1,ch2,ch3; char temp_3[20]; char temp_1[20]; int t1,t3; if((fp1=fopen("hfmTree.hfm","rb"))==NULL) { puts("File open error!"); getchar(); exit(0); } if((fp2=fopen("TextFile.txt","wb"))==NULL) { puts("File open error!"); getchar(); exit(0); } if((fp3=fopen("CodeFile.hfm","rb"))==NULL) { puts("File open Error!"); getchar(); exit(0); } printf("/nAfter Decode the result is:/n"); while((ch3=fgetc(fp3))!=EOF) { t3=0; while(ch3!='-') { temp_3[t3++]=ch3; ch3=fgetc(fp3); } temp_3[t3]='/0'; while((ch1=fgetc(fp1))!=EOF) { if(isalpha(ch1)) { ch2=ch1; t1=0; while((ch1=fgetc(fp1))!=')') { temp_1[t1++]=ch1; } temp_1[t1]='/0'; if(strcmp(temp_1,temp_3)==0) { fputc(ch2,fp2); printf("%c",ch2); rewind(fp1); break; } } } } getchar(); printf("/n/nPress any key to continue..."); getchar(); fclose(fp1); fclose(fp2); fclose(fp3); } /*************************************************/ void Print(void) { FILE *fp1,*fp2; char ch1,ch2; char tmp[20]; int t; if((fp1=fopen("hfmTree.hfm","rb"))==NULL) { puts("File open error!"); getchar(); exit(0); } if((fp2=fopen("hfmcode.txt","wb"))==NULL) { puts("File open error!"); getchar(); exit(0); } while((ch1=fgetc(fp1))!=EOF) { if(ch1=='(') { t=0; ch1=fgetc(fp1); ch2=ch1; while((ch1=fgetc(fp1))!=')') { tmp[t++]=ch1; } tmp[t]='/0'; printf("%c-----%s/n",ch2,tmp); fputc(ch2,fp2); fputc('-',fp2); fputc('-',fp2); fputc('-',fp2); fputs(tmp,fp2); fputc('/n',fp2); } } getchar(); getchar(); printf("Press any key to continue..."); getchar(); fclose(fp1); fclose(fp2); }