Skip to content

Commit e9ff4ed

Browse files
authored
Merge pull request #3 from NoUITeam/new
Basic tasks completed.
2 parents 2867b9f + 61c3f72 commit e9ff4ed

9 files changed

Lines changed: 248 additions & 49 deletions

File tree

Decode.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include"HuffmanCoding.h"
2+
3+
bool Document::Decode(){
4+
FILE* reader=fopen(path,"r");
5+
FILE *writer=fopen("result.txt","wb");
6+
if(!reader||!writer){
7+
cout<<"Oops! Something went wrong. Try again later."<<endl;
8+
return false;
9+
}
10+
int num=-1;
11+
while(!feof(reader)){
12+
if(fscanf(reader,"%d",&num)<=0||num==-1)
13+
break;
14+
}
15+
int flag=top;
16+
while(!feof(reader)){
17+
char c;
18+
if(fscanf(reader,"%c",&c)<=0) break;
19+
if(c=='0'){
20+
flag=HuffmanTree[flag][_left];
21+
}else if(c=='1'){
22+
flag=HuffmanTree[flag][_right];
23+
}else if(c=='\n'){
24+
}else{
25+
cout<<"Oops!\nDecoding failed! Invalid code exists."<<endl;
26+
return false;
27+
}
28+
if(HuffmanTree[flag][_left]==0 && HuffmanTree[flag][_right]==0){
29+
fprintf(writer,"%c",(char)flag);
30+
flag=top;
31+
}
32+
}
33+
cout<<"Finish. Check 'result.txt'."<<endl;
34+
fclose(reader);
35+
fclose(writer);
36+
return true;
37+
}
38+

Document.cpp

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
#include"HuffmanCoding.hpp"
1+
#include"HuffmanCoding.h"
22
#include"Heap.hpp"
3-
#include <stdio.h>
4-
#include<string>
53
using namespace std;
64

7-
Document::Document(const char *Address){
5+
Document::Document(const char *Address,int mode_chose){
86
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;
1110
}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+
}
2722
}
28-
fclose(reader);
29-
return true;
3023
}
3124

3225
bool Document::HTreeInit(){
33-
top=0;
3426
HuffmanTree[0][_parent]=-1;
27+
top=0;
3528
int flag=1;
3629
for(int i=0;i<=258;i++){
3730
if(BytecodeArray[i]!=0){
@@ -77,39 +70,29 @@ bool Document::WordsCreate(){
7770
int cur=i;
7871
int pre=HuffmanTree[cur][_parent];
7972
string code="";
80-
while(pre!=-1){
73+
while(pre!=0){
8174
code = HuffmanTree[pre][_left]==cur ? ('0'+code):('1'+code);
8275
cur=pre;
8376
pre=HuffmanTree[pre][_parent];
8477
}
8578
Words[i]=code;
8679
}
8780
}
81+
return true;
8882
}
8983

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;
9693
}
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;
11294
}
11395

11496

11597

98+

Encode.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include"HuffmanCoding.h"
2+
3+
bool Document::Encode(){
4+
FILE *reader=fopen(path,"rb");
5+
FILE *writer=fopen("out.txt","wb");
6+
if(!reader||!writer){
7+
cout<<"Oops! Something went wrong. Try again later."<<endl;
8+
return false;
9+
}
10+
for(int i=0;i<=256;i++){
11+
if(BytecodeArray[i]>0)
12+
fprintf(writer,"%d %d ",i,BytecodeArray[i]);
13+
}
14+
fprintf(writer,"-1\n");
15+
char buffer[3];
16+
while(!feof(reader)){
17+
int count=fread(buffer,1,1,reader);
18+
if(count>0)
19+
fprintf(writer,"%s",Words[(int)*buffer].c_str());
20+
}
21+
cout<<"Finish. Check 'out.txt'."<<endl;
22+
fclose(reader);
23+
fclose(writer);
24+
return true;
25+
}
26+
27+
28+
29+
30+

