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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct
{
float x;
float y;
float z;
}t_point;
void deallocate_1D_ui( unsigned int *p )
{
if (p)
free(p), p=NULL;
else printf("deallocate_1D_ui -> %s\n",strerror(errno));
}
unsigned int *allocate_1D_ui( unsigned long long int n )
{
unsigned long long int i;
unsigned int *p = NULL;
if ((p = (unsigned int *)malloc(n*sizeof(unsigned int))))
for ( i=0 ; i<n ; i++ )
p[i]=0;
else printf("allocate_1D_ui -> %s\n", strerror(errno));
return p;
}
t_point *point_create_struct(unsigned int x)
{
t_point *p = NULL;
if ( !(p=(t_point*)malloc(x*sizeof(t_point))) )
printf("\n point_allocate_struct : echec de l'allocation");
return p;
}
void point_destroy_struct(t_point *p)
{
if(p)
free(p), p=NULL;
else printf("\n point_destroy_struct : p is already NULL");
}
t_point *equation(t_point *a, t_point *b, t_point *tab, unsigned int length, unsigned int *index)
{
t_point *r=point_create_struct(5);
if ( (a && b && tab && index) )
{
if (r)
{
float r_equation=0.;
float lambda = -((a->x*b->x)+(a->y*b->y)+(a->z*b->z));
unsigned int i;
for(i=0;i<length;i++)
{
r_equation = ((a->x*tab[i].x)+(a->y*tab[i].y)+(a->z*tab[i].z))+lambda;
printf("test %d => r_equation = %f \n", i, r_equation);
if( r_equation == 0. )
{
r[*index].x=tab[i].x;
r[*index].y=tab[i].y;
r[*index].z=tab[i].z;
(*index)++;
}
}
} else printf("\n equation : Echec de l'allocation de r");
} else printf("\n equation : Au moins un des arguments est NULL");
return r;
}
int main(int argc,char **argv)
{
//declaration
t_point *a=point_create_struct(3);
t_point *b=point_create_struct(3);
t_point *t=point_create_struct(5);
t_point *r=point_create_struct(5);
unsigned int *index = allocate_1D_ui(1);
//initialisation
a->x=-1.;a->y=1.;a->z=0.;
b->x=1.;b->y=-1.;b->z=0.;
t[0].x=0.;t[0].y=-2.;t[0].z=0.;
t[1].x=2.;t[1].y=0.;t[1].z=0.;
t[2].x=1.;t[2].y=1.;t[2].z=0.;
t[3].x=0.;t[3].y=6.;t[3].z=0.;
t[4].x=5.;t[4].y=2.;t[4].z=0.;
//calcule des valeurs vérifiant l'equation
r=equation(a,b,t,5,index);
//affichage des résultats
unsigned int k;
for (k=0;k<*index;k++)
printf("Solution %d : (%3.3f,%3.3f,%3.3f)\n", k+1, r[k].x, r[k].y, r[k].z);
point_destroy_struct(a);
point_destroy_struct(b);
point_destroy_struct(t);
point_destroy_struct(r);
deallocate_1D_ui(index);
system("pause");
return 0;
} |
Partager