国内最特色酒店:程序高手进~~~答出献一个吻

来源:百度文库 编辑:中科新闻网 时间:2024/05/05 13:39:39
#include <stdio.h>
#include <stdlib.h>

#ifdef __cplusplus
#include <conio.h>
#include <ctype.h>
#include <string.h>
#endif /*__cplusplus*/

/*树结构和全局结构指针*/
struct node
{
int num;/*结点编号*/
char c;
int weight;
int parent;
int lchild,rchild;
} * ht;

/*常量文件名*/
const char *TableFileName = "HfmTbl.txt";
const char *SourceFileName = "SrcText.txt";
const char *CodeFileName = "HfmCode.txt";
const char *DecodeFileName = "DecodeText.txt";
const char *TreeViewFileName = "TreeView.txt";
/*打印格式字符串常量*/
const char *PrintFormatStr = "%4d %13c %13d %13d %13d %13d\r\n";
/*读取格式字符串常量*/
const char *ReadFormatStr = "%d %c %d %d %d %d";
/*打印参数宏*/
#define PRINT_PARAM(i) ht[(i)].num,ht[(i)].c,ht[(i)].weight,\
ht[(i)].parent,ht[(i)].lchild,ht[(i)].rchild
/*读取参数宏*/
#define READ_PARAM(i) &ht[(i)].num,&ht[(i)].c,&ht[(i)].weight,\
&ht[(i)].parent,&ht[(i)].lchild,&ht[(i)].rchild
/*打印格式和参数宏*/
#define READ_FORMAT_PARAM(i) ReadFormatStr,READ_PARAM(i)
/*读取格式和参数宏*/
#define PRINT_FORMAT_PARAM(i) PrintFormatStr,PRINT_PARAM(i)

/*内存交换函数,用于结构体变量交换*/
void MemSwap(void *buf1, void *buf2, size_t buflen)
{
if(buf1!=buf2)
{
void *p = malloc(buflen);
memmove(p,buf1,buflen);
memmove(buf1,buf2,buflen);
memmove(buf2,p,buflen);
free(p);
}
}

/*打印表*/
void PrintHT(int from, int to)
{
int i;
for(i=from;i<=to;i++)
{
printf(PRINT_FORMAT_PARAM(i));
}
printf("\n");
}

/*选择法排序*/
void SelectSort(int from, int to)
{
int i,j,k;
for(i=from;i<to;i++) /*sort */
{
for(k=i,j=i+1;j<=to;j++)
if(ht[j].weight<ht[k].weight)
k=j;
if(k!=i)
MemSwap(&ht[k],&ht[i],sizeof(struct node));
PrintHT(from,to);
}
}

/*释放ht*/
void free_ht()
{
if(ht != NULL)
{
free(ht);
ht = NULL;
}
}

/*从文件读取数据保存到全局堆中,调用方要记得释放*/
int ReadFromFile()
{
int i;
int m;
FILE *h;

if((h=fopen(TableFileName,"r"))==NULL)
{
printf("cannot open %s\n", TableFileName);
getch();
return 0;
}
fscanf(h,"%d",&m);
free_ht();
ht=(struct node *)calloc(m+1,sizeof(struct node));
for(i=1;i<=m;i++)
{
fscanf(h,READ_FORMAT_PARAM(i));
}
fclose(h);
return m;
}

/*吃掉无效的垃圾字符*/
void EatCharsUntilNewLine()
{
while(getchar()!='\n')
continue;
}
/*按节点序号重新排序*/
void SortByNum(int m)
{
int i = 0, j;
size_t len = sizeof(struct node);
for(i = 1; i < m; i ++)
{
for(j = i + 1; j <= m; j ++ )
{
if (ht[j].num == i)
{
MemSwap(&ht[i],&ht[j],len);
break;
}
}
}
}

/*初始化并写入文件*/
void Initialize()
{
int i=0,x=0,y=0,n,m;
FILE *h;

puts("input the number of the total char n:");
scanf("%d",&n);
EatCharsUntilNewLine();

m=2*n-1;
ht=(struct node *)calloc(m+1,sizeof(struct node));

哈夫曼编译码器吧~~~~~~~~~~

寒....这个15天里肯定每人回答....你献身体都没用撒..=.#~

你是个女的吧?你的名字这么像我同学的呢?
你想问什么问题?程序运行结果?