生成以下代码的流程图void PrintHUffmancode(HuffmanCode H) //将文件中字符的哈夫曼编码打印出来并将其写入指定txt文件 { FILE *fp; char ch; char Filename[80]; char file[80]; printf ("output the Huffmancode of which file:"); scanf("%s",Filename); if((fp=fopen(Filename,"r"))==NULL) printf("failda\n"); ch = fgetc(fp); int L =0; printf("1"); while(ch != EOF) { for(int i = 0; i<n; i++) { if(H[i].ch == ch) { printf("%s",H[i].bits); sprintf(s+L,"%s",H[i].bits); L=strlen(s); } } ch = fgetc(fp); } printf("\n"); for(int k =0;k<n;k++) { printf("%c-%s\n",H[k].ch, H[k].bits); } // printf("3\n"); fclose(fp); printf("stand by\n"); PHUM(file,s); }
时间: 2023-12-11 21:02:31 浏览: 151
以下是 PrintHUffmancode 函数的流程图:
```
Start
|
Input Filename
|
Open File
|
Read Character from File
|
While Character is not EOF
| |
| Loop through HuffmanCode H
| | |
| | If H[i].ch is equal to Character
| | | |
| | | Print H[i].bits
| | | |
| | | Append H[i].bits to s
| | | |
| | | Update L
| | | |
| | End If
| End Loop
| |
| Read Next Character from File
End While
|
Print Huffmancode of each character
|
Close File
|
Output "stand by"
|
Call PHUM with file and s as arguments
|
End
```
相关问题
对以下代码进行细致注释#pragma once #include <map> #include <iomanip> //哈夫曼树存储结构 struct HuffmanNode { int weight; //结点字符和权值 int parent, lchild, rchild; }; class Huffman { private: map<char, int> code; map<string, char> sc_code; map<char, string> cs_code; const int MAXWEIGHT = 2147483647; HuffmanNode* hf = NULL; int N = 0; public: //无参构造 Huffman(); //初始化 void Initialization(); //选择 void select(HuffmanNode*& hf, int n, int& s1, int& s2); //创建Huffman树 void createHuffmanTree(HuffmanNode*& hf, int n); //Huffman编码实现 char** createHuffmanCode(HuffmanNode*& hf, int n); //编码 void Encoding(); //译码 void Decoding(); //印代码文件 void Print(); //印哈夫曼树 void TreePrinting(); };
// 防止头文件重复引用
#pragma once
// 使用map需要引入的头文件
#include <map>
// 用于输出格式化的头文件
#include <iomanip>
// 定义哈夫曼树的结构体
struct HuffmanNode {
int weight; // 结点字符和权值
int parent, lchild, rchild; // 结点父节点、左孩子节点和右孩子节点
};
// 定义哈夫曼编码的类
class Huffman {
private:
// 原始字符 -> 权值的映射
map<char, int> code;
// 压缩后的二进制位 -> 原始字符的映射
map<string, char> sc_code;
// 原始字符 -> 压缩后的二进制位的映射
map<char, string> cs_code;
// 定义最大权值
const int MAXWEIGHT = 2147483647;
// 定义哈夫曼树的指针
HuffmanNode* hf = NULL;
// 定义哈夫曼树的结点数目
int N = 0;
public:
// 无参构造函数
Huffman();
// 初始化哈夫曼编码
void Initialization();
// 选择权值最小的两个结点
void select(HuffmanNode*& hf, int n, int& s1, int& s2);
// 创建哈夫曼树
void createHuffmanTree(HuffmanNode*& hf, int n);
// 实现哈夫曼编码
char** createHuffmanCode(HuffmanNode*& hf, int n);
// 编码
void Encoding();
// 译码
void Decoding();
// 输出压缩后的代码文件
void Print();
// 输出哈夫曼树的结构
void TreePrinting();
};
C语言采用一维数组完成哈夫曼编码HuffmanTree HT,HuffmanCode &HC添加注释 任务:根据给定的若干权值来构造哈夫曼树,实现对应的哈夫曼编码以及译码。 功能要求: 1)按传输文本中字符的个数以及字符的频率来建立并输出哈夫曼树的存储结构; 2)设计并输出各字符对应的哈夫曼编码; 3)将传输的文本转换成对应的哈夫曼编码01序列; 将哈夫曼编码01序列翻译成原来的文本字符。
以下是C语言实现哈夫曼编码的代码,其中包含注释:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000 // 定义最大编码长度
typedef struct {
int weight; // 权值
int parent, lchild, rchild; // 双亲和左右孩子结点
} HTNode, *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT, int n, int *s1, int *s2) {
// 在前n个结点中选择权值最小的两个结点s1和s2
int i, min;
for (i = 1; i <= n; i++) {
if (HT[i].parent == 0) {
min = i;
break;
}
}
for (i = 1; i <= n; i++) {
if (HT[i].parent == 0 && HT[i].weight < HT[min].weight) {
min = i;
}
}
*s1 = min;
for (i = 1; i <= n; i++) {
if (HT[i].parent == 0 && i != *s1) {
min = i;
break;
}
}
for (i = 1; i <= n; i++) {
if (HT[i].parent == 0 && HT[i].weight < HT[min].weight && i != *s1) {
min = i;
}
}
*s2 = min;
}
void CreateHuffmanTree(HuffmanTree *HT, int n) {
// 由n个权值为w的结点构建哈夫曼树HT
if (n <= 1) {
return;
}
int m = 2 * n - 1;
*HT = (HuffmanTree) malloc((m + 1) * sizeof(HTNode));
int i;
for (i = 1; i <= m; i++) {
(*HT)[i].parent = 0;
(*HT)[i].lchild = 0;
(*HT)[i].rchild = 0;
}
for (i = 1; i <= n; i++) {
scanf("%d", &((*HT)[i].weight));
}
int s1, s2;
for (i = n + 1; i <= m; i++) {
Select(*HT, i - 1, &s1, &s2);
(*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;
}
}
void CreateHuffmanCode(HuffmanTree HT, HuffmanCode *HC, int n) {
// 根据哈夫曼树HT求出n个字符的哈夫曼编码HC
*HC = (HuffmanCode) malloc((n + 1) * sizeof(char *));
char *cd = (char *) malloc(n * sizeof(char));
cd[n - 1] = '\0';
int i, c, f;
for (i = 1; i <= n; i++) {
int start = n - 1;
for (c = i, f = HT[i].parent; f != 0; c = f, f = HT[f].parent) {
if (HT[f].lchild == c) {
cd[--start] = '0';
} else {
cd[--start] = '1';
}
}
(*HC)[i] = (char *) malloc((n - start) * sizeof(char));
strcpy((*HC)[i], &cd[start]);
}
free(cd);
}
void HuffmanCoding(HuffmanTree HT, HuffmanCode HC, int n) {
// 输入n个字符的权值,建立哈夫曼树HT,并求出n个字符的哈夫曼编码HC
CreateHuffmanTree(&HT, n);
CreateHuffmanCode(HT, &HC, n);
}
void HuffmanDecoding(HuffmanTree HT, int n) {
// 将01序列翻译成原来的文本字符
printf("请输入01序列:\n");
char str[MAX];
scanf("%s", str);
int i, c = 2 * n - 1;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == '0') {
c = HT[c].lchild;
} else {
c = HT[c].rchild;
}
if (HT[c].lchild == 0 && HT[c].rchild == 0) {
printf("%d", c);
c = 2 * n - 1;
}
}
}
int main() {
int n;
printf("请输入字符的个数n:\n");
scanf("%d", &n);
HuffmanTree HT;
HuffmanCode HC;
HuffmanCoding(HT, HC, n);
int i;
printf("各字符的哈夫曼编码如下:\n");
for (i = 1; i <= n; i++) {
printf("%d %s\n", i, HC[i]);
}
HuffmanDecoding(HT, n);
return 0;
}
```
以上是C语言实现哈夫曼编码的代码,可以根据输入的字符权值构建哈夫曼树,并输出各字符对应的哈夫曼编码。同时,可以将传输的文本转换成对应的哈夫曼编码01序列,并将哈夫曼编码01序列翻译成原来的文本字符。
阅读全文