aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine5/30_compression.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/tme/semaine5/30_compression.c')
-rw-r--r--semestre 2/informatique/tme/semaine5/30_compression.c72
1 files changed, 62 insertions, 10 deletions
diff --git a/semestre 2/informatique/tme/semaine5/30_compression.c b/semestre 2/informatique/tme/semaine5/30_compression.c
index 161cd00..154dbc9 100644
--- a/semestre 2/informatique/tme/semaine5/30_compression.c
+++ b/semestre 2/informatique/tme/semaine5/30_compression.c
@@ -14,15 +14,19 @@ int init_tab(int tab[MAX+1]) {
}
void compress_tab(int tab_brut[], int tab_compress[]){
- int j = 0;
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) {
- int id = j++;
- tab_compress[id*2] = cnt;
- tab_compress[id*2+1] = last;
+ if (cnt == 1) {
+ tab_compress[w] = last;
+ } else {
+ tab_compress[w] = cnt;
+ tab_compress[++w] = last;
+ }
+ w++;
}
last = tab_brut[i];
cnt = 0;
@@ -30,11 +34,35 @@ void compress_tab(int tab_brut[], int tab_compress[]){
cnt++;
}
if (last != -1) {
- int id = j++;
- tab_compress[id*2] = cnt;
- tab_compress[id*2+1] = last;
+ 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_compress[j*2] = -1;
+ tab_brut[w] = -1;
}
int compare(int tab_brut[], int tab_compress[]){
@@ -51,7 +79,31 @@ int compare(int tab_brut[], int tab_compress[]){
int main(){
srand(time(NULL));
- int tab[MAX+1] = {};
+ 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);
@@ -59,6 +111,6 @@ int main(){
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");
+ printf("%s", compare(tab, tab_compress) ? "True" : "False");*/
return 0;
}