Изменил(а) на 'sk2a/compressor.c'

This commit is contained in:
Oleksandr Hryshchenko 2022-01-09 21:48:11 +00:00
parent 42bd39bb6e
commit c3907f8bba

View File

@ -6,43 +6,34 @@
#include "compressor.h"
#define BUFSIZE 1024
int basic_string = 0;
char *rotateString(char *str, int count) {
size_t len = strlen(str);
int counter;
for(counter=0;counter < len;counter++) {
int value = (int)str[counter];
if((char)value != ' ') {
value = value + count;
str[counter] = value;
}
}
return str;
char* leftRotatebyOne(char arr[BUFSIZE * 2]){
char* result = (char*) calloc(strlen(arr), sizeof(char));
char temp = arr[0];
for (int i = 0; i < strlen(arr) - 1; i++)
arr[i] = arr[i + 1];
arr[strlen(arr)-1] = temp;
strcpy(result, arr);
return result;
}
char* bwt_compress(char buffer[BUFSIZE * 2]){
char* encoded = (char*) calloc(BUFSIZE *2, sizeof(char));
char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
char rotate_temp[BUFSIZE * 2];
for(int i = 0; i < strlen(buffer); i++)
rotate_temp[i] = buffer[i];
rotate_temp[strlen(buffer)] = '\0';
char* rotation[strlen(buffer)];
for(int i = 0; i < strlen(buffer); i++)
rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char));
for(int i = 0; i < strlen(buffer); i++)
strcpy(rotation[i], leftRotatebyOne(rotate_temp));
char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char));
for(int i = 0; i < strlen(buffer); i++){
rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char));
}
for(int i = 0; i < strlen(buffer); i++){
strcpy(rotation[i], rotateString(buffer, -1));
}
for(int i = 0; i < strlen(buffer); i++){
for(int j = 0; j < strlen(buffer); j++){
for(int j = 0; j < strlen(buffer) - i - 1; j++){
if(strcmp(rotation[j], rotation[j+1]) > 0){
strcpy(temp, rotation[j]);
strcpy(rotation[j], rotation[j+1]);
@ -51,16 +42,23 @@ char* bwt_compress(char buffer[BUFSIZE * 2]){
}
}
for(int i = 0; i < strlen(buffer); i++){
strncat(encoded, &rotation[i][strlen(rotation[i])], 1);
if(strcmp(rotation[i], buffer) == 0){
basic_string = i;
for(int i = 0; i < strlen(buffer); i++) {
strncat(encoded, &rotation[i][strlen(rotation[i]) - 1], 1);
if(strcmp(rotation[i], buffer) == 0) {
char str[100];
for(int j = 0; j < 100; j++)
str[j] = '\0';
sprintf(str, "%d", i);
FILE* num = fopen("num.txt", "w");
fwrite(str, sizeof(char), strlen(str), num);
}
}
return encoded;
}
char* run_length(char buffer[BUFSIZE]){
char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
int counter = 1;
@ -106,34 +104,47 @@ void compress(FILE* infile,FILE* outfile){
}
}
char* bwt_decompress(char buffer[BUFSIZE * 2]){
char* rotation[strlen(buffer)];
for(int i = 0; i < strlen(buffer); i++){
rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char));
char* bwt_decompress(char buffer[BUFSIZE * 2]){
char* decoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
char rotation[strlen(buffer)][BUFSIZE * 2];
for(int i = 0; i < strlen(buffer); i++) {
for (int j = 0; j < strlen(buffer) + 1; j++) {
if(j != strlen(buffer))
rotation[i][j] = ' ';
else
rotation[i][j] = '\0';
}
}
for(int i = 0; i < strlen(buffer); i++){
for(int j = 0; j < strlen(buffer); j++){
rotation[j][i] = buffer[strlen(buffer) - 1 -j];
for(int i = 0; i < strlen(buffer); i++) {
for (int j = 0; j < strlen(buffer) + 1; j++) {
rotation[j][strlen(buffer) - 1 - i] = buffer[j];
}
char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char));
for(int iterator = 0; iterator < strlen(buffer); iterator++){
for(int j = 0; j < strlen(buffer); j++){
if(strcmp(rotation[j], rotation[j+1]) > 0){
char *temp = (char *) calloc(BUFSIZE * 2, sizeof(char));
for (int iterator = 0; iterator < strlen(buffer); iterator++) {
for (int j = 0; j < strlen(buffer) - 1 - iterator; j++) {
if (strcmp(rotation[j], rotation[j + 1]) > 0) {
strcpy(temp, rotation[j]);
strcpy(rotation[j], rotation[j+1]);
strcpy(rotation[j+1], temp);
strcpy(rotation[j], rotation[j + 1]);
strcpy(rotation[j + 1], temp);
}
}
}
}
return rotation[basic_string];
FILE* num = fopen("num.txt", "r");
char number[100];
for(int i = 0; i < 100; i++)
number[i] = '\0';
fread(number, sizeof(char), 100, num);
strcpy(decoded, rotation[atoi(number)]);
return decoded;
}
char* run_length_decompress(char buffer[BUFSIZE * 2]){
char* decoded = (char*) calloc(BUFSIZE, sizeof(char));
int i = 0;
@ -157,6 +168,7 @@ char* run_length_decompress(char buffer[BUFSIZE * 2]){
return decoded;
}
void decompress(FILE* infile,FILE* outfile){
char buffer[BUFSIZE * 2];
memset(buffer, 0, BUFSIZE*2);