求问关于信息论与编码方面的课程的设计,要求用c++语言进行编写,主要是哈夫曼方面的问题,来点人帮帮我啊
请问是什么课题呢?
Shannon 编码
1.实验目的
(1)进一步熟悉Shannon 编码过程;
(2)掌握C++程序设计和调试过程中数值的进制转换,数值与字符串之间的转换等技术。
2.实验内容
(1)输入:信源符号个数 ,每个信源符号的概率分布P从键盘输入
(2)输出:每个信源符号对应的Shannon 编码的码字
#include<iostream>
#include<vector>
#include<string>
#include <math.h>
using namespace std;
int main(){
cout<<"请输入信源符号个数:"<<endl;
int n,i;
cin>>n;
cout<<"请输入符号分布概率"<<endl;
vector<float> p,p1;
vector<int> l;
float s=0;
for(int i=0;i<n;i++){
p1.push_back (s);
float a;
cin>>a;
double b=-log(a)/log(2);
l.push_back((int)b+1);
p.push_back(a);
s+=a;
}
for(int j=0;j<n;j++){
string str="";
float c=p1[j];
for(int k=0;k<l[j];k++){
if(c*2<1){
str+="0";
c=c*2;
}else{
str+="1";
c=c*2-1;
}
}
cout<<"第"<<j<<"个信源符号的二元码是:"<<str<<endl;
}
return 0;
}
提供参考实例思路【Shannon 香农编码 信息论实验 c++】,链接:https://blog.csdn.net/weixin_43620061/article/details/103378190
【该实例讲解的较为详细和系统,应该对你有所帮助】
#include <iostream>
using namespace std;
#include<string.h>
#include<stdlib.h>
//5 1 29 2 7 3 8 4 14 5 23 6 3 7 11 8输入
struct node {
int element;
int num;
char mima[100];
char ch;
struct node *Lchild, *Rchild;
};
struct tree {
struct node *root;
};
tree start;
void MakeTree(tree *tr, int x, char c, tree *Lt, tree *Rt) {
node *p = (struct node*)malloc(sizeof(struct node));
p->ch = c;
p->element = x;
p->Lchild = Lt->root;
p->Rchild = Rt->root;
Lt->root = Rt->root = NULL;
tr->root = p;
}
void Xunzhao(tree tree[], int *t1, int *t2, int t) {
int min1, min2, k;
if (tree[0].root->element > tree[1].root->element) {
*t1 = 0;
*t2 = 1;
min1 = tree[0].root->element;
min2 = tree[1].root->element;
} else if (tree[0].root->element < tree[1].root->element) {
*t1 = 1;
*t2 = 0;
min1 = tree[1].root->element;
min2 = tree[0].root->element;
}
for (k = 2; k < t; k++) {
if (tree[k].root->element < min1) {
*t2 = *t1;
*t1 = k;
min2 = min1;
min1 = tree[k].root->element;
} else if (tree[k].root->element < min2) {
*t2 = k;
min2 = tree[k].root->element;
}
}
}
tree MakeHaffuma(int w[], char jie[], int n) {
tree zero, ch[1000];
int i, k1, k2, k;
tree *p;
p = &zero;
p->root = NULL;
for (i = 0; i < n; i++)
MakeTree(&ch[i], w[i], jie[i], p, p);
for (k = n - 1; k > 0; k--) {
Xunzhao(ch, &k1, &k2, k);
MakeTree(&ch[k1], ch[k1].root->element + ch[k2].root->element, '$', &ch[k1], &ch[k2]);
ch[k2] = ch[k];
}
return ch[0];
}
void makenode(node *no) {
int i, j;
if (no == start.root) {
for (i = 0; i < 100; i++)
start.root->mima[i] = '/0';
start.root->num = 0;
}
if ((no->Lchild == NULL) && (no->Rchild == NULL)) {
cout<<no->ch;
for (j = 0; j < no->num; j++)
cout<<no->mima[j];
cout<<endl;
}
if (no->Lchild != NULL) {
for (j = 0; j < no->num; j++)
no->Lchild->mima[j] = no->mima[j];
no->Lchild->mima[no->num] = '0';
no->Lchild->num = no->num + 1;
makenode(no->Lchild);
}
if (no->Rchild != NULL) {
for (j = 0; j < no->num; j++)
no->Rchild->mima[j] = no->mima[j];
no->Rchild->mima[no->num] = '1';
no->Rchild->num = no->num + 1;
makenode(no->Rchild);
}
}
void translate(node *n, char p[]) {
char s[100];
node *q = n;
int len, i, k = 0, max;
len = strlen(p);
for (i = 0; i < len; i++) {
if (p[i] == '0')
if (q->Lchild)
q = q->Lchild;
else cout<<"ERROR!";
else {
if (q->Rchild)
q = q->Rchild;
else cout<<"ERROR!";
}
if ((q->Lchild == NULL) && (q->Rchild == NULL)) {
s[k++] = q->ch;
max = i;
q = n;
}
}
if (max != len - 1) cout<<"ERROR!";
else {
for (i = 0; i < k; i++)
cout<<s[i]<<":";
}
cout<<endl;
}
void Print(node *node) {
if (node) {
cout<< node->ch;
cout<<node->element;
Print(node->Lchild);
Print(node->Rchild);
}
}
int main() {
cout<<"欢迎来到哈夫曼编译系统!"<<endl;
int w[500];
char s[500];
char p[500];
char choice = '1';
int n, i;
while (choice != 3) {
cout<<"1创建 2编译 0退出\n";
cin>>choice;
switch (choice) {
case '1': {
cout<<"请输入树节点的个数:";
cin>>n;
cout<<"请输入权值及其对应的节点:"<<endl;
for (i = 0; i < n; i++)
cin>>w[i]>>s[i];
// cin>>s[i];
start = MakeHaffuma(w, s, n);
makenode(start.root);
break;
}
case '2': {
if (start.root == NULL)
cout<<"未构建哈夫曼树!";
else {
translate(start.root, p);
cout<<endl;
}
break;
}
case '3':
cout<<"程序退出!";
break;
}
return 0;
}
}