Update sk1/main.c
This commit is contained in:
		
							parent
							
								
									c3a3eb374b
								
							
						
					
					
						commit
						fba872e93a
					
				
							
								
								
									
										32
									
								
								sk1/main.c
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								sk1/main.c
									
									
									
									
									
								
							| @ -1,4 +1,3 @@ | ||||
| // main.c
 | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| @ -7,20 +6,17 @@ | ||||
| void display_help(); | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|     // Перевірка кількості аргументів
 | ||||
|     if (argc < 4) { | ||||
|         display_help(); | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     // Імена вхідного та вихідного файлів
 | ||||
|     char *input_filename = argv[2]; | ||||
|     char *output_filename = argv[3]; | ||||
|     char *algorithm = argc >= 5 ? argv[4] : "rle";  // Алгоритм, за замовчуванням rle
 | ||||
| 
 | ||||
|     // Обробка компресії
 | ||||
|     if (argc >= 4 && strcmp(argv[1], "-c") == 0) { | ||||
|         if (strcmp(algorithm, "rle") == 0) { | ||||
|     // Компресія
 | ||||
|     if (argc == 4 && strcmp(argv[1], "-c") == 0) { | ||||
|         if (strstr(output_filename, ".rle") != NULL) { | ||||
|             printf("Compressing using RLE...\n"); | ||||
|             int result = rle_compress(input_filename, output_filename); | ||||
|             if (result >= 0) { | ||||
| @ -28,7 +24,7 @@ int main(int argc, char *argv[]) { | ||||
|             } else { | ||||
|                 printf("Compression failed with error code %d\n", result); | ||||
|             } | ||||
|         } else if (strcmp(algorithm, "lz77") == 0) { | ||||
|         } else if (strstr(output_filename, ".lz77") != NULL) { | ||||
|             printf("Compressing using LZ77...\n"); | ||||
|             int result = lz77_compress(input_filename, output_filename); | ||||
|             if (result >= 0) { | ||||
| @ -37,13 +33,13 @@ int main(int argc, char *argv[]) { | ||||
|                 printf("Compression failed with error code %d\n", result); | ||||
|             } | ||||
|         } else { | ||||
|             printf("Unsupported compression algorithm. Use 'rle' or 'lz77'.\n"); | ||||
|             printf("Unsupported compression format. Use .rle or .lz77.\n"); | ||||
|             return -1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // Обробка декомпресії
 | ||||
|     else if (argc >= 4 && strcmp(argv[1], "-d") == 0) { | ||||
|     // Декомпресія
 | ||||
|     else if (argc == 4 && strcmp(argv[1], "-d") == 0) { | ||||
|         if (strstr(input_filename, ".rle") != NULL) { | ||||
|             printf("Decompressing RLE...\n"); | ||||
|             int result = rle_decompress(input_filename, output_filename); | ||||
| @ -61,12 +57,12 @@ int main(int argc, char *argv[]) { | ||||
|                 printf("Decompression failed with error code %d\n", result); | ||||
|             } | ||||
|         } else { | ||||
|             printf("Unsupported decompression format.\n"); | ||||
|             printf("Unsupported decompression format. Use .rle or .lz77.\n"); | ||||
|             return -1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // Виведення довідки
 | ||||
|     // Показати довідку
 | ||||
|     else if (argc == 3 && strcmp(argv[1], "-h") == 0) { | ||||
|         display_help(); | ||||
|     } else { | ||||
| @ -78,14 +74,12 @@ int main(int argc, char *argv[]) { | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| // Функція для виведення довідки
 | ||||
| void display_help() { | ||||
|     printf("Usage:\n"); | ||||
|     printf("  -c infile outfile [algorithm]  : Compress infile to outfile. Algorithm can be 'rle' or 'lz77'. Default is 'rle'.\n"); | ||||
|     printf("  -d compressed outfile          : Decompress compressed to outfile.\n"); | ||||
|     printf("  -h                             : Display this help message.\n"); | ||||
|     printf("  -c infile outfile  : Compress infile to outfile. Supported formats: .rle, .lz77\n"); | ||||
|     printf("  -d compressed outfile : Decompress compressed to outfile. Supported formats: .rle, .lz77\n"); | ||||
|     printf("  -h                 : Display this help message.\n"); | ||||
|     printf("Examples:\n"); | ||||
|     printf("  compression_tool -c input.txt output.rle rle\n"); | ||||
|     printf("  compression_tool -c input.txt output.lz77 lz77\n"); | ||||
|     printf("  compression_tool -c input.txt output.rle\n"); | ||||
|     printf("  compression_tool -d output.rle decompressed.txt\n"); | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user