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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <math.h>
typedef struct{
char nom[80];
int x;
int y;
}Ville;
typedef struct{
int nombre_ville;
Ville **villes;
}Carte;
typedef struct {
int *chemin;
int nb_villes;
int longueur;
}Parcours;
Ville *creer_ville(const char *nom){
Ville *nouvelle;
int ax;
int ay;
ax=800;
ay=600;
nouvelle=(Ville*)malloc(sizeof(Ville));
if(nouvelle==NULL)
return 0;
strcpy (nouvelle->nom,nom);
nouvelle->x=rand()%ax;
nouvelle->y=rand()%ay;
return nouvelle;
}
void ecrire_fichier (Ville *a){
FILE* fichier = NULL;
fichier = fopen("test.txt", "a");
fprintf(fichier,"%s %d %d\n",a->nom,a->x,a->y);
fclose(fichier);
}
void affiche_ville(Ville *a){
printf("%s %d %d\n",a->nom,a->x,a->y);
}
double calcul_distance (Ville *a,Ville *b){
double d;
d=sqrt(((b->x-a->x)*(b->x-a->x))+((b->y-a->y)*(b->y-a->y)));
return d;
}
void gestion_carte(){
char nom[80];
Carte carte1;
srand(time(NULL));
int i;
double resul;
scanf("%d",&carte1.nombre_ville);
carte1.villes = malloc(sizeof(Ville *)*carte1.nombre_ville);
for (i = 0; i < carte1.nombre_ville; i++) {
sprintf(nom,"V%d",i+1);
carte1.villes[i] = creer_ville(nom);
affiche_ville(carte1.villes[i]);
ecrire_fichier(carte1.villes[i]);
}
resul=calcul_distance(carte1.villes[2],carte1.villes[6]);
printf("%f",resul);
}
void lires_fichier(){
int i;
Ville *a;
FILE* fichier = NULL;
fichier = fopen("test.txt", "r");
while(fscanf(fichier, "%s %d %d\n",a->nom,&a->x,&a->y)==3){
printf("\n%s %d %d\n",a->nom,a->x,a->y);
}
fclose(fichier);
}
Parcours *creer_parcours(){
int nb_ville;
scanf("%d",&nb_ville);
Parcours *nouvelle;
nouvelle=(Parcours*)malloc(sizeof(Parcours));
if(nouvelle==NULL){
return 0;
}
nouvelle->chemin=nb_ville;
return nouvelle;
}
void genere_visite_alea (Parcours *v,int nb_ville){
int i,j;
int s;
int trous;
int *vs;
for (i=0;i<nb_ville;i++){
s=rand()%(nb_ville-2-i);
trous=0;
for(j=1;j<nb_ville-1;j++){
if (vs[j]==0)
trous++;
if(trous==s)
break;
vs[j]=i;
}
}
}
int main(int argc,char* argv[]){
gestion_carte();
lires_fichier();
return 0;
} |
Partager