1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <stdio.h>
#include <stdlib.h>
void RLE_enc(int **matrix, int nr, int nc, int *&vet, int *n_vet)
{
int j, i, count, elem;
bool exit;
j=i=count=0;
*n_vet = 0;
exit = false;
vet = new int[1];
elem=matrix[i][j];
while (!exit)
{
if (matrix[i][j]==elem)
{
count++;
j++;
if (j==nc)
{
i++;
if (i==nr)
{
exit = true;
vet = (int*) realloc(vet, sizeof(int) * ((*n_vet)+2));
vet[(*n_vet)]=count;
vet[(*n_vet)+1]=elem;
(*n_vet)+=2;
}
else
j=0;
}
}
else
{
vet = (int*) realloc(vet, sizeof(int) * ((*n_vet)+2));
vet[(*n_vet)]=count;
vet[(*n_vet)+1]=elem;
elem=matrix[i][j];
(*n_vet)+=2;
count=0;
}
}
}
int main()
{
int **matrix;
FILE *fp;
int nr = 2, nc = 3, i, j;
matrix = new int* [nr];
for (i = 0; i<nr; i++)
matrix[i] = new int[nc];
for (i = 0; i<nr; i++)
for (j= 0; j<nc; j++)
matrix[i][j] = i*nc + j;
int n_vet, *vet;
RLE_enc(matrix, nr, nc, vet, &n_vet);
fp = fopen("prova.bin", "wb");
for (i = 0; i<n_vet; i++)
fwrite(&vet[i], sizeof(int), 1, fp);
fclose(fp);
fp = fopen("prova.bin", "rb");
i = 0;
while (!feof(fp))
{
fread(&vet[i], sizeof(int), 1, fp);
printf("%d", vet[i]);
i++;
}
fclose(fp);
return 0;
}
|