-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (75 loc) · 2.14 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "HuffmanStream.h"
void encode(const char* encodeFileName, const char* outputFileName, const char* mode){
FILE* fin = fopen(encodeFileName, mode);
FILE* fout = fopen(outputFileName, "wb");
Encoding(fin, fout);
fclose(fin);
fclose(fout);
}
void decode(const char* decodeFileName, const char* outputFileName, const char* mode)
{
FILE* fin = fopen(decodeFileName, "rb");
FILE* fout = fopen(outputFileName, mode);
Decoding(fin, fout);
fclose(fin);
fclose(fout);
}
void printCompressibility(const char* source, const char* encoded)
{
FILE* en = fopen(source,"rb");
fseek(en, 0, SEEK_END);
long i = ftell(en);
fclose(en);
en = fopen(encoded, "rb");
fseek(en, 0, SEEK_END);
long o = ftell(en);
fclose(en);
long com = i-o;
double rate = (double)com / (double)i;
std::cout << "--------------------------\nCoding Information:\n--------------------------\nInput file: "<<i/1000<<"kb"<< std::endl;
std::cout << "Output file: " << o/1000 <<"kb" <<std::endl;
std::cout << "Compressibility: " << rate*100 <<"%\n--------------------------"<< std::endl;
}
enum command{ENCODE, DECODE};
void acceptCommand(command c)
{
if(c==ENCODE)
{
std::cout << "Enter The Name of Which to be Encoded:\n";
char input[140];
gets_s(input, 139);
std::cout << "Enter The Name of Output File:\n";
char output[140];
gets_s(output, 139);
std::cout << "Encoding Begin...\n";
encode(input, output, "rb");
printf("Finished!\n");
printCompressibility(input, output);
}
else {
std::cout << "Enter The Name of Which to be Decoded(it should be generated by this program):\n";
char input[140];
gets_s(input, 139);
std::cout << "Enter The Name of Output File:\n";
char output[140];
gets_s(output, 139);
std::cout << "Decoding Begin...\n";
decode(input, output, "wb");
printf("Finished!\n");
}
}
int main()
{
command c;
do
{
printf("Enter Number of Command:\n1.Encode;\n2.Decode;\n3.Exit.\n");
char ch = (char)getchar(); getchar();
if (ch == '1')c = ENCODE;
else if (ch == '2')c = DECODE;
else if(ch=='3') break;
else continue;
acceptCommand(c);
} while (true);
return 0;
}