|
[code:1]
/****************************************************
smartcode.c ---used to code a series of chars.
Writer: Longwen Date :08/06/04
****************************************************/
#include <stdio.h>
#include <string.h>
#define MAXLINE 50
struct codenode
{
char ch;
char *code;
};
struct htnode
{
int times;//how many times it appeared in your input text
char ch; //the char
int parent,lchild,rchild;
};
void select (struct htnode *,int,int *,int *);
struct codenode *code(const char *,const char *);
void codeprint(struct codenode *,int,const char *);
int main(void)
{
char text[MAXLINE]; //how many chars you can input
char diff[MAXLINE]; //the number of different chars
printf("Input yur words,50 at most(an empty line to quit):\n");
while(fgets(text,MAXLINE,stdin)!=NULL && text[0]!='\n')
{
int count =0;
text[strlen(text)-1]=0;
int i=0,j=0;
/*To find and store the different char*/
for(;i<strlen(text);i++)
for(;j<count;j++)
if(text[i]==diff[j])
j=count; //to out the loop and test the next char
diff[count++]=text[i];
struct codenode *map= code(diff,text); //code
codeprint(map,strlen(diff) ,text);
//print the code of each different char
for(i=0;i<strlen(diff);++i)
free(map[i].code);
free(map);
}
return 0;
}
struct codenode *code (const char *diff,const char *text)
{
int len=strlen(text);
int n=strlen(diff);
int m=2n-1;
struct codenode *codemap; //to store the code of each different char
int i;
if (((struct htnode *HT)=(struct htnode*)malloc(sizeof(htnode)*m))==NULL)
{
fprintf(stderr,"ERROR!\t$-3-$\n");
exit(3);
}
//to init the HT
for(i=0;i<n;++i)
HT[i]={0,diff[i],-1,0,0};
for(;i<m;++i)
HT[i]={0,0,-1,0,0};
int j=0;//count the times each char appeares
for(i=0;i<n;++i)
for(;j<len;++j)
if(diff[i]==text[j])
++HT[i].times;
int s1,s2;
for(i=n;i<m;++i)
{
select(HT,i-1,&s1,&s2);
HT[s1].parent=HT[2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].times=HT[s1].times+HT[s2].times;
}
if((struct codenode *codemap=(struct codenode *)malloc(sizeof(struct codenode)*n))==NULL)
{
fprintf("ERROR!\t$-4-$\n");
exit(4);
}
if(((char *cd)=(char *)malloc(sizeof(char)*n))==NULL)
exit(5);
cd[strlen(diff)-1]=0;
i=0;
for(;i<n;++i)
{
int start=n-1;
int c,f;
for(c=i,f=HT[i].parent;f!=-1;c=f,f=HT[f].parent)
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
codemap[i].ch=HT[i].ch;
if((codemap[i].code=(char *)malloc(sizeof(char)*(n-start)))==NULL)
exit(6);
strcpy(codemap[i].code,&cd[start]);
}
free(cd);
free(HT);
return ((struct node *)codemap);
}
void codeprint(struct codenode *codemap,int m,const char *text)
{
puts("CHAR\t\tCODE");
int i=0,j=0;
while(i++<m)
printf("%4c\t\t%s\n",codemap[i].ch,codemap[i].code);
puts("Your text's code are:\n")
for(i=0;i<strlen(text);++i)
{
for(j=0;j<m;++j)
if(text[i]==codemap[j].ch)
printf("%s",codemap[j].code);
}
}
void select(struct htnode *ht,int n,int *s1,int *s2)
{
int i;
*s1=*s2=0;
for(i=0;i<n;++i)
{
if(ht[*s1].times>ht[i].times && ht[i].parent==-1)
*s1=i;
}
for(i=0;i<n;++i)
{
if(ht[*s2].times>ht[i].times && i!=*s1 && ht[i].parent==-1)
*s2=i;
}
}
[/code:1][/code] |
|