邻接矩阵如何转化为计算机语言,C编程语言,邻接矩阵

在你的帮助下,我可以从一个文本文件(input.txt)中取得输入,其中行由城市city2的距离构成......并且将城市名称写入矩阵中而不重复。根据这个矩阵,我写了一段代码在邻接矩阵中添加它们的距离。但输出看起来很奇怪,我的意思是,这是不正确的。我想在我的代码下面应该是缺少或错误的东西。任何一点帮助都非常感谢。C编程语言,邻接矩阵

#include

#include

#include

#include

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

myMat[i] = (int*) malloc(sizeof(int));

}

myMat[x][y] = dist;

myMat[y][x] = dist;

x=0; y=0;

}

return 0;

}

2012-06-09

bledi

+0

你忘了告诉我们你的预期输出和你得到的是什么。 –

+2

'malloc(sizeof(char))'您保留的区域相当于只有一个字符。 –

+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 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值