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
| #include<stdio.h>
#include<stdlib.h>
#include<stddef.h>
#include"struct1.h"
POINTEUR (*select)(void);
int nb_unit=0;
UNIT *Tete=NULL;
void cree_unit(void){
UNIT * pCourant=NULL;
UNIT *Nouveau=NULL;
int instance_unit=1;
printf("combien d'unit ? \n");
scanf("%i",&nb_unit);
if (nb_unit>=1){
Nouveau=(UNIT*)malloc(sizeof(UNIT));
if (Nouveau==NULL){
printf("allocation malloc ");
exit(EXIT_FAILURE);
}
Nouveau->x=0;
Nouveau->y=0;
Nouveau->angle=0;
//Nouveau->pSuivant = Tete;
Tete = Nouveau;
Tete->pSuivant=NULL;
if (Tete != NULL) {
pCourant = Tete;
//while (pCourant->pSuivant != NULL) pCourant = pCourant->pSuivant;
if (nb_unit>=2){
for(instance_unit;instance_unit<nb_unit;instance_unit++){
Nouveau = (UNIT*)malloc(sizeof(UNIT));
if (Nouveau==NULL){
printf("allocation malloc ");
exit(EXIT_FAILURE);
}
Nouveau->x=0;
Nouveau->y=0;
Nouveau->angle=0;
pCourant->pSuivant = Nouveau;
Nouveau->pSuivant = NULL;
pCourant=Nouveau;
}
}
}
}
if(nb_unit==0){
printf("vous n'avez cree aucune unité \n");
return ;
}
}
POINTEUR selection_unit(void){
int select_unit=0;
int tete_unit=1;
UNIT *pCourant=NULL;
printf("selectionner une unit ");
scanf("%i",&select_unit);
if (select_unit>nb_unit){
printf ("erreur selection unit > au nb unit\n");
exit(EXIT_FAILURE);
}
if (select_unit==0){
pCourant=Tete;
printf("unit %i \n",select_unit);
return pCourant;
}
if (select_unit==1){
pCourant=Tete;
pCourant=pCourant->pSuivant;
printf("unit %i \n",select_unit);
return pCourant;
}
else {
pCourant=Tete;
for(tete_unit;tete_unit<select_unit;tete_unit++){
pCourant=pCourant->pSuivant;
}
printf("unit %i \n",select_unit);
return pCourant;
}
}
void emplacement_unit(POINTEUR select){
printf("\n indiquer position x ");
scanf("%i",&select->x);
printf("\n indiquer position y ");
scanf("%i",&select->y);
printf("\n indiquer angle ");
scanf("%i",&select->angle);
return;
}
void detruire_allunit(){
int instance_unit=2;
UNIT *pCourant=NULL;
UNIT *Nouveau=NULL;
if (nb_unit==1){
free(Tete);
return;
}
if (nb_unit>=2){
Nouveau=Tete;
Nouveau=Tete->pSuivant;
free(Tete);
for(instance_unit;instance_unit<nb_unit;instance_unit++){
pCourant=Nouveau->pSuivant;
free(Nouveau);
}
}
}
void montrer_unit(POINTEUR select){
printf("x vaut %i \n",select->x);
printf("y vaut %i \n",select->y);
printf("angle vaut %i \n",select->angle);
}
void main(){
printf("hello ");
cree_unit();
if (nb_unit>=1){
emplacement_unit(selection_unit());
}
montrer_unit(selection_unit());
detruire_allunit();
exit(EXIT_SUCCESS);
} |
Partager