From e29d9e80b1ca35fb1a95b5add8609d3d0978e958 Mon Sep 17 00:00:00 2001 From: Yurii Chechur Date: Tue, 24 Dec 2024 17:17:13 +0000 Subject: [PATCH] Update sk1/main.c --- sk1/main.c | 81 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/sk1/main.c b/sk1/main.c index 59bcf00..2e3cbea 100644 --- a/sk1/main.c +++ b/sk1/main.c @@ -7,48 +7,57 @@ #define BUFSIZE 1024 void print_help() { - printf("Usage:\n"); - printf(" ./compressor -c1 infile outfile Compress using algorithm 1 (Huffman)\n"); - printf(" ./compressor -d1 infile outfile Decompress using algorithm 1 (Huffman)\n"); - printf(" ./compressor -c2 infile outfile Compress using algorithm 2 (RLE)\n"); - printf(" ./compressor -d2 infile outfile Decompress using algorithm 2 (RLE)\n"); - printf(" ./compressor -h Show this help message\n"); + printf("Available Options:\n"); + printf("1 - Compress using algorithm 1 (Huffman)\n"); + printf("2 - Decompress using algorithm 1 (Huffman)\n"); + printf("3 - Compress using algorithm 2 (RLE)\n"); + printf("4 - Decompress using algorithm 2 (RLE)\n"); + printf("0 - Exit\n"); } -int main(int argc, char* argv[]) { - if (argc < 2) { - print_help(); - return 1; - } +int main() { + int choice; + char infile[256], outfile[256]; - if (strcmp(argv[1], "-h") == 0) { - print_help(); - return 0; - } + // Покажемо користувачу доступні варіанти + print_help(); - // Ensure correct number of arguments - if (argc != 4) { - fprintf(stderr, "Error: Invalid number of arguments.\n"); - print_help(); - return 1; - } + while (1) { + // Запитуємо користувача про вибір + printf("Enter your choice (0 to exit): "); + if (scanf("%d", &choice) != 1) { + fprintf(stderr, "Invalid input. Exiting.\n"); + break; + } - const char* mode = argv[1]; - const char* infile = argv[2]; - const char* outfile = argv[3]; + if (choice == 0) { + printf("Exiting program.\n"); + break; + } - if (strcmp(mode, "-c1") == 0) { - compress_1(infile, outfile); - } else if (strcmp(mode, "-d1") == 0) { - decompress_1(infile, outfile); - } else if (strcmp(mode, "-c2") == 0) { - compress_2(infile, outfile); - } else if (strcmp(mode, "-d2") == 0) { - decompress_2(infile, outfile); - } else { - fprintf(stderr, "Error: Unknown mode '%s'.\n", mode); - print_help(); - return 1; + // Вводимо імена файлів + printf("Enter input file name: "); + scanf("%s", infile); + printf("Enter output file name: "); + scanf("%s", outfile); + + switch (choice) { + case 1: + compress_1(infile, outfile); + break; + case 2: + decompress_1(infile, outfile); + break; + case 3: + compress_2(infile, outfile); + break; + case 4: + decompress_2(infile, outfile); + break; + default: + printf("Invalid choice. Please select again.\n"); + break; + } } return 0;