HuffmanCoding.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef _HUFFMANCODING_H
2+
#define _HUFFMANCODING_H
3+
#include "Heap.hpp"
4+
#include <stdio.h>
5+
#include <iostream>
6+
#include <string>
7+
#include <windows.h>
8+
using namespace std;
9+
10+
#define _weigth 0
11+
#define _left 1
12+
#define _right 2
13+
#define _parent 3
14+
#define _value 4
15+
16+
struct Node{
17+
int val;
18+
int weight;
19+
int parent;
20+
Node(int val,int weight,int parent){
21+
this->val=val;
22+
this->weight=weight;
23+
this->parent=parent;
24+
}
25+
friend bool operator < (Node a,Node b){
26+
return a.weight < b.weight;
27+
}
28+
};
29+
30+
31+
class Document{
32+
private:
33+
int BytecodeArray[258];
34+
int HuffmanTree[512][5];
35+
string Words[258];
36+
const char* path;
37+
Heap<Node> Nodes;
38+
int mode;
39+
int top;
40+
///
41+
bool Readin(int mode);
42+
bool HTreeInit();
43+
bool Select(int & node1,int & node2);
44+
bool HTreeCreate();
45+
bool WordsCreate();
46+
public:
47+
Document(const char *Address,int mode_chose);
48+
//~Document();
49+
bool Encode();
50+
bool Decode();
51+
bool watch();
52+
53+
};
54+
55+
#endif

Main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include"HuffmanCoding.h"
2+
#include"Document.cpp"
3+
#include"Readin.cpp"
4+
#include"Encode.cpp"
5+
#include"Decode.cpp"
6+
using namespace std;
7+
int main(){
8+
string address1="in.txt",address2="out.txt";
9+
int mode0=0,mode1=1;
10+
/*
11+
cout<<"Enter name or path of a file:";
12+
cin>>filePath;
13+
cout<<"Enter mode:";
14+
cin>>mode;
15+
*/
16+
const char* path1=address1.c_str();
17+
const char* path2=address2.c_str();
18+
Document test(path1,mode0);
19+
Document test2(path2,mode1);
20+
//test.watch();
21+
return 0;
22+
}

Readin.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include"HuffmanCoding.h"
2+
3+
bool Document::Readin(int mode){
4+
if(mode==0){
5+
FILE* reader=fopen(path,"rb");
6+
if(reader==NULL) return false;
7+
char buffer[3];
8+
while(!feof(reader)){
9+
int count=fread(buffer,1,1,reader);
10+
if(count>0)
11+
BytecodeArray[(int)*buffer]++;
12+
}
13+
fclose(reader);
14+
}else if(mode==1){
15+
FILE* reader=fopen(path,"r");
16+
int num,wt;
17+
int flag=0;
18+
while(!feof(reader)){
19+
int count=fscanf(reader,"%d",&num);
20+
if(count<=0||num==-1) break;
21+
count=fscanf(reader,"%d",&wt);
22+
if(count<=0||num==-1) break;
23+
BytecodeArray[num]=wt;
24+
}
25+
fclose(reader);
26+
}
27+
return true;
28+
}

in.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
123 000 abcd !@# test `;\?
1+
#include"HuffmanCoding.h"
2+
#include"Document.cpp"
3+
#include"Readin.cpp"
4+
#include"Encode.cpp"
5+
#include"Decode.cpp"
6+
using namespace std;
7+
int main(){
8+
string address1="in.txt",address2="out.txt";
9+
int mode0=0,mode1=1;
10+
/*
11+
cout<<"Enter name or path of a file:";
12+
cin>>filePath;
13+
cout<<"Enter mode:";
14+
cin>>mode;
15+
*/
16+
const char* path1=filePath.c_str();
17+
const char* path2=filePath.c_str();
18+
Document test(path1,mode0);
19+
Document test2(path2,mode1);
20+
//test.watch();
21+
return 0;
22+
}

