Teoria de La Informacion
Teoria de La Informacion
Teoria de La Informacion
Descripción formal
Entradas
Salida
Objetivo
Ejemplo
Entrada (A, Símbolo (ai) a b c d e Suma
W) Peso (wi) 0.10 0.15 0.30 0.16 0.29 =1
Longitud de la palabra
(en bits) 3 3 2 2 2
Salida C
(li)
Probabilidad
Optimalidad 1/8 1/8 1/4 1/4 1/4 = 1.00
(2-li)
Cantidad de
información (en bits) 3.32 2.74 1.74 2.64 1.79
(−log2 wi) ≈
Entropía H(A) =
0.332 0.411 0.521 0.423 0.518
(−wi log2 wi) 2.205
Por simplicidad, los símbolos con probabilidad nula han sido dejados fuera de
la fórmula anterior).
Proceso de compilación
Ejemplo de código
int main(void) {
printf("Hola Mundo\n");
return 0;
}
El siguiente escribe "Hola Mundo" en C89
if (condicion 1) {
sentencia 1
} else if (condicion 2){
sentencia 2
} else if (condicion n){
sentencia n
} else {
sentencias por defecto
}
#include <stdio.h>
#include <malloc.h>
struct nodo
char bit; /* 0 o 1 */
}HOJAS[256],*TELAR[256],*MENOR,*SEGUNDO;
int NSIMB=0,nsimb;
FILE *f,*g;
int NBYTES=0;
/*--------------------------------
--------------------------------*/
int j;
for(j=0;j<256;++j){
HOJAS[j].der=HOJAS[j].izq=HOJAS[j].arr=NULL;
HOJAS[j].cuenta=0;
HOJAS[j].karacter=j;
HOJAS[j].código=NULL;
if ((f=fopen(archivo,"rb"))!=NULL){
while ((j=fgetc(f))!=EOF){
++HOJAS[j].cuenta;
++NBYTES;
fclose(f);
else
return(1);
for(j=0;j<256;++j){
if (HOJAS[j].cuenta!=0)
++NSIMB;
nsimb=NSIMB;
return(0);
}
/*--------------------------------
preparar telar
--------------------------------*/
void preparar_telar()
int j;
for(j=0;j<256;++j){
TELAR[j]=&(HOJAS[j]);
return;
/*--------------------------------
tejer el árbol
--------------------------------*/
void tejer()
int j;
if (nsimb==1) return;
/* buscar menor valor */
for(j=0;j<256;++j){
if (TELAR[j]==NULL) continue;
if (TELAR[j]->cuenta==0) continue;
if (menor==-1){
menor=j;
temporal=TELAR[j]->cuenta;
} else
if (TELAR[j]->cuenta<temporal){
menor=j;
temporal=TELAR[j]->cuenta;
for(j=0;j<256;++j){
if (TELAR[j]==NULL) continue;
if (TELAR[j]->cuenta==0) continue;
if (j==menor) continue;
if (segundo==-1){
segundo=j;
temporal=TELAR[j]->cuenta;
} else
{
if (TELAR[j]->cuenta<temporal){
segundo=j;
temporal=TELAR[j]->cuenta;
TELAR[menor]->arr=P;
TELAR[segundo]->arr=P;
P->izq=TELAR[menor];
P->der=TELAR[segundo];
P->arr=NULL;
TELAR[menor]->bit=0;
TELAR[segundo]->bit=1;
P->cuenta=TELAR[menor]->cuenta+TELAR[segundo]->cuenta;
TELAR[menor]=NULL;
TELAR[segundo]=P;
--nsimb;
tejer();
}
/*--------------------------------
fin de la cadena.
--------------------------------*/
void codificar()
char pila[64];
char tope;
int j;
char *w;
for(j=0;j<256;++j){
if (HOJAS[j].cuenta==0) continue;
tope=0;
while (P->arr!=NULL){
pila[tope]=P->bit;
++tope;
P=P->arr;
HOJAS[j].nbits=tope;
HOJAS[j].código=(char *)malloc((tope+1)*sizeof(char));
w=HOJAS[j].código;
--tope;
while (tope>-1){
*w=pila[tope];
--tope;
++w;
*w=2;
return;
/*--------------------------------
--------------------------------*/
void debug()
int j,k;
char *w;
int tam_comprimido=0;
for(j=0;j<256;++j){
if (HOJAS[j].cuenta==0) continue;
tam_comprimido+=(HOJAS[j].cuenta*HOJAS[j].nbits);
w=HOJAS[j].código;
while (*w!=2){
printf("%c",48+(*w));
++w;
printf("\n");
printf("NSIMB: %d\n",NSIMB);
printf("NBYTES: %d\n",NBYTES);
return;
/*--------------------------------
descompresion
--------------------------------*/
int j,k;
FILE *g;
if ((g=fopen(destino,"wb"))==NULL) return(1);
for(j=0;j<4;++j){
fputc(*p,g);
++p;
p=(char *)(&NSIMB);
fputc(*p,g);
for(j=0;j<256;++j){
if (HOJAS[j].cuenta==0) continue;
fputc(j,g);
p=(char *)(&(HOJAS[j].cuenta));
for(k=0;k<4;++k){
fputc(*p,g);
++p;
}
fclose(g);
return(0);
/*--------------------------------
archivo de destino
--------------------------------*/
int x;
char nbit=0;
char *p;
if ((f=fopen(origen,"rb"))==NULL) return(1);
while ((x=fgetc(f))!=EOF){
p=HOJAS[x].código;
while (*p!=2){
if (nbit==8){
nbit=0;
fputc(d,g);
d=0;
} else
if (*p==1){
d|=(1<<nbit);
++nbit;
++p;
fputc(d,g);
fclose(f);
fclose(g);
return(0);
/*--------------------------------
la cabecera:
NBYTES|NSIMB|(char,cuenta)*
--------------------------------*/
char *p;
int j,k,n,m;
if ((g=fopen(origen,"rb"))==NULL) return(1);
if ((f=fopen(destino,"wb"))==NULL) return(2);
/* leer NBYTES */
p=(char *)(&n);
for(j=0;j<4;++j){
*p=(unsigned char)fgetc(g);
++p;
NBYTES=n;
/* leer NSIMB */
NSIMB=nsimb=fgetc(g);
/* preparar las hojas */
for(j=0;j<256;++j){
HOJAS[j].cuenta=0;
HOJAS[j].izq=HOJAS[j].der=HOJAS[j].arr=NULL;
HOJAS[j].karacter=j;
for(j=0;j<NSIMB;++j){
n=fgetc(g);
p=(char *)(&m);
for(k=0;k<4;++k){
*p=(unsigned char)fgetc(g);
++p;
HOJAS[n].cuenta=m;
/* construyo el árbol */
preparar_telar();
tejer();
j=0;
j=0;
x=fgetc(g);
nbit=0;
Q=P;
while(j<NBYTES){
if (Q->izq==NULL){
fputc(Q->karacter,f);
Q=P;
++j;
} else
if (nbit==8){
x=fgetc(g);
nbit=0;
} else
if (x&(1<<nbit)){
Q=Q->der;
else
Q=Q->izq;
++nbit;
}
fclose(f);
fclose(g);
return(0);
{ int j;
if (argc<2) return;
if (*(argv[1])=='C'){ /* comprimir */
if (argc!=4) return;
if (preparar_hojas(argv[2])){
return;
preparar_telar();
tejer();
codificar();
if (escribe_cabecera(argv[3])){
return;
if (comprimir(argv[2],argv[3])){
else
if (*(argv[1])=='D'){ /* descomprimir */
if (argc!=4) return;
if (descomprimir(argv[2],argv[3])){
return;
else
if (*(argv[1])=='I'){ /* info */
if (argc!=3) return;
if (preparar_hojas(argv[2])){
return;
preparar_telar();
tejer();
codificar();
debug();
return;
}
4. Conclusiones