add compress example default tip
authorJens Kasten <jens@kasten-edv.de>
Sat, 05 Mar 2011 23:40:07 +0100
changeset 4 5d1f5383d06d
parent 3 7be71cab2ebf
add compress example
compress/Makefile
compress/compress.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compress/Makefile	Sat Mar 05 23:40:07 2011 +0100
@@ -0,0 +1,19 @@
+IDIR="/usr/src/linux/include/rsbac"
+CC=gcc
+CFLAGS=-I$(IDIR)
+
+ODIR=obj
+
+LIBS=-lrsbac
+OBJ=compress.o
+
+compress: $(OBJ)
+	gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean: 
+	rm -f *.o compress
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compress/compress.c	Sat Mar 05 23:40:07 2011 +0100
@@ -0,0 +1,103 @@
+#include <stdio.h>
+#include <time.h>
+
+#define MAX_LENGTH 1000
+
+void create_random_values(int number, int *pvalues);
+void show_values(int number, int *pvalues);
+int compress(int number, int *pvalues);
+
+int main(void)
+{
+    int values[MAX_LENGTH];
+    int *pvalues;
+    int number;
+
+    pvalues = values;
+
+    printf("This example program compress an autogenerated block\n");
+    printf("The block contain random values from one and zero.\n");
+    printf("The rules for compress are:\n");
+    printf(" 1. if two follow numbers are equal replace the two values with 0\n");
+    printf(" 2. if two follow numbers are not equal replace them with 1\n");
+    printf(" 3. repeat it till only one number is left over\n");
+    printf("Insert a number wich represent the length.\n");
+    while (1) {
+        printf("Minimum > 0 and maximum < %d: ", MAX_LENGTH);
+        scanf("%d", &number);
+        if (number > 0 && number < MAX_LENGTH) {
+            break;
+        }
+    }
+    
+    create_random_values(number, pvalues);
+    printf("Generated block:\n");
+    show_values(number, pvalues);
+    printf("Result:\n");
+    compress(number, pvalues);
+
+    return 0;
+}    
+
+void create_random_values(int number, int *pvalues)
+{
+    int i;
+
+    srand(time(NULL));
+    for (i=0; i < number; i++) {
+        pvalues[i] = rand() % 2;
+    }
+}
+
+void show_values(int number, int *pvalues)
+{
+    int i, line_break=79;
+      
+    for (i=0; i < number; i++) {
+        printf("%d", pvalues[i]); 
+        if (i >= line_break && (i % line_break) == 0 && i > 0) {
+            printf("\n");
+        }
+    }
+    printf("\n");
+}
+
+int compress(int number, int *pvalues) 
+{
+    int i=0, counter=0, line_break=79, no_break=0;
+    int result[number];
+
+    while (i < number){
+        if (pvalues[i] == pvalues[i+1]) {
+            result[counter] = 0;
+        }
+        else if (pvalues[i] != pvalues[i+1]) { 
+            result[counter] = 1;
+        }
+        else 
+        {
+            result[counter] = pvalues[counter];        
+        }
+        i = i + 2;
+        counter++;
+    }
+    for (i=0; i < counter; i++) {
+        printf("%d", result[i]);
+        if (i >= line_break && (i % line_break) == 0 && i > 0) {
+            printf("\n");
+            no_break = 1;
+        }
+        else {
+            no_break = 0;
+        }
+    }
+    if (no_break == 0) {
+        printf("\n");
+    }
+    if (counter == 1) {
+        return 0;
+    } 
+    pvalues = result;
+    compress(counter, pvalues);
+}
+