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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
char *allocate_1D_c( unsigned long long int n );
int *allocate_1D_i( unsigned long long int n );
void deallocate_1D_i( int *p );
void deallocate_1D_c( char *p );
void deallocate_2D_i( int **p, unsigned long long int x );
int **allocate_2D_i( unsigned long long int x, unsigned long long int y );
unsigned long long int str_length(const char *S);
void file_close(FILE *f);
FILE *file_open(const char *p, const char *r);
unsigned long long int file_length( const char *p );
char *file_to_string( const char *p );
int matrix_get_m( unsigned long long int element_qty, unsigned long long int n);
int char_to_int( char c );
void matrix_fill( char *s, int **matrix, unsigned long long int m, unsigned long long int n );
void matrix_display( int **matrix, unsigned long long int m, unsigned long long int n );
int main()
{
// Redimensionnement de la console en 150*100
system("mode con cols=150 lines=100");
// Le fichier text.txt est copié dans un tableau de char
char *s = file_to_string("text.txt");
// Calcul de la longueur du fichier texte maintenant copié dans le tableau de char s
unsigned long long int s_length = str_length(s);
// Ton fichier fait s_length caractères et ta matrice est définie comme MAT(m,76).
// Ici on calcule m
unsigned long long int m = matrix_get_m(s_length, 76);
// Maintenant que l'on connait la taille, on peut allouer une matrice de m*76 éléments
int **matrix = allocate_2D_i(m,76);
// A partir de la chaine s, on remplit cette matrice
matrix_fill(s, matrix, m, 76);
// Une fois remplie, on peut l'afficher
matrix_display(matrix, m, 72);
// Libération de la mémoire alloué.
deallocate_1D_c(s);
deallocate_2D_i(matrix, m);
return 0;
}
char *allocate_1D_c( unsigned long long int n )
{
unsigned long long int i;
char *p = NULL;
if ((p = (char *)malloc(n*sizeof(char))))
for ( i=0 ; i<n ; i++ )
p[i]='\0';
else printf("allocate.h::allocate_1D_c -> %s\n", strerror(errno));
return p;
}
int *allocate_1D_i( unsigned long long int n )
{
unsigned long long int i;
int *p = NULL;
if ((p = (int *)malloc(n*sizeof(int))))
for ( i=0 ; i<n ; i++ )
p[i]=0;
else printf("allocate.h::allocate_1D_i -> %s\n", strerror(errno));
return p;
}
void deallocate_1D_i( int *p )
{
if (p)
free(p), p=NULL;
else printf("deallocate.h::deallocate_1D_i -> %s\n",strerror(errno));
}
void deallocate_1D_c( char *p )
{
if (p)
free(p), p=NULL;
else printf("deallocate.h::deallocate_1D_c -> %s\n",strerror(errno));
}
void deallocate_2D_i( int **p, unsigned long long int x )
{
if (p)
{
unsigned long long int i;
for ( i=0 ; i<x ; i++ )
free(p[i]), p[i]=NULL;
free(p), p=NULL;
}
else printf("deallocate.h::deallocate_2D_i -> %s\n",strerror(errno));
}
int **allocate_2D_i( unsigned long long int x, unsigned long long int y )
{
int **t = NULL;
if ( (t = (int**)malloc( x*sizeof(int*))) )
{
unsigned long long int i;
for ( i=0 ; i<x ; i++ )
{
if ( !( t[i] = allocate_1D_i(y) ) )
{
printf("allocate.h::allocate_2D_i -> %s\n", strerror(errno));
deallocate_2D_i(t,i-1);
i=x;
}
}
}
else printf("allocate.h::allocate_2D_i -> %s\n", strerror(errno));
return t;
}
unsigned long long int str_length(const char *S)
{
unsigned long long int c = 0;
while( *S != '\0')
c++, S++;
return c;
}
void file_close(FILE *f)
{
fclose(f);
}
FILE *file_open(const char *p, const char *r)
{
FILE *f = NULL;
if ( !(f=fopen(p,r)) )
printf("file.h::file_open -> %s\n", strerror(errno));
return f;
}
unsigned long long int file_length( const char *p )
{
char c;
unsigned long long int l = 0;
FILE *f = file_open(p,"r");
while( fscanf(f,"%c",&c) != EOF )
l++;
file_close(f);
return l;
}
char *file_to_string( const char *p )
{
char c;
unsigned long long int i = 0;
char *s = allocate_1D_c( file_length(p)+1 );
FILE *f = file_open(p,"r");
while( fscanf(f,"%c",&c) != EOF )
{
s[i] = c;
i++;
}
file_close(f);
return s;
}
int matrix_get_m( unsigned long long int element_qty, unsigned long long int n)
{
return element_qty/n;
}
int char_to_int( char c )
{
if ( c == '0' ) { return 0; }
if ( c == '1' ) { return 1; }
if ( c == '2' ) { return 2; }
if ( c == '3' ) { return 3; }
if ( c == '4' ) { return 4; }
if ( c == '5' ) { return 5; }
if ( c == '6' ) { return 6; }
if ( c == '7' ) { return 7; }
if ( c == '8' ) { return 8; }
if ( c == '9' ) { return 9; }
return -1;
}
void matrix_fill( char *s, int **matrix, unsigned long long int m, unsigned long long int n )
{
unsigned long long int i,j;
for ( i=0 ; i<m ; i++ )
for (j=0; j<n ; j++)
matrix[i][j] = char_to_int( s[j + n*i] );
}
void matrix_display( int **matrix, unsigned long long int m, unsigned long long int n )
{
printf(" ");
unsigned long long int i,j;
for ( i=0 ; i<m ; i++ )
for (j=0; j<n ; j++)
{
if ( j==n-1 )
printf("\n%c", matrix[i][j]);
else printf("%d",matrix[i][j]);
}
} |
Partager