软件工程课程实验报告-压缩算法.docx
《软件工程课程实验报告-压缩算法.docx》由会员分享,可在线阅读,更多相关《软件工程课程实验报告-压缩算法.docx(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验报告课程名称:交互式媒体原理开课学期:专 业:软件工程年级班级:学生姓名:学 号:指导教师:计算机与信息科学学院软件学院46. huffmanCoderoot-ch = str;47. )48. encode(root-left, str + nOn, huffmanCode);49. encode(root-right, str + ” 1 ,huffmanCode);50. )51. void decode(HNode* root, int& index, string str)52. 53. if (root = nullptr) 54. return;55. 56. /found
2、a leaf node57. if (!root-left &!root-right)58. 59. cout root-ch;60. return;61. 62. index+;63. if (strindex= V)64. decode(root-left, index, str);65. else66. decode(root-right, index, str);67. 68. 建立赫夫曼树,解码输入的字符串69. void buildHuffmanTree(string text)70. (71. unordered_map freq;72. for (char ch : text)
3、 73. freqch+;74. 75. priority_queueHNode*, vector, comp pq;76. for (auto pair : freq) 77. pq.push(getNode(pair.first, pair.second, nullptr, nullptr);78. )79. while (pq.size()!= 1)80. (LZ、90. HNode* left = pq.top(); pq.popQ;91. HNode* right = pq.topO; pq.popO;92.93. int sum = left-freq + right-freq;9
4、4. pq.push(getNode(0, sum, left, right);95. )96.97. HNode* root = pq.topO;98.99. /遍历赫夫曼树并存储编码到map里同时打印100. unordered_map huffmanCode;101. encode(root,H, huffmanCode);102.103. cout Huffman Codes are :nn n;104. for (auto pair : huffmanCode) 105. cout pair.first pair.second An,;106. 107.108. cout AnOri
5、ginal string was :n text endl;109.110. 打印111. string str = ”;112. for (char ch : text) 113. str += huffmanCodech;114. 1115.116. cout nEncoded string is :n str endl;117.118. 遍历赫夫曼树119. 解码120. int index = -1;121. cout nDecoded string is: n;122. while (index (int)str.size() - 2) 123. decode(root, index
6、, str);124. 125. M.h1. #pragma once2. #include3. #include4. #include5. #includeLZ、6.7. using namespace std;8.9. class LZW10. 11. public:12. struct encodeinfo13. (14. string P;15. int index;16. ; 键值对17. LZW();18.19. vector LZW_encode(string s, int encodenum);20. string LZW_decode(vector code, int beg
7、innum);21.22. map diet;字典23. map revdict;解码时使用24. LZW();25. ;Mcpp1. include LZW.h2. using namespace std;3.4.5. LZW:LZW()6. (7. dict.clearQ;8. revdict.clear();9. for (int i = 0;i 128; i+)10. (11. string s = nt;12. s0 = char(i);13. dicts = i;14. revdicti = char(i);15. 16. 17.18. vector LZW: LZW_encode
8、(string s, int encodenum)19. 120. string P = n;21. char C;22. vector EncodeResult; /存储编码之后的结果23. for (int i = 0; i s.length(); i+)24. 25. C = si;26. string tempStr = P + C;27. 在字典里面寻找这个字符串28. map:iterator iter = dict.find(tempStr);29. if (iter != dict.end。)找到了30. P = tempStr;31. 32. else 没找到33. enco
9、deinfo a = P, dictP ;34. EncodeResuIt.push_back(a);将P的对应的编码存放起来35. 建立起一个新的索引36. encodenum+;37. dicttempStr=encodenum;38. P = C;39. 40. )41. encodeinfo a = P, dictP ;42. EncodeResult.push_back(a); 最终结尾处43. 这是编码过程的输出44. cout LZW编码输出的信息如下:n endl;45. for (int i = 0; i EncodeResult.size(); i+)46. cout En
10、codeResulti.P EncodeResulti.index endl;47. 48. return EncodeResult;49. 50. string LZW:LZW_decode(vector code, int beginnum) 51. string ret=最终译码的输出52. string P = n;53. char C;54. int pW, cW;55. 第一步,初始化,读入第一个的符号cW,解码输出56. cW = code0.index;57. ret += revdictcW; 解码输出58. for (int i = 1; i code.size(); i+
11、) 59. pW = cW;60. cW = codei.index;61. map:iterator iter = revdict.find(cW);62. if (iter != revdict.end。)找至lj 了63. 解码输出64. ret += iter-second;65. P = revdictpW;66. C = revdictcW0;67. string tempStr = P + C;68. beginnum+;69. revdictbeginnum = tempStr;70. 71. else72. (73. P = revdictfpW;74. .C = revdi
12、ctpW0;75. beginnum+;76. string tempStr = P + C;77. revdictbeginnumj = tempStr;78. ret += tempStr;79. 80. 81. return ret;82. 83. LZW:LZW()84. (85. Range.h1. #pragma once2. class range3. (4. public:5. range();6. range。;7. double GetLow();8. double GetHigh();9. void SetLow(double low);10. void SetHigh(
13、double high);11. void SetDelta(double delta);12. private:13. double low;14. double high;15. double deltalevel;16. 1;Range.cpp1. 用于存储算术编码中的上下界2. #include nrange.h3. range: :range()4. (5. low = 0.0;6. high = 1.0;7. deltalevel =1.0;8. )9. range:-range()10. 11. low = 0;12. high = 0;13. deltalevel = 0;14
14、. 15. double range:GetLow()16. (17. return this-low;18. )19. double range:GetHigh()20. 21. return this-high;22. 128. void range:SetLow(double low)129. (130. this-low = low;131. 132. void range:SetHigh(double high)133. (134. this-high = high;135. 136. void range:SetDelta(double delta)137. (138. this-
15、deltalevel = delta;139. Arithmetic.h1. #pragma once2. #include 3. #include range.h4. #include 5. #include 6. using namespace std;7. class Arithmetic8. (9. public:111. Arithmetic();112. Arithmetic。;13. void RequireCode(string str);14. double Encode(string str);15. string Decode(double value);j 17.int
16、 getLength();18. private:19. int length;20. mapmap;21. ;Arithmetic.cpp1. #include Arithmetic.h2. Arithmetic: Arithmetic()3. 4. length = 0;5. 6. Arithmetic:Arithmetic。7. (8. 9. void Arithmetic:RequireCode(string str)10. (11. doublelowlevel = 0.0;12. doublehighlevel = 0.0;13. doubleprobability = 0.0;1
17、4. for (int i = 0; i str.length(); i+)15. (16. cout Please input the probability endl;17. cin probability;18. lowlevel = highlevel;19. highlevel = lowlevel + probability;20. range r;21. r.SetLow(lowlevel);22. r.SetHigh(highlevel);23. r.SetDelta(probability);24. map.insert(std:pair(stri, r);25. 26. 2
18、7. double Arithmetic:Encode(string str)28. 29. double LowRange = 0.0, HighRange = 1.0;30. for (string:iterator itr = str.beginQ; itr != str.end(); +itr)31. 32. double delta = HighRange - LowRange;33. HighRange = LowRange + delta * map*itr.GetHigh();34. LowRange = LowRange + delta * map*itr.GetLow();
19、35. +length;36. 37. return LowRange;38. 39. string Arithmetic:Decode(double value)40. 41. double Lowlnterval = 0;42. double Highlnterval = 0;43. string symbol =44. for (auto itr = map.begin(); itr != map.end(); +itr)45. 46. if (itr-second).GetLow() second).GetHigh() value)55.(56.Lowlnterval = (itr-s
20、econd).GetLow();57.Highlnterval = (itr-second).GetHigh();58.symbol += (itr-first);59.break;60.)61.)62.63.for (int i = 0; i second).GetLow() second).GetHigh() value) 72.Lowlnterval = (i-second).GetLow();73.Highlnterval = (i-second).GetHigh();74.symbol + =(i-first);75.break;76.)77.)78.79.)80.return sy
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 软件工程 课程 实验 报告 压缩 算法
限制150内