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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct bal{
unsigned taille;
char c;
int a;
int *p;
};
typedef struct bal Tbal;
struct maillon{
Tbal e;
struct maillon *psuivant;
};
typedef struct maillon Tmaillon;
struct liste{
Tmaillon *ptete;
Tmaillon *pqueue;
};
typedef struct liste Tliste;
Tbal creebal(char s,int b,unsigned taille)
{
Tbal e;
e.taille=taille;
e.c=s;
e.a=b;
e.p=(int*)malloc(sizeof(int));
return e;
}
Tmaillon *creemaillon(Tbal b)
{
Tmaillon *m=malloc(sizeof(Tmaillon));
if(m!=NULL)
{
m->e.taille=b.taille;
m->e.c=b.c;
m->e.a=b.a;
m->e.p=(int*)malloc(sizeof(int));
m->psuivant=NULL;
}
return m;
}
void ajoutliste(Tliste *l,Tbal e, int *w)
{
unsigned i;
Tmaillon *m=creemaillon(e);
for(i=0;i<m->e.taille;i++)
m->e.p[i]=w[i];
l->pqueue=m;
l->pqueue=m->psuivant;
if(l->ptete==NULL)
l->ptete=m;
return ;
}
Tliste *creeliste()
{
Tliste *u=malloc(sizeof(Tliste));
u->ptete=NULL;
u->pqueue=NULL;
return u;
}
void affichage_binaire(Tliste *l)
{
unsigned i;
Tmaillon *m;
m=l->ptete;
while(m!=NULL)
{
printf("(");
for(i=0;i<m->e.taille;i++)
printf("%d",m->e.p[i]);
printf(")");
m=m->psuivant;
}
}
main()
{
unsigned j,taille,i,k;
int aide,stok,a;
int *l=(int*)malloc(sizeof(int));
int q;
Tbal z;
Tliste *x;
char *p,*ch,s;
x=creeliste();
ch=(char*)malloc(sizeof(char));
printf("\tEntrez le texte a convertir en binaire\n");
gets(ch);
printf("\n\n");
printf("%s\n\n\n",ch);
for(p=ch,q=0;*p;p++,q++)
{
s=*(ch+q);
a=*(ch+q);
printf("\t\t%c -----> en code ASCII [%d]\n",s,a);
stok=a;
i=0;
if(stok==0)
{
l=malloc(1*sizeof(int));
l[0]=0;
i=2;
}
else
do
{
l[i]=stok%2;
stok/=2;
i++;
}while(stok);
j=i;
taille=j;
z=creebal(s,a,j);
for(j=0,k=i;j<k;j++,k--)
{
aide=*(l+j);
*(l+j)=*(l+k);
*(l+k)=aide;
}
ajoutliste(x,z,l);
free (l);
}
printf("\n\n\t\t (");
affichage_binaire(x);
printf(")");
} |
Partager