代码如下,编译没有保错或警告运行结果是空的。请问问题在哪?如何解决?是打印数据部分有问题吗?
txt文件内容如下
8
0 1 1 0 0 0 0 0
1 0 0 1 1 0 0 0
1 0 0 0 0 1 1 0
0 1 0 0 0 0 0 1
0 1 0 0 0 0 0 1
0 0 1 0 0 0 1 0
0 0 1 0 0 1 0 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
typedef struct {
int Edges[MAX][MAX];//邻接矩阵
int vexnum;//当前顶点个数
}MGraph;
int CreatGraph(MGraph &G)
{
FILE *fp;
if((fp=fopen("matrix.txt","r"))==NULL)
{
printf("文件打开失败");
return 0;
}
fscanf(fp,"%d",&G.vexnum);
int i,j;
for(i=1;i<=G.vexnum;i++)
{
for(j=1;j<=G.vexnum;j++)
fscanf(fp,"%d",&G.Edges[i][j]);
}
fclose(fp);
return 1;
//输出边矩阵
printf("创建无向图边关系如下:\n");
for(i=1;i<=G.vexnum;i++)
{
for(j=1;j<=G.vexnum;j++)
{
printf("%3d",G.Edges[i][j]);
}
printf("\n");
}
}
int main(void)
{
MGraph G;
G=*(MGraph*)malloc(sizeof(MGraph));
CreatGraph(G);
return 0;
}