-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.cpp
109 lines (104 loc) · 2.33 KB
/
encoder.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// Created by Андрей Москалёв on 05.11.2019.
//
#include <iostream>
#include <map>
#include <fstream>
int ind = 0;
std::string dct = "";
char * tmp;
std::map<char, std::string> mp;
std::string getCode() {
++ind;
std::string answer = "";
while (dct[ind] == '0' || dct[ind] == '1') {
answer += dct[ind++];
}
++ind;
return answer;
}
void loadDict() {
tmp = new char;
std::ifstream in("generator.output");
while (in.read(tmp, 1)) {
dct += tmp;
}
while (ind < dct.size()) {
char dind = dct[ind];
std::string code = getCode();
std::cout << dind << ": " << code << "\n";
mp[dind] = code;
}
}
int main() {
loadDict();
// freopen("encoder.input", "rb", stdin);
// freopen("encoder.output", "wb", stdout);
std::ifstream in("encoder.input");
std::ofstream out("encoder.output");
while (in.read(tmp, 1)) {
out << mp[*(tmp)];
std::cout << mp[*(tmp)];
}
return 0;
}
//
// Created by Андрей Москалёв on 13.11.2019.
//
//
//#include <bits/stdc++.h>
//
//using namespace std;
//
//struct Node {
// char c;
// Node * left, * right;
// int id, weight = 1;
// Node() {
// left = nullptr;
// right = nullptr;
// }
// Node(char c, int id) {
// this->c = c;
// this->id = id;
// left = nullptr;
// right = nullptr;
// }
// bool isTerm() {
// return left == nullptr && right == nullptr;
// }
//};
//
//Node * loadTree() {
// std::ifstream in("/Users/andrewmoskalev/CLionProjects/Haffman/cmake-build-debug/generator.output");
// int n;
// in >> n;
// std::vector<pair<Node *, int>> v(n + 1);
// for (int i = 1; i <= n; ++i) {
// std::ifstream in("generator.output");
// char * tmp = new char;
// char * c = new char;
// in.read(c, 1);
// string pInd = "";
// while (in.read(tmp, 1)) {
// if (*tmp == '\n') {
// break;
// }
// pInd += *tmp;
// }
// int p = stoi(pInd);
// v[i] = {new Node(*c, i), p};
// }
// for (int i = 2; i <= n; ++i) {
// if (v[i].first->left == nullptr) {
// v[i].first->left = v[v[i].second].first;
// } else {
//
// }
// }
//}
//
//int main() {
//
// return 0;
//}