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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include"predefine.h"
void solve(){ cout << "Enter the filename: "; string filename; cin >> filename; cout << "Sender's ID: "; string sender_id; cin >> sender_id; cout << "Sender's name: "; string sender_name; cin >> sender_name; cout << "Receiver's ID: "; string receiver_id; cin >> receiver_id; cout << "Receiver's name: "; string receiver_name; cin >> receiver_name; cout << '\n';
string info = sender_id + ", " + sender_name + "\n" + receiver_id + ", " + receiver_name + "\n";
ifstream file(filename, ios::binary); file.seekg(0, ios::end); int length = file.tellg(); file.seekg(0, ios::beg); unordered_map<uchar, int> freqMap; char ch; for(auto ch : info) freqMap[((uchar)ch + 0x55) % 0x100]++; while(file.get(ch)) freqMap[((uchar)ch + 0x55) % 0x100]++; file.close(); priority_queue<HuffmanNode> minHeap; for(auto &[ch, freq] : freqMap){ minHeap.emplace(HuffmanNode(ch, freq)); }
auto temp = minHeap; break_line; cout << "Word frequency statistics list:" << '\n'; cout << "byte freq" << '\n'; while(!temp.empty()){ cout << "0x" << setw(2) << setfill('0') << hex << (int)temp.top().ch << setw(6) << setfill(' ') << dec << temp.top().freq << '\n'; temp.pop(); } break_line;
while (minHeap.size() > 1) { auto left = new HuffmanNode(minHeap.top()); minHeap.pop(); auto right = new HuffmanNode(minHeap.top()); minHeap.pop();
uchar merge_ch = max(left->ch, right->ch); int merge_freq = left->freq + right->freq;
auto newNode = new HuffmanNode(merge_ch, merge_freq, left, right); minHeap.emplace(*newNode); } HuffmanNode Head = minHeap.top();
auto cal_WPL = [](auto &&self, HuffmanNode *u, int dep) -> int{ if(u->left == nullptr && u->right == nullptr) { return u->freq * dep; } return self(self, u->left, dep + 1) + self(self, u->right, dep + 1); }; cout << "WPL : " << dec << cal_WPL(cal_WPL, &Head, 0) << '\n';
map<uchar, string>HuffmanCode; auto generate = [&](auto &&self, HuffmanNode* node, const string& code) -> void{ if (node == nullptr) return; if (node->left == nullptr && node->right == nullptr) { HuffmanCode[node->ch] = code; } self(self, node->left, code + "0"); self(self, node->right, code + "1"); }; generate(generate, &Head, "");
ofstream outfile("code.txt", ios::binary); outfile << length << '\n'; for(auto &[ch, cd] : HuffmanCode){ outfile << "0x" << hex << setw(2) << setfill('0') << (int)ch; int code_len = cd.size(); outfile << " 0x" << hex << setw(2) << setfill('0') << (int)code_len; string encoded; for(int i = 0; i < cd.size(); i += 8){ uchar byte = 0; for(int j = 0; j < 8 && i + j < cd.size(); j++){ byte |= (cd[i + j] == '1') << (7 - j); } encoded += byte; } for(auto &byte : encoded){ outfile << " 0x" << hex << setw(2) << setfill('0') << (int)(uchar)byte; } outfile << '\n'; } outfile.close();
file.open("code.txt"); string line; break_line; cout << "Code table:" << '\n'; while (getline(file, line)) { cout << line << endl; } file.close();
string compressed_file = filename.substr(0, filename.length() - 3) + "hfm"; outfile.open(compressed_file, ios::binary); file.open(filename, ios::binary); string bitStream; for(auto ch : info) bitStream += HuffmanCode[((uchar)ch + 0x55) % 0x100]; while(file.get(ch)) bitStream += HuffmanCode[((uchar)ch + 0x55) % 0x100]; file.close(); break_line; cout << "Size of compressed file: " << bitStream.size() / 8 << " bytes" << '\n'; cout << "Last 16 bytes in " + compressed_file << ":\n"; for(int i = 0; i < bitStream.size(); i += 8){ uchar byte = 0; for(int j = 0; j < 8 && i + j < bitStream.size(); j++){ byte |= (bitStream[i + j] == '1') << (7 - j); } outfile << byte; if(bitStream.size() - i < 8 * 16){ cout << "0x" << setw(2) << setfill('0') << hex << (int)byte << " "; } } cout << '\n'; break_line; outfile.close();
file.open(filename, ios::binary); char *buffer = new char[length]; file.read(buffer, length); file.close(); char* buffer_with_info = new char[info.size() + length]; memcpy(buffer_with_info, info.c_str(), info.size()); memcpy(buffer_with_info + info.size(), buffer, length); uint64_t hash_value = fnv1a_64(buffer, length); cout << "Original file's FNV-1a hash: 0x" << hex << hash_value << '\n'; hash_value = fnv1a_64(buffer_with_info, info.size() + length); cout << "File with info's FNV-1a hash: 0x" << hex << hash_value << '\n';
file.open(compressed_file, ios::binary); file.seekg(0, ios::end); int compressed_length = file.tellg(); file.seekg(0, ios::beg); buffer = new char[compressed_length]; file.read(buffer, compressed_length); file.close(); hash_value = fnv1a_64(buffer, compressed_length); cout << "Compressed file's FNV-1a hash: 0x" << hex << hash_value << '\n'; break_line; }
signed main(){ solve(); system("pause"); return 0; }
|