out.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
32 11 33 1 35 1 48 3 49 1 50 1 51 1 59 1 63 1 64 1 92 1 96 1 97 1 98 1 99 1 100 1 101 1 115 1 116 2 -1
2-
11011111000110101111110011001100111111000011001010000101110111110110101001110011111111111111111101101011111101001011011111111110101011010110001101000
1+
10 22 13 22 32 75 34 18 35 5 40 6 41 6 42 4 44 4 46 10 47 4 48 4 49 6 50 4 58 2 59 13 60 4 61 6 62 4 67 1 68 4 69 3 72 1 80 3 82 1 95 2 97 20 99 26 100 20 101 33 102 7 103 3 104 12 105 19 108 9 109 13 110 27 111 19 112 14 114 12 115 15 116 36 117 14 119 1 120 2 123 1 125 1 -1
2+
1110010110000011001010000101010110110111100010110111010101001000101000100010110100011011011100110011101111000001111100110111000111110100011110111110111001011000001100101000010101011011011110001100000011001001001010000100111001110011110000010010010100110001111011111011100101100000110010100001010101101101111000101101010101111101011011110000011111000001001001010011000111101111101110010110000011001010000101010110110111100011110011100110010110011101101111110000010010010100110001111011111011100101100000110010100001010101101101111000110000000111001011001110110111111000001001001010011000111101111100101001011110000011111001101010011110100001001110101101001110100010011110101011100111011000111110111110110000011100110100010110101100000110000110000010110001111110111110101101101101010111001111111110000011111001101011101011011110111111110111010110101100000000001010001110000011111000100101101111100110001100000111010110111101111111101110101101011011001100001010001110010101010011110001001011011111001100010001111101111101011011011011100000111001101000101100111011011101100100000100110010100000100010110011101101110000000000100000000001111101111101011011011010110100011000011101111101011011011010010110010101010010100001010000110001111001110011100101111111111010011110100001001111011100111111110101001110101001111110101110010100011011101010101000111000100001011101100010100010001111101111101011011011010010110000011011011001101100100011100010000101110100000110101001111110000111110111110101101101101001011001010101001010000101000011000111100111001110010111111111101000101100111011011101100010100010001111101111101011011011010010110000011011011001101100001011001110110111000111110111110101101101101011000001101001110111110101101101101001011001001101011100110100101111101101011111101100001010100111010100111111000000000001001000111000100001011101000001101010011111101110000010011010110101110011111110000110000010001111101111101011011011010010110010011010111001101001011111011010111111011000010101001110101001111110011001100001001000111000100001011101000001101010011111101110000010011010110101110011111110000110000010001111101111101011011011011000000110010010010100001001110011100110110010111010111001000011010011101010011111100000001000001000101100111011011101100100000010001111101111101011011011011000000110010010010100001001110011100110110010111010111001011001100001101001110101001111110011001110000010001011001110110111000000000001000111110111110101101101101011010001101001001011101011100111100001100011011010100100101111100000110000010001111101111101011011011011111110111100101010111111001110101100100001111101111100110101001110111110

result.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include"HuffmanCoding.h"
2+
#include"Document.cpp"
3+
#include"Readin.cpp"
4+
#include"Encode.cpp"
5+
#include"Decode.cpp"
6+
using namespace std;
7+
int main(){
8+
string address1="in.txt",address2="out.txt";
9+
int mode0=0,mode1=1;
10+
/*
11+
cout<<"Enter name or path of a file:";
12+
cin>>filePath;
13+
cout<<"Enter mode:";
14+
cin>>mode;
15+
*/
16+
const char* path1=filePath.c_str();
17+
const char* path2=filePath.c_str();
18+
Document test(path1,mode0);
19+
Document test2(path2,mode1);
20+
//test.watch();
21+
return 0;
22+
}

0 commit comments

Comments
 (0)