Avec votre aide, je pouvais prendre l'entrée d'un fichier texte (input.txt) où les lignes se composaient de la distance city1 de city2 ... et d'écrire les noms des villes dans une matrice sans répétition. Selon cette matrice, j'ai écrit un morceau de code pour ajouter leurs distances dans une matrice d'adjacence. Mais la sortie semble bizarre je veux dire, ce n'est pas correct. Je suppose que dans mon code ci-dessous devrait être quelque chose qui manque ou qui ne va pas. Toute petite aide est très appréciée.langage de programmation C, matrice de contiguïté
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]){
int i=1,j, state=0, k, dist,x=0,y=0;
int** myMat;
char *city1, *city2, **matnames;
FILE* p;
city1 = (char*) malloc(sizeof(char));
city2 = (char*) malloc(sizeof(char));
matnames = (char**) malloc(sizeof(char*));
myMat = (int**) malloc(sizeof(int*)*4);
p = fopen(argv[1],"r");
/************************************************************/
matnames[0] = (char*) malloc(sizeof(char));
matnames[1] = (char*) malloc(sizeof(char));
matnames[2] = (char*) malloc(sizeof(char));
matnames[2] = NULL;
fscanf(p, "%s %s %d", city1, city2, &dist);
strcpy(matnames[0],city1);
strcpy(matnames[1],city2);
/************************************************************/
for(i=0;i<3;i++){
myMat[i] = (int*) malloc(sizeof(int));
}
myMat[1][2] = dist; /* the first two distances placed at matnames */
myMat[2][1] = dist;
/************************************************************/
while(fscanf(p,"%s %s %d",city1,city2, &dist) != EOF){
for(j=0; matnames[j]!=NULL; j++){
if(strcmp(matnames[j], city1) != 0){
state++;
}
}
if(state == j){
matnames = realloc(matnames, sizeof(char*)*(j+3));
matnames[j] = (char*) malloc(sizeof(char));
strcpy(matnames[j], city1);
matnames[j+1] = (char*) malloc(sizeof(char));
matnames[j+1] = NULL;
}
state = 0;
for(j=0; strcmp(matnames[j], city1) != 0;j++){
x++; /* "x" finds the city1 indeks from matnames*/
}
for(k=0; matnames[k] != NULL;k++){
if(strcmp(matnames[k], city2) != 0){
state++;
}
}
if(state == k){
matnames = realloc(matnames, sizeof(char*)*(k+4));
matnames[k] = (char*) malloc(sizeof(char));
matnames[k+1] = (char*) malloc(sizeof(char));
strcpy(matnames[k], city2);
matnames[k+1] = NULL;
}
state = 0;
/* till to here the names of cities are placed in matnames without repetion*/
for(j=0; strcmp(matnames[j], city2) != 0;j++){
y++; /* "y" finds the city2 indeks from matnames*/
}
/****** the problem should be in this part */
myMat = realloc(myMat,sizeof(int*)*(k+3));
for(i=2;i<k+2;i++){
myMat[i] = (int*) malloc(sizeof(int));
}
myMat[x][y] = dist;
myMat[y][x] = dist;
x=0; y=0;
}
return 0;
}
Vous avez oublié de nous dire quelle sortie vous attendiez, et ce que vous avez obtenu à la place. –
'malloc (sizeof (char))' Vous réservez une zone équivalente à un seul caractère. – BLUEPIXY
il devrait être quelque chose comme ça http://www.google.com/imgres?um=1&hl=en&sa=N&biw=1280&bih=914&tbm=isch&tbnid=PBgIE6L4D_GYAM:&imgrefurl=http://pages.cpsc.ucalgary.ca/~ eharris/cpsc319/tut17/& docid = iYHck4pU_PMitM & imgurl = http: //pages.cpsc.ucalgary.ca/~eharris/cpsc319/tut17/matrix2.bmp&w=774&h=448&ei=vyLTT_HQA4fcsgbLhtTrDw&zoom=1&iact=hc&vpx=946&vpy=512&dur=4409&hovh=171&hovw = 295 & tx = 118 & ty = 90 & sig = 108197378289192158396 & page = 2 & TBNH = 122 & tbnw = 210 & start = 30 & NDSP = 25 & ved = 1t: 429, r: 4, s: 30, i: 184 s'il y sont reliés de leur distance doit être placé dans leurs cordinates – bledi