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
|
#include <stdlib.h>
typedef struct
{
int dummy;
}
COMPLEX;
typedef struct
{
int n;
int m;
COMPLEX **P;
}
structMatrice, *Matrice;
static int deMemeDim (Matrice A, Matrice B)
{
return A->n == B->n && A->m == B->m;
}
static Matrice Mat_nulle_quelconque (int n, int m)
{
return NULL;
}
static void AddComplex (COMPLEX * pc, COMPLEX * pa, COMPLEX * pb)
{
}
/* allouer une matrice de l lignes et c colonnes */
static Matrice allouerMatrice (int l, int c)
{
Matrice A = malloc (sizeof *A);
A->n = l;
A->m = c;
A->P = malloc (l * sizeof *A->P);
{
int i;
for (i = 0; i < l; i++)
{
A->P[i] = malloc (c * sizeof *A->P[i]);
}
}
return A;
}
/* libère l'espace mémoire occupé par la matrice */
static void libererMatrice (Matrice A)
{
if (A != NULL)
{
if (A->P != NULL)
{
int i;
for (i = 0; i < A->n; i++)
{
free (A->P[i]);
}
}
free (A->P);
}
free (A);
}
/* renvoie la matrice issue de l'addition de A et de B */
static Matrice Mat_add (Matrice A, Matrice B)
{
Matrice C = NULL;
if (A != NULL && B != NULL)
{
int i, j;
Matrice Mat_nulle;
if (!deMemeDim (A, B))
{
Mat_nulle = Mat_nulle_quelconque (A->n, A->m);
return Mat_nulle;
}
C = allouerMatrice (A->n, A->m);
for (i = 0; i < A->n; i++)
{
for (j = 0; j < A->m; j++)
{
//C->P[i][j]=A->P[i][j]+B->P[i][j];
AddComplex (&C->P[i][j], &A->P[i][j], &B->P[i][j]);
}
}
}
return C;
}
int main (void)
{
Matrice A = allouerMatrice (2, 3);
if (A != NULL)
{
Matrice B = allouerMatrice (2, 4); /* '4' intentionnel */
if (B != NULL)
{
Matrice C = Mat_add (A, B);
libererMatrice (C);
libererMatrice (B);
}
libererMatrice (A);
}
return 0;
} |
Partager