aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine5/30_compression.c
blob: 154dbc96f2508aee8d4f5e70c47b97de647bc7a7 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MIN 2
#define MAX 15

int init_tab(int tab[MAX+1]) {
	int size = rand()%(MAX-MIN+1)+MIN;
	for (int i = 0; i < size; i++) {
		tab[i] = rand()%2;
	}
	tab[size] = -1;
	return size;
}

void compress_tab(int tab_brut[], int tab_compress[]){
	int last = -1;
	int cnt = 0;
	int w = 0;
	for (int i = 0; tab_brut[i] != -1; i++) {
		if (last == -1 || tab_brut[i] != last) {
			if (last != -1) {
				if (cnt == 1) {
					tab_compress[w] = last;
				} else {
					tab_compress[w] = cnt;
					tab_compress[++w] = last;
				}
				w++;
			}
			last = tab_brut[i];
			cnt = 0;
		}
		cnt++;
	}
	if (last != -1) {
		if (cnt == 1) {
			tab_compress[w] = last;
		} else {
			tab_compress[w] = cnt;
			tab_compress[++w] = last;
		}
		w++;
	}
	tab_compress[w] = -1;
}

void decompress(int tab_brut[], int tab_compress[]) {
	int i = 0;
	int w = 0;
	while (tab_compress[i] != -1) {
		int v = tab_compress[i];
		if (v < 2) {
			tab_brut[w++] = v;
		} else {
			int ins_v = tab_compress[++i];
			for (int j = 0; j < v; j++) {
				tab_brut[w++] = ins_v;
			}
		}
		for (int j = 0; j < w; j++) printf("%d, ", tab_brut[j]);
		printf("\n");
		i++;
	}
	tab_brut[w] = -1;
}

int compare(int tab_brut[], int tab_compress[]){
	int j = 0;
	for (int i = 0; tab_compress[i] != -1; i += 2) {
		int n = tab_compress[i];
		int v = tab_compress[i+1];
		for (int cnt = 0; cnt < n; cnt++) {
			if (tab_brut[j++] != v) return 0;
		}
	}
	return 1;
}

int main(){
	srand(time(NULL));
	int tab1[]={0,0,1,1,1,1,0,1,1,1,0,0,1,0,0,0,0,-1};
	//int tab1[] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,-1};
	for (int i = 0; tab1[i] != -1; i++) printf("%d, ", tab1[i]);
	printf("\n");
	int tab2[20];
	compress_tab(tab1,tab2);
	int tab3[20];
	decompress(tab3, tab2);
	int i;
	for (i = 0; tab1[i] != -1 && tab3[i] != -1; i++) {
		printf("%d - %d\n", tab1[i], tab3[i]);
	}
	printf("%d\n", i);
	i = 0;
	while (tab2[i] != -1) {
		int v = tab2[i];
		if (v < 2) {
			printf("%d ", v);
		} else {
			printf("%d:%d ", v, tab2[++i]);
		}
		i++;
	}
	printf("\n");
	/*int tab[MAX+1] = {};
	int tab_compress[MAX+1] = {};
	init_tab(tab);
	compress_tab(tab, tab_compress);
	for (int i = 0; tab[i] != -1; i++) printf("%d, ", tab[i]);
	printf("\n");
	for (int i = 0; tab_compress[i] != -1; i += 2) printf("%d:%d, ", tab_compress[i], tab_compress[i+1]);
	printf("\n");
	printf("%s", compare(tab, tab_compress) ? "True" : "False");*/
	return 0;
}