|
1 | | -#include"HuffmanCoding.hpp" |
| 1 | +#include"HuffmanCoding.h" |
2 | 2 | #include"Heap.hpp" |
3 | | -#include <stdio.h> |
4 | | -#include<string> |
5 | 3 | using namespace std; |
6 | 4 |
|
7 | | -Document::Document(const char *Address){ |
| 5 | +Document::Document(const char *Address,int mode_chose){ |
8 | 6 | path=Address; |
9 | | - if(!Readin()){ |
10 | | - cout<<"No such file or failed to open."<<endl; |
| 7 | + mode=mode_chose; |
| 8 | + if(!Readin(mode)){ |
| 9 | + cout<<"Oops!\nNo such file or failed to open."<<endl; |
11 | 10 | }else{ |
12 | | - HTreeInit(); |
13 | | - HTreeCreate(); |
14 | | - WordsCreate(); |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -bool Document::Readin(){ |
19 | | - FILE* reader=fopen(path,"rb"); |
20 | | - if(reader==NULL) return false; |
21 | | - char buffer[3]; |
22 | | - if(!reader) return false; |
23 | | - while(!feof(reader)){ |
24 | | - int count=fread(buffer,1,1,reader); |
25 | | - if(count>0) |
26 | | - BytecodeArray[(int)*buffer]++; |
| 11 | + Sleep(500); |
| 12 | + cout<<"Read-in success.\nCreating codeTree..."<<endl; |
| 13 | + Sleep(500); |
| 14 | + if(HTreeInit()&&HTreeCreate()&&WordsCreate()){ |
| 15 | + cout<<"Going to "<<(mode==0?"encoding":"decoding")<<"..."<<endl; |
| 16 | + Sleep(500); |
| 17 | + if(mode==0){ Encode(); } |
| 18 | + else{ Decode(); } |
| 19 | + }else{ |
| 20 | + cout<<"Oops! Something went wrong. Try again later."<<endl; |
| 21 | + } |
27 | 22 | } |
28 | | - fclose(reader); |
29 | | - return true; |
30 | 23 | } |
31 | 24 |
|
32 | 25 | bool Document::HTreeInit(){ |
33 | | - top=0; |
34 | 26 | HuffmanTree[0][_parent]=-1; |
| 27 | + top=0; |
35 | 28 | int flag=1; |
36 | 29 | for(int i=0;i<=258;i++){ |
37 | 30 | if(BytecodeArray[i]!=0){ |
@@ -77,39 +70,29 @@ bool Document::WordsCreate(){ |
77 | 70 | int cur=i; |
78 | 71 | int pre=HuffmanTree[cur][_parent]; |
79 | 72 | string code=""; |
80 | | - while(pre!=-1){ |
| 73 | + while(pre!=0){ |
81 | 74 | code = HuffmanTree[pre][_left]==cur ? ('0'+code):('1'+code); |
82 | 75 | cur=pre; |
83 | 76 | pre=HuffmanTree[pre][_parent]; |
84 | 77 | } |
85 | 78 | Words[i]=code; |
86 | 79 | } |
87 | 80 | } |
| 81 | + return true; |
88 | 82 | } |
89 | 83 |
|
90 | | -bool Document::Encode(){ |
91 | | - FILE *reader=fopen(path,"rb"); |
92 | | - FILE *writer=fopen("out.txt","wb"); |
93 | | - if(!reader||!writer){ |
94 | | - printf("Failed to encode. File Error.\n"); |
95 | | - return false; |
| 84 | +bool Document::watch(){ |
| 85 | + cout<<endl; |
| 86 | + for(int i=0;i<512;i++){ |
| 87 | + cout<<i<<" "//<<(char)i<<" " |
| 88 | + <<HuffmanTree[i][_weigth]<<" " |
| 89 | + <<HuffmanTree[i][_left]<<" " |
| 90 | + <<HuffmanTree[i][_right]<<" " |
| 91 | + <<HuffmanTree[i][_parent]<<" " |
| 92 | + <<(i<=256?Words[i]:" ")<<endl; |
96 | 93 | } |
97 | | - for(int i=0;i<=256;i++){ |
98 | | - if(BytecodeArray[i]>0){ |
99 | | - fprintf(writer,"%d %d ",i,BytecodeArray[i]); |
100 | | - } |
101 | | - } |
102 | | - fprintf(writer,"-1\n"); |
103 | | - char buffer[3]; |
104 | | - while(!feof(reader)){ |
105 | | - int count=fread(buffer,1,1,reader); |
106 | | - if(count>0) |
107 | | - fprintf(writer,"%s",Words[(int)*buffer].c_str()); |
108 | | - } |
109 | | - fclose(reader); |
110 | | - fclose(writer); |
111 | | - return true; |
112 | 94 | } |
113 | 95 |
|
114 | 96 |
|
115 | 97 |
|
| 98 | + |
0 commit comments