91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡(jiǎn)介

使用c++實(shí)現(xiàn)的算術(shù)編碼,從屏幕讀入一個(gè)字符串并輸出十進(jìn)制及二進(jìn)制編碼,并計(jì)算壓縮率,字符概率自動(dòng)統(tǒng)計(jì)生成

資源截圖

代碼片段和文件信息

#include?“arithmetic.h“

void?Arithmetic::getProbabilities(char?*str)?{
????char?ch;
????double?lowLevel?=?0.0;
????double?highLevel?=?0.0;
????double?probability;
????int?freq[128]?=?{0};
????for?(int?i?=?0;?str[i];?i++)
????????freq[str[i]]++;
????cout?<????for?(int?i?=?0;?i?????????if?(freq[i])?{
????????????ch?=?(char)?i;
????????????probability?=?(double)?freq[i]?/?(double)?strlen(str);
????????????cout?<????????????lowLevel?=?highLevel;
????????????highLevel?=?lowLevel?+?probability;
????????????Range?range;
????????????range.setLow(lowLevel);
????????????range.setHigh(highLevel);
????????????range.setDelta(probability);
????????????map.insert(std::pair(ch?range));
????????}
????}
}

double?Arithmetic::encode(string?str)?{
????double?lowRange?=?0.0?highRange?=?1.0;
????for?(char?&i?:?str)?{
????????//?使用map來通過字符完成概率的查找
????????//?map[*i]表示的就是對(duì)應(yīng)的字符的出現(xiàn)概率
????????double?delta?=?highRange?-?lowRange;
????????highRange?=?lowRange?+?delta?*?map[i].getHigh();
????????lowRange?=?lowRange?+?delta?*?map[i].getLow();
????????++length;
????}
????return?lowRange;
}

void?Arithmetic::runArithmetic()?{
????long?beginT?=?clock();
????cout?<????char?str[1024];
????cin>>str;
????getProbabilities(str);
????long?endT?=?clock();
????long?costT?=?endT?-?beginT;
????cout?<????cout?<????dec2bin(encode(str));
????cout<????cout?<????cout?<????cin.clear();
????cin.sync();
}


void?Arithmetic::dec2bin(double?n)?{
????stack?s;
????int?m?=?(int)?n;
????double?t?=?n?-?m;//0.4
????binLength?=?0;
????while?(m)??????????????????????????//?處理整數(shù)
????{
????????s.push(m?%?2);
????????m?/=?2;
????}
????while?(!s.empty())?{
????????printf(“%d“?s.top());

????????s.pop();
????}
????printf(“0.“);
????while?(t?-?int(t)?!=?0)????????????????//處理小數(shù)點(diǎn)后的位數(shù),乘2取整法?,當(dāng)乘2變?yōu)檎麛?shù)后結(jié)束
????{
????????int?temp?=?int(t?*?2);
????????printf(“%d“?temp);
????????binLength++;
????????t?=?2?*?t?-?int(2?*?t);
????}
}

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????2433??2018-12-22?00:16??arithmetic.cpp
?????文件????????1216??2018-12-21?23:29??arithmetic.h

評(píng)論

共有 條評(píng)論