2012-06-09 64 views
0

在你的幫助下,我可以從一個文本文件(input.txt)中取得輸入,其中行由城市city2的距離構成......並且將城市名稱寫入矩陣中而不重複。根據這個矩陣,我寫了一段代碼在鄰接矩陣中添加它們的距離。但輸出看起來很奇怪,我的意思是,這是不正確的。我想在我的代碼下面應該是缺少或錯誤的東西。任何一點幫助都非常感謝。C編程語言,鄰接矩陣

#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; 
} 
+0

你忘了告訴我們你的預期輸出和你得到的是什麼。 –

+2

'malloc(sizeof(char))'您保留的區域相當於只有一個字符。 – BLUEPIXY

+0

它應該是類似的東西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&頁= 2&TBNH = 122&tbnw = 210&開始= 30 NDSP = 25 VED =1噸&&:429,R:4,S:30,I:184,如果有連接它們的距離應該被放置在它們的cordinates – bledi

回答

1

您爲city1和city2分配1個字符(malloc與sizeof(char))。除非你的城市都只有一個角色,否則fscanf會超出範圍。

請再試一次city1 =(char *)malloc(1024),這樣一個城市可以再長一點。

matnames不是很明顯,你希望它是什麼,而是你給它分配的sizeof 1(指針),也就是4個字節。然後你做matnames [0] = ...,matnames [1] = ... matnames [1]已經溢出你分配的內存。所以你在隨機寫作。

C語言對內存分配非常寬容,但結果是完全不可預測的。確保你的malloc足夠的空間,並儘可能考慮其他語言。 :)

+0

實際上有沒有問題與matnames,但與memeory的「myMat」 – bledi