Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Impossible is not for the faint of heart.
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cmath>
- int main() {
- std::ifstream input_file("comp", std::ios::binary);
- std::string input((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
- const int N = input.length();
- std::vector<double> x(N + 1);
- // input = "We the People We the People"; // First 5 characters of the US Constitution
- // input.resize(N);
- x[0] = 1;
- std::cout << "Original string: " << input << std::endl;
- std::cout << "Forward x_n sequence: ";
- for (int n = 0; n < N; ++n) {
- unsigned char y = input[n];
- x[n + 1] = x[n] + (static_cast<double>(y) / (n + 1));
- std::cout << x[n] << " ";
- }
- std::cout << x[N] << std::endl;
- double compressed_value = x[N];
- std::cout << "Compressed value (x_N): " << compressed_value << std::endl;
- std::string decompressed;
- for (int n = N - 1; n >= 0; --n) {
- double y_calc = (x[n + 1] - x[n]) * (n + 1);
- int y = std::round(y_calc);
- y = std::max(0, std::min(255, y));
- decompressed = static_cast<char>(y) + decompressed;
- x[n] = x[n + 1] - (static_cast<double>(y) / (n + 1));
- }
- std::cout << "Decompressed string: " << decompressed << std::endl;
- if (input == decompressed) {
- std::cout << "Compression is fully reversible." << std::endl;
- } else {
- std::cout << "Reversibility failed." << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement