资源简介
Huffman编码和算术编码的C++实现,分别提供了读取文档和手动输入两种输入方式。
代码片段和文件信息
// suanshu.cpp : Defines the entry point for the console application.
//
#include “stdafx.h“
#include
#include
#include
#include “Arithmetic.h“
using namespace std;
//输入字符
void Arithmetic::get_charactor()
{
int i;
cout << “请输入字符集大小n(n>1):“ << endl;
cin >>count;
//if (charactors != NULL) //清零
// delete charactors;
charactors = new ArithmeticCharators[count]; //申请动态存储空间,0号单元未使用
cout << “请输入字符和概率:“ << endl;
for ( i = 0; i < count; i++)
{
cin.ignore(); //清除输入缓冲区
cin.get(charactors[i].c); //输入单个字符,可以是空白符
cin >> charactors[i].chance;
}
}
//输入待编码字符串
void Arithmetic::get_code()
{
cout<<“please input the code‘‘s length:“;
cin>>length;
while(length>=M)
{
cout<<“the length is too largerplease input a smaller one.“;
cin>>length;
}
for(int i=0;i {
cin>>input[i];
}
}
//打印字符和待编码字符串
void Arithmetic::PrintC()
{
for(int i = 0;i {
cout << charactors[i].c << “;“;
cout << “出现次数:“ << charactors[i].weight << “;“;
cout << “出现概率:“ << charactors[i].chance << “;“;
cout << endl;
}
for( int i = 0;i cout << input[i] ;
cout << endl;
cout << count << endl;
cout << length << endl;
}
/*
//从文件读入原文
void Arithmetic::ReadTextFromFile(char *filename)
{
ifstream infile(filename);
if(!infile)
{
cerr << “无法打开文件!“ < return;
}
char a;
while(infile.get(a))
{
text += a;
}
}*/
//输出文本串
void Arithmetic::PrintText()
{
cout << text << endl;
}
//将原文中出现过的字符存入charactors[].c并统计各字符的权值相应的存入charactors[].weight
//然后计算概率存入charactors[].chance
void Arithmetic::CountCharactorsChance()
{
if (text.empty())
return;
//if (charactors!= NULL)
// delete charactors;
int i = 0;
n = 0;
charactors = new ArithmeticCharators[1];
charactors[0].c = text[i];
charactors[0].weight = 1;
input[0] = text[0];
++n;
for (i = 1; i != text.size(); i++)
{
int j;
input[i] = text[i];
for (j = 0; j < n;j++) //遍历当前字符表,如果已存在该字符,权值+1
{
if (text[i] == charactors[j].c)
{
++charactors[j].weight;
j--;
break;
}
}
if (j == n) //该字符不存在,添加该字符
{
++n;
PCharactors newchars = new ArithmeticCharators[n];
memcpy(newchars charactors (n-1) * sizeof(ArithmeticCharators));
delete charactors;
charactors = newchars;
charactors[n-1].c = text[i];
charactors[n-1].weight = 1;
}
}
for(i = 0; i <= n; i++)
{
charactors[i].chance = charactors[i].weight/float(text.size());//C++中整数除以整数结果默认是整数,因此需将一个数强行转换
}
length = text.size();
count = n;
}
void Arithmetic::coding()
{
int ij=0;
for(i=0;i if(input[0]==charactors[i].c) break;
while(j Low+=charactors[j++].chance;
R=charactors[j].chance;
High=Low+R;
for(i=1;i for(j=0;j {
if(input[i]==charactors[j].c)
{
if(j==0)
{
low=Low;
high=Low+charactors[j].chance*R;
High=h
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5354 2011-12-21 14:53 arithmetic\arithmetic\arithmetic.cpp
文件 1008 2011-12-16 14:06 arithmetic\arithmetic\Arithmetic.h
文件 4773 2011-12-07 15:17 arithmetic\arithmetic\arithmetic.vcproj
文件 1407 2011-12-21 19:51 arithmetic\arithmetic\arithmetic.vcproj.PP-PC.PP.user
文件 16 2011-12-21 14:54 arithmetic\arithmetic\code.txt
文件 663 2011-12-16 12:29 arithmetic\arithmetic\Debug\arithmetic.exe.em
文件 728 2011-12-16 12:29 arithmetic\arithmetic\Debug\arithmetic.exe.em
文件 621 2011-12-21 15:18 arithmetic\arithmetic\Debug\arithmetic.exe.intermediate.manifest
文件 69520 2011-12-21 14:53 arithmetic\arithmetic\Debug\arithmetic.obj
文件 3211264 2011-12-16 11:17 arithmetic\arithmetic\Debug\arithmetic.pch
文件 7170 2011-12-21 15:18 arithmetic\arithmetic\Debug\BuildLog.htm
文件 80655 2011-12-16 11:17 arithmetic\arithmetic\Debug\Huffman.obj
文件 32790 2011-12-21 15:18 arithmetic\arithmetic\Debug\main.obj
文件 67 2011-12-21 15:18 arithmetic\arithmetic\Debug\mt.dep
文件 12333 2011-12-16 11:17 arithmetic\arithmetic\Debug\stdafx.obj
文件 297984 2011-12-21 15:18 arithmetic\arithmetic\Debug\vc90.idb
文件 290816 2011-12-21 15:18 arithmetic\arithmetic\Debug\vc90.pdb
文件 6165 2011-12-09 22:01 arithmetic\arithmetic\Huffman.cpp
文件 1504 2011-12-16 11:14 arithmetic\arithmetic\Huffman.h
文件 2096 2011-12-21 15:18 arithmetic\arithmetic\main.cpp
文件 1320 2011-12-04 21:50 arithmetic\arithmetic\ReadMe.txt
文件 8 2011-12-21 14:54 arithmetic\arithmetic\resulttext.txt
文件 297 2011-12-04 21:50 arithmetic\arithmetic\stdafx.cpp
文件 320 2011-12-04 21:50 arithmetic\arithmetic\stdafx.h
文件 765 2011-12-04 21:50 arithmetic\arithmetic\targetver.h
文件 8 2011-12-09 10:22 arithmetic\arithmetic\text.txt
文件 1862656 2011-12-21 19:51 arithmetic\arithmetic.ncb
文件 896 2011-12-04 21:50 arithmetic\arithmetic.sln
..A..H. 23552 2011-12-21 19:51 arithmetic\arithmetic.suo
文件 60928 2011-12-21 15:18 arithmetic\Debug\arithmetic.exe
............此处省略11个文件信息
- 上一篇:21点游戏C++模拟
- 下一篇:C++面向对象程序设计 经典例题 附练习题
评论
共有 条评论