From 518590d59512143ff279e98aa16c9b956f4b8699 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 3 Feb 2026 18:12:44 +0100 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=20=D0=BF=D1=8F=D1=82=D0=BE=D0=B9=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=B0=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5/1_huffman/main.cpp" | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/1_huffman/main.cpp" (limited to 'Занимательное программирование/5/1_huffman/main.cpp') diff --git "a/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/1_huffman/main.cpp" "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/1_huffman/main.cpp" new file mode 100644 index 0000000..24aba2c --- /dev/null +++ "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/1_huffman/main.cpp" @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include "huffman.hpp" + +std::vector read_file(char *filename) +{ + std::ifstream input_file{ filename, std::ios::binary | std::ios::in }; + input_file.exceptions(std::ios::badbit | std::ios::failbit); + + return std::vector{ std::istreambuf_iterator(input_file), {} }; +} + +enum class direction { + compress, + decompress +}; + +int show_usage() +{ + std::cerr << "Usage: huffman (compress|decompress) input_file output_file." << std::endl; + return 1; +} + +int main(int argc, char **argv) +{ + direction operation; + if (argc != 4) + { + return show_usage(); + } + if (std::strcmp(argv[1], "compress") == 0) + { + operation = direction::compress; + } + else if (std::strcmp(argv[1], "decompress") == 0) + { + operation = direction::decompress; + } + else + { + return show_usage(); + } + std::vector input; + try + { + input = read_file(argv[2]); + } + catch (std::iostream::failure& exception) + { + std::cerr << "Error opening file \"" << argv[2] << "\" for reading." << std::endl; + return 2; + } + std::ofstream output_file{ argv[3], std::ios::binary | std::ios::out | std::ios::trunc }; + output_file.exceptions(std::ios::badbit | std::ios::failbit); + + switch (operation) + { + case direction::compress: + compress(input, output_file); + break; + case direction::decompress: + decompress(input, output_file); + break; + } + return 0; +} -- cgit v1.2.3